-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
engine: Minor refactor to prefer simplified ranged-for-loop #11234
engine: Minor refactor to prefer simplified ranged-for-loop #11234
Conversation
625656d
to
55157e7
Compare
55157e7
to
cb5df28
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the willingness to refactor, but in this case I'm seriously doubting the usefulness. The resulting code is harder to read and understand for beginners while providing no obvious performance benefit. The only change that would've made sense IMO is to replace the oldschool for loop with index lookup by a ranged-for loop...
I'm quite a noob on C++20 ranges, so I would've liked to see that implementation, maybe the resulting code would've had better ergonomics. |
I agree with @Swiftb0y. I think it is worth to eliminate all Introducing lambdas is not that trivial. They need a thoroughly review and manual tests. Doing that in a mass refactoring will bind resources we can make better use of, in introducing new features ir fixing bugs. |
So @Swiftb0y, the ranges version of this PR is still available to view here: 625656d 🔍 | (raw .patch version) 📥 |
@daschuer I understand that the project has limited bandwidth to review refactoring PRs especially if it uses the standard library or lambda syntax. I apologise; it was on me to explain the reason for the code in this PR & what the specific lambda does, so to ease the review of the PR. ✔️ It is great @daschuer that we are in agreement that: 😸
As the popular computer science joke goes:
As you already know, the recommended common practice is by using the standard library, or some ranges form of code, it helps greatly to eliminate the even potential of variable-counter problems, and provides a uniform syntax to express "intent" of code. From: C++ Core Guidelines
Maybe helpful for @Swiftb0y:
|
Thank you, I think the ranges version is a little easier to understand from a beginners PoV. The only thing that I still dislike about that is the conversion from The links you posted mostly concern themselves with increased complexity from the lack of use of iterators or ranges. While that's certainly the case in more complex code, I don't think the upfront cost of being paid makes sense. Sure, when you have complex filter code, using for (const ChannelInfo* pChannelInfo : m_channels) {
if (pChannelInfo->m_pChannel->getGroup() == group) {
return pChannelInfo->m_pChannel;
}
}
return nullptr; |
I agree with you a lot @Swiftb0y! 😉 "Possible implementation of std::find_if" for (; first != last; ++first)
if (p(*first))
return first;
return last; So you see @Swiftb0y, this
for (const ChannelInfo* pChannelInfo : m_channels) {
if (pChannelInfo->m_pChannel->getGroup() == group) {
return pChannelInfo->m_pChannel;
}
}
return nullptr; In your code example @Swiftb0y, one beginner would already:
So why teach & continue non-standard coding methods in the project's codebase over the standardised way? I can understand & relate, if one has not used To quote: C++ Core Guidelines - P.1: Express ideas directly in code
We both agree that the construct is basic and simple, so let's look out for areas where we can use the basics of the standard way in the codebase in C++. 😉 |
I agree with you once more @Swiftb0y 😉 .
In your kind example, there are two code-patterns that seasoned C++ developers can see: for (const ChannelInfo* pChannelInfo : m_channels) {
if (pChannelInfo->m_pChannel->getGroup() == group) {
return pChannelInfo->m_pChannel;
}
}
return nullptr;
If one uses S.O.L.I.D principles, this area of code has maybe already violated The conversion code highlights this awkwardness. that piece of code-block must do above two responsibilities at once. To become some sort of better C++ "standardised" code, the point is to represent some sort of "maybe" state. In C++17 and later, we have an official datatype to represent this state. It is How to use it in the codebase with your current recommended code example... well, that is slightly outside of the scope of this current PR. But it is important down-the-line in making the code more closer to C++ standardisation (of algorithms & datatypes). |
I fully agree with most of the points you make. The only thing I disagree with is that in order to understand I also acknowledge that the design of the entire function and even the class is flawed ( The biggest problem I have with the proposed change (which also introduces the unwanted complexity) is the intermingling / conversion between the plain pointer-based code and the iterator based code. Unfortunately due to the member function signature, you can't change that without a bigger refactoring. For that reason I prefer the iterator-less function because the introduced shift in paradigm only introduces additional complexity. |
For me the issue is not the use of |
006dd64
to
8756251
Compare
Prefer ranged-for-loop structure. As discussed in: mixxxdj#11234 (comment)
8756251
to
f14f863
Compare
Minor refactor to prefer ranged-for-loop structure. As discussed in: mixxxdj#11234 (comment)
f14f863
to
5d673ed
Compare
Can you rebase this on main? The Arch linux build failure has been fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Oh, never mind this is only a small commit.
Thank you. |
Ah, just saw your update. Thanks for merging, grateful! 😸 |
A minor refactor to reduce codebase size using standard algorithm functions.
(Previously this PR used the C++20 ranged version, but removed it because sadly the Apple Clang buildchain does not support it) 😿