19 ms·
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
by userbinator 8y ago
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 : 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 prevalent recently, since the idea of making a simple thing simple is so enticing and easy, but emphasising "micro-readability" to that extent (that's 10 lines of code for two simple decisions) will bloat the code and make "macro-readability" harder, and it's the latter that really matters for the understanding of complex software.
Anyone who has the experience of reading lots of highly trivial functions like those, and gotten the feeling "I've read a lot of code, and I understand what each little bit does, but what is the whole thing trying to do?" That's a symptom of excessive verbosity.
To offer another (subjective) point, I think the lack of semicolons or other statement delimiters does make code harder to read, as it makes it look similar to a run-on sentence. Natural languages have punctuation for a similar reason --- you can quickly scan over sentences by finding their delimiters.
Also, the counterargument: https://www.linusakesson.net/programming/kernighans-lever/index.php https://www.linusakesson.net/programming/kernighans-lever/in...
- __s 8y agoMeanwhile I'll write (a>b)-(a<b), which is what Python suggests after having removed cmp
- userbinator 8y agoThat works nicely too, for those languages with "1-0" booleans. For languages without them (Java, C# come to mind), the ternary is the next-best way.
- alexgmcm 8y agoI mainly use Python but that would still be unclear to me. I think the ternary operator is the sweet spot between too verbose and too confusing.
- deleted 8y ago[deleted]
- sametmax 8y agoNo, what Python suggests is just returning the value you sort on, or a tuple of the values to sort on. cmp() is deprecated because there is seldom reason to use it anymore. E.G: Sorting words according to their number of letter is just: sorted(words, key=len) You don't return 1, -1 or 0. If you want to rank participants of a game in a dictionary of scores using names as keys and points as values, you would to: sorted(scores.items(), key=lambda score: score[1]) Again you don't return 1, -1 or 0, just the value that is significant. It makes it very, very simple to reason about.
- Leszek 8y agoThank you for mentioning micro- vs macro-readability, I feel that too often people concentrate on the former without considering the latter.
- wayneftw 8y agoThe multi-line comparators are definitely clearer and more readable to me. The 10(!) lines of code you referenced are actually only 8(!) lines compared to your 1(!) line because you included the function declaration and the final brace in your count. The line you proposed is 34(!!) characters wide. I wonder if your statement will continue to grow past 80(!!!) characters as your operand expressions grow in width? Will you transform it back to multi-line when that happens? However, I'm not sure why we're exclaiming line counts at all and now, apparently, character counts. Agreed on the semi-colons though.
- illys 8y agoMaybe you'll prefer return (a < b) ? -1 : // a smaller (a > b) ? 1 : // a greater 0; // equal
- beobab 8y agoThis is a beautiful way to express multiple ?: expressions.
- kazinator 8y agoI 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 : a > b ? 1 : 0;
- atq2119 8y agoWhat you're writing here is equivalent to insisting that people do: if (a < b) { return -1; } else { if (a > b) { return 1; } else { return 0; } }
- deleted 8y ago[deleted]
- crististm 8y agoI find your example to be on the clever spectrum. If anything, it should remind you that clarity depends on the context.
- deleted 8y ago[deleted]
- illys 8y agoFully agree! Combinations of if/else can seriously damage readability and maintainability. I (all the time) face verbose code that pretend to be readable and that requires lots of attention just to find out you are setting a single variable across 10-20 lines. Example: if (cond1) { myVar = 1; } else if (cond2) { if (cond3) { myVar = 10; } else { myVar = 100; } ... } else { myVar = defaultValue; } * huge number of lines * not obvious (until we read all) that we setup a single variable * combination of conditions difficult to trace for each given value * high risk of forgetting cases... Instead of myVar = (cond1) ? 1 : // comment case blabla (cond2) && (cond3) ? 10 : // comment case bla (cond2) && (!cond3) ? 100 : // comment case blablabla ... defaultValue; // comment default * one line per given value * operation on a single variable obvious * match between conditions and values obvious * high visibility on all the cases * even more readable with proper vertical alignment * a comment can be added on each line to document the case [sorry for the multiple edits: I had a hard time getting { } properly display, they ate up the new lines]
- couchand 8y agoFYI https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- illys 8y agoGreat info, thanks!
- maxxxxx 8y agoI actually prefer the long version. But I think the real trick is to be consistent in style. Parsing ternary operators is a learned skill and so is parsing long if statements. You can get used to both.
- markrages 8y agoProgrammers have to learn to read different styles, to read code from other projects. So use the construction that is clearest in its local context. Style is a matter of taste, just like writing English: there are synonyms and idioms for every idea that you want to express, so use that freedom to choose the clearest communication. Like English, programming code is meant to communicate to other humans, so don't artificially limit your vocabulary in the name of consistency. Consistency is a hobgoblin.
- saagarjha 8y agoFWIW, I write a decent amount of "clever" code (think C right out of K&R) but this took me a couple extra seconds to parse. Maybe it's an acquired skill?
- bayindirh 8y agoClear code is supported because it can be maintained easier by other people, and the original developer won't need to remember the cleverness he/she did in the first place, when he/she returns to the code six months later. If I write clever code, I document it extensively on top of the clever code, with references if possible. It's about the life cycle, maintainability and longevity of the code in the first place.
- jbverschoor 8y agoreturn -1 if a < b return 1 if a > b return 0
- jstewartmobile 8y agoThank you for taking up brother Ken's righteous crusade
- deng 8y ago> return a < b ? -1 : a > b ? 1 : 0; This depends on the right-to-left associativity of the ternary operator, so this definitely counts as 'clever'.
- macspoofing 8y ago>To offer another (subjective) point, I think the lack of semicolons or other statement delimiters does make code harder to read, as it makes it look similar to a run-on sentence YES!! And what do you think the statement return a < b ? -1 : a > b ? 1 : 0; looks like to many people? One big 'run-on sentence'. It took me a few moments to parse that line. I had a good idea what it was doing, but I had to do a double take to make sure there wasn't some subtle bug in that line. And that's the point here. In some sense, you are writing to the lowest common denominator. You're a clever guy who can optimize their code to minimize line count and terse statements are obvious to you, but your code will be touched and looked at by developers of all levels. Developers that will have to fix your bugs, and extend your code, or refactor it - years after you write it. The standard for collaborative programming is to write code to be understood, not to show how clever you are, or to minimize character count or to minimize line count.
- lsaferite 8y agoI personally find this one-liner mentally difficult to parse in a glance. Yes, I can fully understand what it's actually doing, but when I glance at the code I have to focus on it too long before I am sure of what it is doing. For better or worse, I operate mainly in the PHP world and the recently added spaceship operator is a solution to this. While I think it's a crazy operator on the one hand, on the other it's much simpler to use. https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.spaceship-op https://www.php.net/manual/en/migration70.new-features.php#m... return a <=> b; EDIT: fixing typos
- Random_Person 8y agoI couldn't disagree more. Nested ternarys are NEVER okay. They are confusing and misleading to anyone new to your code base.
- _bxg1 8y agoI would make the exception that if you judiciously indent then across multiple lines, to reflect the nested structure the way the if/else equivalent would, then they're fine. But yeah, nested ternaries on one line are impossible to follow.
- demo123 8y agoIf you even need to indent them then why you don't write a full if else statement instead? Nested ternaries shouldn't be used at all.
- _bxg1 8y agoFor languages in which if/else is unfortunately only imperative, not an expression
- _bxg1 8y agoIMO if if/else can be used as an expression, your language shouldn't have a ternary operator at all
- josephg 8y agoIf/else chains are worse because they allow problems like this: if (cond) { var1 = x; } else if (cond2) { var2 = y; } else { var1 = z; } Did you notice the second block assigned to var2 instead of var1? Best case it’s a gotcha for anyone reading the code later. It might be a bug by the original author; we can’t tell at a glance if this behaviour was intentional. Ternaries remove this problem. The intent of the author is clearer, and (with proper formatting) there’s no gotchas for anyone reading the code later.
- pekim 8y agoreturn a < b ? -1 : a > b ? 1 : 0; That's a nice approach. However the document is about Go, and Go doesn't have a ternary operator. So that probably leaves the switch solution as the clearest one.
- brmgb 8y agoI tend to avoid the ternary operator because in my experience even people who think they know how it works are often unaware of its edge cases when it comes to silent numeric type conversion (looking at you Java). If you don't put the same type on each return sides of your ternary, you might be in for a nasty surprise.
- maxxxxx 8y ago“If you don't put the same type on each return sides of your ternary, you might be in for a nasty surprise.” Definitely true for JavaScript and PHP. I am always surprised what expressions can evaluate to true or false.
- yiyus 8y ago(a>b)-(a<b)
- ptero 8y agoIn my experience, verbosity is a slight detriment to clarity -- concise code tends to be easier to understand, although the relationship is not very strong and there are many counterexamples. The key to clarity and maintainability for me, though, is in the structure. If the code is structured in manageable blocks with clear description on what each block does and how blocks fit together I care little whether within each function the code is concise or verbose. As a data point, in larger projects with good structure, I generally saw more use concise code than verbose, which may account for my original "concise is better" bias. Just my 2c.
- deepaksurti 8y agoI always found CL's `cold` macro very readable to use instead: ``` (cond ((< a b) 1) ((> a b) -1) (t 0)) ``` edit 1: formatting
- coldtea 8y ago>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 : a > b ? 1 : 0; That's nowhere near clear. I frequently use a single level ternary, but this is an abomination and very easy to make a subtle error in.
- java-man 8y agoalso, try to step through this in a debugger.
- vladus2000 8y agoI completely agree. At my first professional programming job, I had to follow a style guide that forced me to code the verbose way. It drove me nuts and I found another job about 6 months later. The less lines of code, the faster you can read a code base, apply fixes and have less bugs. Obviously you can take that too far and make code so dense no one can read it. It is finding a balance between the two. Comments can go a long way towards making complex code understandable within reason.
- mannykannot 8y agoWhile code of this size and scope can be unclear, it is not where the real problem lies. In general, what's not clear is some version of "what does this do" and "why is this being done" where "this" is often a function call with an arbitrary amount of complexity behind it. When the code is mostly calls to well-documented APIs, clarity is achievable (though not guaranteed.) if it is more complicated than that, I do not know that there is any way to achieve clarity other than to structure it, internally, in that style.
- pier25 8y agoAs 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.
- ConcernedCoder 8y agoI disagree, what is "saved" is your time when reading/understanding... why spend time to read 10 lines and figure out that it actually does what a simple ternary operation would accomplish?? On that same vein, if a ternary operation is going to throw someone for a loop, I've got some bad news about their career in programming.
- lovich 8y agoThese are both extremes and both bad. The 10 line switch statement to switch a variable is a high cognitive load for no good reason but so is a nested ternary under pretty much any circumstances, but especially with single character variables and magic numbers. >On that same vein, if a ternary operation is going to throw someone for a loop, I've got some bad news about their career in programming. You're right about someone having and issue if they can't understand this on it's own. What was the last application you worked in where that single line is all you had to understand to accomplish whatever task you were working on?
- pier25 8y ago> What was the last application you worked in where that single line is all you had to understand to accomplish whatever task you were working on? Exactly. The bigger constructs expressed in functions, classes, etc, are where the attention should really be. The smaller ideas expressed in single lines of code are, should be, insignificant in terms of cognitive load. There are some exceptions to this, in particular very high performant code. We all know that "premature optimization...".
- lovich 8y ago
- jaabe 8y agoIs your code really clear though? What are a and b? What types are they? What if they, or one of them aren’t that type? Why would you call the function? When would you call it? I mean, I get that your example is directly related to the point you are making in verbosity, I don’t even necessarily disagree with you, but your example isn’t very clear to me.
- massung 8y agoI always feel like the idea of "clear" vs. "clever" is missing the point. When I've come across - or written - "clever" code in my career, it's always been about making something that normally wouldn't fit into memory - or perform slowly - viable. The examples of this are numerous and still happen plenty today on modern hardware (e.g. games, VMs, NaN tagging). There is nothing wrong with "clever" code so long as it's _clearly_ documented and abstracted away with various types, macros, functions, etc. Then there's all other code. And what I feel the author wanted to say was that anyone coming along later shouldn't have to think twice about what a given line of code is doing. If there's a bug, sometimes I'll be lucky enough to have a debugger, sometimes not. But, I shouldn't have to second guess a line of code to determine whether or not the bug is there. In your nested ternary expression, I would be second guessing all over the place; what's the precedence? Hell, whenever I use ternary operators I still force myself to parenthesize the conditional, because while operator precedence in C/C++ is well defined, it's not well known. Clarity is another reason I prefer type safe languages over dynamic ones for critical code. It allows me to be a little more "expressive" with my code where the reader coming along after-the-fact can be assured that everything works out type-wise. Having worked on code that people's lives depended on, there are times when getting it wrong isn't an option. I can only feel for the software engineers of the 737 MAX going back over gobs of code and trying to dissect what went wrong and come up with a fix. They'd be heavily scrutinizing every line of code, and I can promise that a nested ternary would be very scrutinized. But, so would a deep chain of if, elsif, elsif, ... and recursive functions (which are often a _big_ no-no in mission-critical code). Although, I don't think anyone here effectively argue that recursion is something to be considered "clever". It's all about a code reader being able to _prove_ to themselves that the code does _exactly_ what it looks like it does and nothing more. I'm not a Rust fanboy, but - just like static type checking - the borrow checker adds one more level of surety to the reader that another thread isn't coming along and stomping over the data. And this is without needing the whole context of the code base. This is a _big_ deal.
- ummonk 8y agoThat is extremely hard to read, at least without parentheses to show where to split the expressions at a glance.
- baby 8y agoYour single line is terrible code.
- kissgyorgy 8y agoI 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.
- yiyus 8y agoBut there are other benefits. You need to make a compromise and there is no win-win situation. It is true that the 10 lines version is totally obvious for everyone, but I do not agree it will make easier to understand a complex piece of code. If I fill my screen with a function that makes a simple comparison, I will need an IDE and at least a couple of monitors for a function that actually makes something, I will not be able to understand the code because I will be too busy navigating it.
- unwind 8y agoI agree, but would go with: return a < b ? -1 : a > b; This does the right thing, but removes the nested tertiary which to me is a big win.
- lisper 8y agoPersonally I prefer (cond ((< a b) -1) ((> a b) 1) (t 0)) Or, if that's too many parens for you: (mcond (< a b) -1 (> a b) 1 0) The definition of MCOND is four lines of code and can be found here: https://github.com/rongarret/ergolib/blob/master/core/ergobase.lisp https://github.com/rongarret/ergolib/blob/master/core/ergoba...
- edejong 8y agoI find it interesting to see so much emphasis on small syntax nit-picking while totally forgetting the large scale question. 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 < b statement is most likely part of the natural ordering of the type of whatever variable a and b are. So we should be much more interested in describing the natural ordering of this type, so we can say things like: 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.
- JJMcJ 8y agoParentheses would help make the one liner simpler but your basic point seems quite accurate. In the end, what's simpler, spending 15 seconds looking at a one liner using well known language syntax, or digging through half a screen of if states, for loops (use iterators instead), for two examples.
- superlopuh 8y agoPresenting '{' on newlines feels like a bit of a strawman to me, I'd expect the more verbose styles to still look something like if (a == b) { return 0; } else { return a < b ? -1 : 1; }
- Theodores 8y agoCompilers are very good at doing the ternaries for you. With good ol' Javascript you get things like the Google Closure Compiler that will return yet more Javascript rather than some bytecode, this informs me that my use of ternaries is a waste of time. I might as well go for the if/then/else type of approach and refactor it when it gets ugly into some functions. My excuse for taking this approach in all languages is accessibility, with accessibility applying to people who can't really code. Whatever the language you can hack a few if statements even if you don't know the language. There are people who are learning and you want to make it 'accessible' for them. Let the compiler do the work and stay off the bleeding edge features. That includes spaceship operators in PHP. I also think it is worth keeping code a few versions old, so in the case of the spaceship operator, I can do if/then/else things for the compiler to do the work and have my code work on someone else's box that is still on PHP 5.4 because the main software they use doesn't work on PHP 7.3. Or in Javascript, it is obviously best practice to use the fancy new things but if your web page dies in the console on some Apple iPad running an old version of Safari then you can end up refactoring those best practice iterators into for loops. Invariably these refactoring exercises are hastily done, so you can come a cropper. Another thing is that if I had to explain my code to someone then the nearer to natural language the better. The example ternary would require me to stop and think for a moment, if it was in boring if statements I could explain it to a not-so-technical person in terms they could understand, even if they could not write it themselves.
- userbinator 8y agoThe example ternary would require me to stop and think for a moment, if it was in boring if statements I could explain it to a not-so-technical person in terms they could understand, even if they could not write it themselves. On the contrary, even a "not-so-technical person" is likely to be able to guess at the meaning of a question mark; and as a bonus, it's not specific to English either.
- stcredzero 8y agoIn Smalltalk, we had return values from ifTrue:ifFalse: statements. This served the function of the ternary ( ? : ) while enabling if-then style brackets. Also, deeply nested logic and methods longer than 10 lines were regarded as "code smells" in the community, so a lot of that logic was implemented through polymorphism. Anyone who has the experience of reading lots of highly trivial functions like those, and gotten the feeling "I've read a lot of code, and I understand what each little bit does, but what is the whole thing trying to do?" That's a symptom of excessive verbosity. Smalltalkers sometimes solved this through executable comments. There would be some code, with a message to "debug this," and we could just highlight the code and quickly and easily walk through it in the debugger. This demonstrates one of the advantages and disadvantages of Smalltalk: Everything could be embodied in the interaction of relatively small objects, so everything tended to be modular, more easily. The disadvantage comes from the fact that the interaction has to be understood to understand the system as a whole.
- sagichmal 8y agoI strongly disagree that your example is better than Dave's. I would much prefer to see Dave's switch statement in a new-to-me codebase than your ternary.
- syn_rst 8y agoDepending on what syntax constructs are available, it's possible to achieve clarity without the verbosity or "line noise factor." For example, in Rust you could write that same code as: match a.cmp(&b) { Ordering::Equal => 0, Ordering::Greater => 1, Ordering::Less => -1 } It's easy to see what the possible outputs are and what is produced in each case. The compiler even checks it to make sure you didn't forget any situations. The clarity comes at the cost of some verbosity, but there's only 4 lines of functional code (not counting the closing brace) here compared to the article's 10. You'd get around the same number if you added line breaks to make the ternary example more readable.
- kazinator 8y agoNested ?: is hard to read due to the precedence. It's better to spend a few lines and break it up: return a < b ? -1 : a > b ? 1 : 0; If that is too much: return a < b ? -1 : a > b ? 1 : 0; Other possibilities: return a < b ? -1 : a > b ? 1 : 0; return a < b ? -1 : a > b ? 1 : 0;
- otikik 8y agoIt's not 10 lines, it's 8. Your implementation would need the function declaration and the } at the end too. Now consider this: return a > b ? 1 : a < b ? -1 : 0; Or: return a == b ? 0 : a < b ? -1 : 1; Or: return b < a ? 1 : a == b ? 0 : -1; Are all these equivalent? Figuring that out is much more difficult than their 8-line counterpart, at least for me. My eyes have to keep jumping from left to right in order to do the checks, like they would do in a piece of uglyfied javascript. That said, context is everything: if your one-liner was the single line in a function called `comp` with `a` and `b` as parameters, I would not comment on it in a code review. However, if this line was part of a bigger function, and if instead of `a` and `b` we had `someLongVariableName` and `someOtherVariableName` I would not approve the pull request until this "oneliner" was changed into something else (possibly a call to the `comp` function).
- commandlinefan 8y ago> Are all these equivalent? But is that really what you're looking for? I see that idiom and I think, "Ok, this is a comparator" and I move on. If I suspect a _bug_ in the comparator, then I'll look at it more carefully, but the context is about writing code whose intent is clear, not writing code that can be scanned for bugs easily - which would be a tall to impossible order anyway and isn't made any easier by sprawling your code across 10 lines where one will do.
- otikik 8y agoWell that is what I think if it's on the first case I mentioned. I see it contained inside a comp function and I move on. If I see it "on the wild" (as part of a bigger function), however, my train of thought goes "Wait, why is this piece of code here instead of a function call called `comp`? Is this a comparator or is this something else? Ok geez time to do the javascript eye dancing thing. You know what? I'm just not going to approve this and demand that this piece of code is at least given a proper name and put somewhere. Possibly with unit tests"
- vivekseth 8y agoWhy not something as simple as `return (a - b)` ? It’s concise and simple to understand. Instead of returning 1 or -1 it returns a positive or negative value with I think is equally effective.
- commandlinefan 8y ago> return (a - b) That will overflow if a & b are longs.
- Vadoff 8y agoCouldn't you just break the ternary up? if a == b { return 0 } return a > b ? 1 : -1 Also it would be ~7 lines, not 10? if a > b { return 1 } else if a < b { return -1 } else { return 0 }
- ApolloFortyNine 8y ago> return a < b ? -1 : a > b ? 1 : 0; Is this a joke? This comes off like an order of operations test.
- c3534l 8y agoThe 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.
- a1369209993 8y agoI think part of the problem is that it'sn't visually obvious that ? and especially : are lower precedence than < and >. I suspect that most of the people with genuine objections would feel better about: a<b ? -1 : a>b ? +1 : 0 # or maybe a < b ?? -1 !! a > b ?? +1 !! 0 # perl6 style # or a < b then -1 else a > b then +1 else 0 not for verbosity vs succinctness but for the mnemonic that visually-smaller operators bind more tightly.
- ivanhoe 8y agoIMHO this is pretty hard to read, unless I guess you work on code written this way every day (and most of us don't). It's not that I can't understand it, but it takes unnecessary extra effort and time to process. I want to be able to scan quickly throw a code, concentrating on understanding the logic behind, not on syntax itself, and that's very hard when it's written very dense like this. For me ternary operators are great when used on simple expressions, without nesting, and always with brackets around expressions to help eyes to navigate. In your example I'd always prefer if/elseif/else structure in place of this (unless there's a spaceship operator of course).
- dr0verride 8y agoI agree. I find that overly verbose code can be just as difficult to follow as code that's too clever.
- loup-vaillant 8y agoNitpick: I often break nested conditionals in several lines, like this: return a < b ? -1 : a > b ? 1 : 0; Reads like a switch statement, yet not overly verbose. I can parse it even more easily than the one liner. Your point about "macro-readability" is spot on though. Nobody should care about the readability of an isolated piece of code. We should care about the readability of pieces of functionality and that generally requires understanding of a whole function, or even a whole class/module.
- Retric 8y agoUsing semantically meaningless structure is unlikely to survive being edited. if (a < b) return -1; if (a == b) return 0; return 1; Is simple boring code, has the advantage of looking odd when something odd happens.
- anbop 8y agoOne could make analogues to prose writing. The author's is in the style of xkcd's "Thing Explainer" where they deliberately only use 1000 words: https://xkcd.com/1133/ https://xkcd.com/1133/ Yours is in the style of James Joyce where it's very dense... the easiest to read is probably somewhere in between
- Retric 8y agoThat’s a nested if statement which stops being clear. return a > b ? 1 : a < b ? -1 : 0; return a < b ? -1 : a == b ? 0 : 1; return a == b ? 0 : a < b ? 1 : -1; return a > b ? 1 : a == b ? 0 : -1; return a != b ? a > b ? -1 : 1 : 0; return a != b ? a < b ? 1 : -1 : 0; Are all equally valid ways of writing that code. You can get used to a convention that makes it seem more clear, but reading it they are all equally easy to decode.
- abawany 8y agoIn recent times, this verbosity has been accompanied by the (to me) insane decision in some JS prettifier plugins (opinionated even) of effectively putting one word per line. Now every if condition spans 4 lines on average. Delightful!