6 ms·
> aversion to certain shortcuts like ternary operators. > They don't understand that unclear code is probably the number one cause of technical debt. At the s
by kbenson 10y ago
> aversion to certain shortcuts like ternary operators.
> They don't understand that unclear code is probably the number one cause of technical debt.
At the same time, verbosity can have an obfuscation quality all of its own. For simple assignment, I find a ternary operator very clear and concise, and much preferable to a 5-9 line (depending on style) if/else for a simple assignment. It also might keep you from using the single statement version of if/else if your language supports it, and that's probably justification in itself given how many problems that's caused in the past.
Specifically, I think:
usefulMetric = wantComplexCalc ? complexCalc(foo)
: simpleCalc(foo);
is preferable to:
if ( wantComplexCalc ) {
usefulMetric = complexCalc(foo)
} else {
usefulMetric = simpleCalc(foo)
}
even if only because it doesn't obscure intent with what is essentially boilerplate.
- ludicast 10y agoAgreed, plus obviously your "if" statement doesn't do the assignment to usefulMetric. One more way the ternary wins (along with functional languages that use "if"s as expressions).
- kbenson 10y agoD'oh! Fixed. Thanks. :)
- LgWoodenBadger 10y agoa "final" declaration (in Java) would have pointed that out through a compilation error ;)
- pacala 10y agoOP is about Scala, where you write directly what you mean, without special operators: val usefulMetric = if (wantComplexCalc) { complexCalc(foo) } else { simpleCalc(foo) }
- kbenson 10y agoWell, that's only "directly what you mean, without special operators" if you come from a C style procedural background and have already internalized all the special operators you've included there, such as parenthesis and braces. Sure, that's most people, but that doesn't mean they aren't operators.
- amyjess 10y agoI'd rather have Python's way of doing it, which plays with the order for the sake of readability: useful_metric = complex_calc(foo) if want_complex_calc else simple_calc(foo)
- christophilus 10y agoRuby has something similar, and I can't stand it. I think the conditional is the most important item in the phrase, and it's shoved off to the right. If you lead with the conditional, it becomes immediately apparent that the assignment is predicated on the result of a branch.
- kbenson 10y agoRuby (and Python) likely get that from Perl, which has post conditionals, but with specific qualities to prevent them from too much abuse, and which also prevents them from being used in the way presented here (which is why I didn't trot them out earlier, as much as I was tempted by the "you write what you mean" line). The limitations are that there is no else branch, and it only applies to a single statement, so you can't have a block executed with a post conditional. It leads to usage like so: die "Invalid param: please enter a positive number" unless $param1 > 0; $param2 = 0 unless defined $param2; return undef if $param1 and not $param2; my $foo = 1 if $bar; # This unfortunately creates a closure around $foo and is a big source of bugs. As much flak as Perl gets, quite a lot of thought went into making it flow similar to how people think and talk (which is no surprise if you know Larry Wall is a linguist by training). There were some missteps, but it was very early in this area, so that's expected.
- jiaweihli 10y agoUnfortunately, ternary operators eventually end up like this due to refactoring blindness: usefulMetric = wantComplexCalc ? (complexity > 40 ? superComplexCalc(foo) : regularComplexCalc(foo)) : simpleCalc(foo);
- kbenson 10y agoAt some point you have to rely on policy and not language constraints. I submit that no language is constrained enough to protect against refactoring stupidity while also being flexible enough to be useful to the average programmer on the average project. If not ternary if, it will be something else. So, do you throw out every alternative method to accomplish the same thing, or do you put policies in place to keep the code sane, such as "no chained ternary operators are allowed" ?
- jiaweihli 10y agoIn all honesty, I prefer having rules that have no special-case 'unless' issues. It's too much effort/trouble to remember all the cases where things don't work. I'm a good engineer but a terrible compiler. I believe part of learning a new library/framework/language is to limit yourself to a certain subset of the API offered. After working with Ruby (the language) and Javascript (the ecosystem), I feel like that's the only way to preserve your sanity and productivity. I don't need to know 4 different ways of creating a lambda in Ruby, selecting 1 that can express the other 4 is good enough. --- In this case, the rule would be no ternary operators, since they work well unless you nest them or unless you make them long/complicated. Other examples - You don't need to wrap if conditions unless you have a multi-line body: if (myCondition) x = 42; y = 23; Early returns simplify short circuiting logic unless your function becomes too long: if (myVariableAtBeginningOfFunction) { return true; } ... // 2 screens later ... if (x == 42) { return false; // why am I not getting false?! } Using a variable as a conditional in javascript to test against undefined works well unless the value can be falsy: if (person.isStudent) { showSchool(); } if (person.age) { showBirthCertificate(); // what if age is 0? }