6 ms·
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
by pier25 8y ago
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.
- 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 agoThe bigger constructs are made out of these smaller pieces. Focusing on just the high level abstractions will end up with your application being difficult to fix when something lower level breaks and focusing on just the lower level code will cause your application to end up unscaleable or refactorable because of poor design. It's a question of trade offs but you cant entirely ignore one or the other
- datavirtue 8y agoYeah, don't nest ternaries. That's evil.
- pier25 8y ago> why spend time to read 10 lines and figure out that it actually does what a simple ternary operation would accomplish?? Because when something is expressed in 10 lines explicitly, generally you can simply skim over the code to get an idea of what's going on without having to go into the details. "Smart and clever" code, like using nested ternaries, demands you to focus and try to figure out what is going on.
- wtetzner 8y ago> Because when something is expressed in 10 lines explicitly, generally you can simply skim over the code to get an idea of what's going on without having to go into the details. Which is why important details get missed when reviewing verbose code.
- Misdicorl 8y agoThis is what good function names and default code folding is for...
- Cthulhu_ 8y agoBecause if the structure is different from what you're 'used' to, you spend ten times as much time reading (or as the original presentation coins, "decoding") that one line to understand what it means. You don't read ten lines, you decode them. If it's ten simple lines, decoding them is easy. If it's a nested ternary, it's a lot more cognitive load - unless you're used to nested ternary expressions. However, I'd say it's a fair assertion to make that most people aren't used to reading nested ternaries. And I think that's what Go (and this presentation) is about; you shouldn't need to get used to a certain code style to be able to decode it. You should be able to open up a file and not be surprised. If I were to come across a nested ternary, my first reaction is a raised eyebrow and a "wtf?". The wtfs / hour is one of the metrics that Go language is based on.
- tomxor 8y ago> We are not using parchment anymore, there is nothing to be gained by saving a couple of lines. This is not what the OP is talking about - we are not using parchment anymore, but we still have the same brains, being able to fit more code in our head with less i/o allows is a more holistic view. There is a balance in between terse and verbose that produces a reasonable length of code for what it is doing, and that is subjective, to the individual and the code - so you are never going to agree on everything. But I say fuck no to people who want to lower everything to the common denominator, that is mediocrity, I don't want my grandma to be able to read the code if it makes macro comprehension horrible to the point of making it unmaintainable spaghetti.
- Cthulhu_ 8y agoIt really is a balance, and it's very hard to get it "just right". I mean I can appreciate a succinct oneliner (which is why I really like functional programming, doing a .map() instead of a for loop for example; it's a lot more compact without losing expressiveness, however, you do need to learn the lingo at first). When I first went from Java to Scala, I found that while you can perform the same logic in 10 times less lines of code, each line is also 10 times as powerful and complex. A random example I found in code from back then: private def fetchDevices = (for (device <- devices.find()) yield device.getAs[String]("deviceId").get).toList Super concise but at the same time the code doesn't actually do much as far as I can decode now, it's a lot of type wrangling.
- tomxor 8y ago> It really is a balance, and it's very hard to get it "just right". Yup, and you will find bad examples at both ends of the spectrum. And sometimes it doesn't even have anything to do with terse vs verbose, it's just bad code, not because of that one part of code but because of the context requiring it - just like in your example, I don't know scala, but if it's doing a lot of "type wrangling" then the real problem is probably further upstream from that point in the code, and no amount of terseness or verbosity is going to make it more clear - we are only discussing one dimension of the many subjective and objective dimensions that make code clear or confusing.
- jimbokun 8y ago"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 elsif 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 elsif 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.
- pier25 8y ago> It's not about the line count, it's about expressing the main idea clearly and succinctly I disagree. I find no clarity in using nested ternaries, but I understand it's a matter of opinion.
- michaelcampbell 8y agoI find German inscrutable, and yet Germans don't. Everything's hard to read until you learn to read it.
- rishav_sharan 8y agoOr don't learn German but continue speaking in English which is inherently same as German in functionality but at least you and people around you can understand each other.
- uryga 8y agoit's also nice with Haskell's Multiway If: x = if | a < b -> -1 | a > b -> 1 | otherwise -> 0 (and `cond` in lisps)
- c22 8y agoIn Python you can write it like this: if a < b: x = -1 else if a > b: x = 1 else: x = 0 Which a lot of people seem to hate for some reason, but I always appreciated the compromise between terseness and clarity.
- hawaiian 8y ago>We are not using parchment anymore, there is nothing to be gained by saving a couple of lines. Parchment, no, but do you not value vertical terseness when evaluating a function for code clarity?