It's interesting that the comparator function is one of the examples, because that's something which shows how people often confuse verbose with clear ; I think it's best written in a single line: return a b ? 1 : 0; That's one line, compared with the 10(!) of his proposed method using a switch statement. Having worked with some "modern" codebases, I think verbosity is a bigger problem that's become especially preval…
I disagree. Reading the verbose example takes less time to understand, not just because the concept is clearer, but it is also visually easier to separate lines, even if that is 10 times as long as yours. If you see that somewhere, it will take a blink of an eye to understand, but your one line solution has to be read from left to right carefully considering colons and separating parts in your head.
Clear is better than clever [pdf]
161–170 of 335 posts
Re: Clear is better than clever [pdf]
#162It's interesting that the comparator function is one of the examples, because that's something which shows how people often confuse verbose with clear ; I think it's best written in a single line: return a b ? 1 : 0; That's one line, compared with the 10(!) of his proposed method using a switch statement. Having worked with some "modern" codebases, I think verbosity is a bigger problem that's become especially preval…
As long as verbosity is meant to make your code more explicit I'm fine with it. Personally, I find that ternary example you posted to be terrible code. We are not using parchment anymore, there is nothing to be gained by saving a couple of lines.
It's not about the line count, it's about expressing the main idea clearly and succinctly, which this code does.
My nitpick would be with the language constructs themselves. The ternary operator is simply a crutch for lack of an if-then expression returning a value.
if a b then 1 else 0
Or can add some white space to make the structure a little more clear: if a b then
1
else
0
end
Expressions like the ternary operator micro-optimize by replacing tokens recognizable as natural language words with arbitrary punctuation. But that's a language level complaint, which you generally don't have any control over once you've started the project or when editing existing code.Re: Clear is better than clever [pdf]
#163However, I've never seen a convincing and objective description of clear code.
Sometimes people mean that clear code is verbose code.
Sometimes people mean that clear code is uniformly formatted code. [For best results we should manually format the code the same. This builds character. Automatically formatting will not be considered.]
Similarly, sometimes people mean code smells are unfamiliar code. Or that code smells are patterns that they misused once and it bit them.
Objective methods of describing the ability for people to comprehend code when all else is equal do not exist as far as I have been able to see. The best we have is cyclomatic complexity, but there's some reason to believe that line count may be a better indicator (which can't be good). And cyclomatic complexity completely misses the effect of mutable or immutable state, the presence of bad APIs, poor variable naming, etc.
Re: Clear is better than clever [pdf]
#164Earlier quoted context omitted.
> Ternary isn't the same as a branch It should be. The C ternary is just an expression-oriented version of if/elseif/else. Languages with a functional bend simply make if/elseif/else an expression in the first place e.g. in Rust it'd be return if a b { Ordering::Greater } else { Ordering::Equal } Though obviously that specific version is an overly complex way of writing: return a.cmp(b);
i find it really odd that of all the knocking back and forth on this thread, you are the only person to suggest that using symbolic values for 'greater than', etc, improves readability.
Re: Clear is better than clever [pdf]
#165It's interesting that the comparator function is one of the examples, because that's something which shows how people often confuse verbose with clear ; I think it's best written in a single line: return a b ? 1 : 0; That's one line, compared with the 10(!) of his proposed method using a switch statement. Having worked with some "modern" codebases, I think verbosity is a bigger problem that's become especially preval…
I agree, but would go with: return a b; This does the right thing, but removes the nested tertiary which to me is a big win.
Re: Clear is better than clever [pdf]
#166It's interesting that the comparator function is one of the examples, because that's something which shows how people often confuse verbose with clear ; I think it's best written in a single line: return a b ? 1 : 0; That's one line, compared with the 10(!) of his proposed method using a switch statement. Having worked with some "modern" codebases, I think verbosity is a bigger problem that's become especially preval…
Although I've used a ternary operator or 'if' statement from time to time, in general I would like to model the boundrary conditions themselves and separate condition and action (reduces coupling and enhances the possibility of reporting what is going on).
In your case, the a
intList.sort(IntOrdering.reverse())
to sort a list of ints in reverse order, or intStringList.sort(IntOrdering.compose(StringOrdering))
to sort a list of tuples of ints and strings in natural ordering.So, creating this natural ordering needs to be done only once, over all the possible programs that can be expressed with ints. Maybe that one, single, time you'd be writing the actual expression you wrote above. But this lower-level logic will be well hidden by the abstractions used to reason at a higher level about correctness when placed orthogonally with other low-level code.
In other words, every time I write ternary expressions or long if-then-else expressions, I am either writing something low-level, or I need to abstract away.
Re: Clear is better than clever [pdf]
#167Earlier quoted context omitted.
As long as verbosity is meant to make your code more explicit I'm fine with it. Personally, I find that ternary example you posted to be terrible code. We are not using parchment anymore, there is nothing to be gained by saving a couple of lines.
"Personally, I find that ternary example you posted to be terrible code." It's not about the line count, it's about expressing the main idea clearly and succinctly, which this code does. My nitpick would be with the language constructs themselves. The ternary operator is simply a crutch for lack of an if-then expression returning a value. if a b then 1 else 0 Or can add some white space to make the structure a little…
I disagree. I find no clarity in using nested ternaries, but I understand it's a matter of opinion.
Re: Clear is better than clever [pdf]
#168Re: Clear is better than clever [pdf]
#169Earlier quoted context omitted.
> Is it really the state of the industry where a new person cannot learn a codebase, has no time or effort, and cannot read a single line of the language they were hired to work on, and that's the person we should build everything for? Seasoned 10x developers, titans of the industry, and all agree on simplicity and even they frequently make mistakes on easily confusable constructs such as nested ternaries (or even BS…
Or it could be that it’s subjective and there is no truly objective answer.
From my experience, and from my reading on tons of books, code, clean code suggestions, bug reports, etc, patterns of bugs are more commonly found on such code bits, that sacrifice clarity for succinctness or "clever points" (in fact that's the very premise of TFA).
Most respected elders in development also warn against those types of code.
Lacking objective research, that's the best we can get, and I'd say better than "it works for me".