Live data from Hacker News

Clear is better than clever [pdf]

dave.cheney.net

221–230 of 335 posts

Re: Clear is better than clever [pdf]

#221

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 couldn't disagree more. Nested ternarys are NEVER okay. They are confusing and misleading to anyone new to your code base.

As someone that would be new to his code base, I neither find that confusing nor am I mislead by it.

Here's a blog post I'm becoming more and more persuaded by as time goes on:

https://medium.com/javascript-scene/nested-ternaries-are-gre...

Re: Clear is better than clever [pdf]

#223

Earlier quoted context omitted.

I don't agree. It occludes the nested structure. We can regard the condition as the head of a clause, and the ? and : as heads of sub-clauses: A ? B : C which leads to: return a b ? 1 : 0;

What you're writing here is equivalent to insisting that people do: if (a b) { return 1; } else { return 0; } }

[deleted]

Re: Clear is better than clever [pdf]

#225

Earlier quoted context omitted.

Writing for yourself is a fallacy. We can all read our own code. Write for the next guy who has to look at it and figure out what is going on.

Who should you be writing for? Everyone is different. What's clearer for one person might be harder to read for someone else.

totally agree, but life is full of compromise. I like to write code so that someone new to field could without too much effort follow what's going on but also so that someone better than I isn't so bored that they miss obvious things in the code (which does happen).

It's a balancing act which I have found to be one of the more fulfilling aspects of my work. Having people of different skill levels review my code has made this much easier.

One thing that I do a lot though is format code in a way that certain patterns appear for those of us that like to skim code. This generally means avoiding ternary operations except for very simple things where that pattern is easily recognized. But when to do that depends on the team(s) involved.

Re: Clear is better than clever [pdf]

#226

Using the keyword switch for successive condition testing is a travesty. Switching means taking one of several code paths based on a value. The problem with using a switch syntactic sugar for conditional testing is that the cases are not exclusive: switch { a > 5: ... a > 3: ... } Here a > 3 is not reachable: it tests a condition that overlaps with a > 5 completely, and a > 5 is earlier. This form of switch would be…

Either my minimal understanding of Go is showing, or this just proves your point, but don't you have your conditions backwards? `a == 4` would fail the `a > 5` condition and so execute the `a > 3` case, right?

Re: Clear is better than clever [pdf]

#227

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…

> return a b ? 1 : 0;

Is this a joke? This comes off like an order of operations test.

Re: Clear is better than clever [pdf]

#228
post #142

Earlier quoted context omitted.

Golang doesn't have enums

Neither does JS, but there are ways to accomplish the same thing[0]. [0] https://stackoverflow.com/questions/14426366/what-is-an-idio...

That's not really the same. That's badly designed and error prone.

Re: Clear is better than clever [pdf]

#229

Earlier quoted context omitted.

I don't agree. It occludes the nested structure. We can regard the condition as the head of a clause, and the ? and : as heads of sub-clauses: A ? B : C which leads to: return a b ? 1 : 0;

What you're writing here is equivalent to insisting that people do: if (a b) { return 1; } else { return 0; } }

[deleted]

Re: Clear is better than clever [pdf]

#230

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…

The fact that half the people in this subthread think that your version is less readable and half think it's more is really strange to me. If you have to traverse more lines and scroll down and switch between lines to track variables and conditions, that's unpleasant to parse. On the otherhand, excessively dense code where you have to figure out which of multiple function calls or syntax constructions to start at, then that's unreadable, too. But if you can do one thing and do that one thing in one line with predictable syntax, that's the ideal solution. I have a hard time empathizing with those who prefer verbosity over succinctness.
Post reply on HN