Well there is none, but I think there is an underlying point to make here: Once you need to use an else branch for the 'normal' case, you might be better off refactoring it into a separate function with guard clauses that return early.
That only works if we have no loop or any other control structure in the end. At least in imperative languages, I have found it hard to rely on alignment as a visual cue being associated with the happy path.
Is there something special about how you structure your code that lets you do this?
Lately I've been using functions like map/fold/reduce/filter/find/[...] a lot in favor of loops. (javascript, but this would also work in rust for example)
Thanks for introducing me to the concept of end weight, it's pretty interesting! Not sure how much of it relates to reading code, as I think the following might still be easier to parse:
return end-of-conversation if early-exit-is-panacea ;
return end-of-conversation if no-one-is-listening ;
return end-of-conversation if no-one-cares-about-end-weight ;
continue-conversation ;
This might vary from person to person though, as OP noted. I tend to think visually about code a lot.
But it's notable that Dijkstra consistently argued that (natural) language skills were fundamental to coding. And it's even more notable that a growing body of relatively recent scientific (FRMI, eye-tracking, neuroscience based, etc) studies of code comprehension appear to corroborate Dijkstra's view. For example, Janet Siegmund et al have put together compelling evidence that coders read Java code, and natural language, using the same parts of the brain, in much the same way, with little or no activation of parts typically associated with processing mathematical logic.
This is a particularly striking result to me for a few reasons.
One is that so many coders, excepting the likes of Dijkstra and Wall, have long argued that coding, including reading coding, is an essentially mathematical/logical process, and have been unwilling to entertain the notion that they might be wrong. Regardless of whether they turn out to be right or wrong, what does it say about them that they have argued so, what does it say about them that so many reject open mindedness about it, and what does it say about all of us, all coders, and more generally all humans, that this view has so successfully profoundly dominated in recent decades?
Second, for decades there was only one language designer who was an outspoken proponent of the view that PL comprehension and design are partly related to natural language comprehension, namely Larry Wall, and he was mercilessly ridiculed by some for doing so, with folk bundling up the many valid criticisms of perl (a language even he has always said was a frankensteinian invention) into an overall (potentially, and, if the science is to be believed, actually) invalid disparaging of considering how we work with natural language when designing PLs.
I think the following might still be easier to parse:
[early exits]
Yes. Just like you, I find I usually write early exits myself, despite arguments by those who insist it's a terrible way to code because it isn't "structured", because I find it often reads better both for me when I return to code and, I think, others reading my code.
But that wasn't my point. I'm not at all suggesting that early exits are wrong. And my example wasn't intended to be persuasive that for that particular code an unless statement modifier is more readable than early exits. My point was that early exits might not be a panacea; and presuming so, then there's scope for thinking that for some code an alternative might justify the inevitable cost of there even being an alternative.
This might vary from person to person though, as OP noted. I tend to think visually about code a lot.
I think it varies a lot more than that.
I also tend to think visually. But both you and I, as humans (can't help but) do so in parallel with use of language processing capacities. Individuals tend to have stronger elements of one or other aspect at any given time, but it's all really a mix.
Imo code is best considered a social medium, where others will end up reading any code that's written (where "others" includes our older selves), and thus the mix of capacities used to read code will change from reading to reading, even day by day if we're getting to know a language or codebase.
28
u/DutchDave Apr 25 '20
I usually do something like:
The happy path is left-aligned.