0% completed
The C++ code in this course is generally lacking, but this example was particula...
Adam Sweeney
Jul 20, 2022
The C++ code in this course is generally lacking, but this example was particularly over-complicated. While I can understand certain design decisions, there are a couple others that really should not have been made. And a few times, outdated practices are employed.
The biggest offender in this lesson is the Interval itself. It should be a struct, and the constructor should utilize the initialization section for more efficient, and more concise code.
My next gripe is the expression(s) *intervalItr++. The dereference and increment are close enough in precedence that I have to look it up every now and again. While what it accomplishes in a single expression is neat, it hurts readability. A similar tack can be used to simplify the while loop while increasing readability and eliminating an intermediate variable.
Global using namespace std; should never be taught. Even in a technical interview, I'd see it as a red flag if they don't caveat with "I would never do this in production." Which they usually won't.
Others, like the Java-like functor are much more subjective. In this case, just make it a free function. A case can be made for wrapping other functions that rely on helper functions which we don't want to expose publicly, but even then I might prefer namespaces and the unnamed namespace to keep the helper local to the translation unit. That practice makes more sense in a larger, multi-file project, hence my saying it's much more subjective.
Here's my reworked solution. While it has a bit more boilerplate (mostly to make main() easier on the eyes), I think the algorithm is easier to understand.
#include #include #include struct Interval { int start = 0; int end = 0; Interval(int start, int end) : start(start), end(end) {} }; bool operator
3
0
Comments
Adam Sweeney3 years ago
I'll throw in that overall, this course has been an immense help and a great learning resource. There are still some rough edges all around, but this is much better than the free resources I had been using.
Adam Sweeney3 years ago
I'm seeing now that there's a character limit, and code formatting doesn't copy/paste very well.
Design Gurus3 years ago
Thanks Adam for the appreciation and the feedback. We consistently work on improving and adding more content to the courses. Yesterday, we introduced a new pattern called "Island (Matrix Traversal)".
Let us message you to understand more on the character limit issue.
On this page