by codesections · 6y ago · 0 comments
This is an excellent point, and one that not enough programmers pay attention to (well, outside of the APL family of languages, anyway!)
}
} // not
} // a single
} // fucking else
// implicit else
// return nullptr
return result;
}
A deeply-indented scope should mean "shit is complicated, think deeply about it," not "there were five times I tried something and elected to punt and return null if it failed." The LLVM style guide has the right idea; if you're gonna punt, early-return.As a side-note, I really wish the C++ community would take a page out of Lisp's book and stop giving closing braces their own lines. But that'll never happen.
I don't know what it is but I once interviewed for an embedded C position. During the interview, I wrote out some code samples. The only issue the interviewer found was that I had "returned early". He said there could basically only be one return in a function and it had to be at the end. I told him I'd never heard of that. He only said it was a "standard" that had to be followed because of the crypo involved in the code base.
Sadly, even though I didn't get the job, that stupid statement of only one return call in a function has stuck with me and lead to many mental arguments.
There are two main advantages:
- it's obvious when the function ends, no hidden early exits in the middle of nowhere
- if you need to do something before returning, there is only one place to do it (e.g. trim the output, e.g. log performances), and it make it easier to move this kind of last mile logic to a different function (if it's of any interest)
About your interview, don't sweat it, is not for that you didn't pass (you could do a great job and yet lose to a more fit candidate)
Before:
if (a.works()) {
// some code
if (b.works()) {
// more code
}
}
After: if (!a.works()) {
// error handling code, if necessary
return;
}
// some code
if (!b.works()) {
// error handling code, if necessary
return;
}
// more codeBail early and you wont have spaghetti code, plus you wont even allocate things to memory you're not even going to use, cause you're going to bail.
> if you're gonna punt, early-return
Oh for the love of God, please don't! That would make things even more unreadable.
Also, the closing braces are often accompanied by error handling and cleaning up code.
You might like the golang approach better, which is really the Linux kernel approach when I think about it....
More generally, you can specify KEEP and UNDO phasers in a block, that will fire whether the block is left successfully or not. So a typical usage would be `UNDO $dbh.rollback`.
Or are you saying that there are some lines of code at the end checking the post conditions? I'm not exactly sure what that would look like, it sounds a bit insane, especially if you do it for every function. Could you give an example?
(all I can think of is something where you could handle it with a finally-type clause)
maybe a fn shouldn't have post conditions?
(Aside from the attraction in small and simple code, each screen in forth is traditionally allocated a designated spot on the hard-drive so it's partially also for space and HDD usage efficiency too)
I haven't seen many developers do this: get a computer screen that can stand in the portrait position.
My setup currently is: laptop monitor + 27" screen in portrait. I never have the trouble of scrolling back up and down again when it comes to reading code.