4 ms·
> 7. There’s no ternary (?:) operator. I didn't realise there are people who prefer this thing. Generally speaking, what should we think about languages that
by vesak 9y ago
> 7. There’s no ternary (?:) operator.
I didn't realise there are people who prefer this thing.
Generally speaking, what should we think about languages that are theoretically weak but are quite successful? Does that imply that the theories are wrong?
- pdpi 9y agoIf you come from a functional background (or otherwise just prefer expression-based languages), the if statement-based version is just horrible. Several languages work around this by not having ?: but instead having `if` be an expression.
- dingo_bat 9y agoTernary operators are the best! Highly expressive and concise, and everybody instantly understands it. Is there a single drawback about it?
- sethammons 9y agoNested ternary
- masklinn 9y agoEven more so. Between var v T if (cond0) { v = value0 } else if (cond1) { v = value1 } else if (cond2) { v = value2 } else { v = default } and val v T = cond0 ? value0 : cond1 ? value1 : cond2 ? value2 : default I'd much rather have the latter.
- randomdata 9y agoHowever, your if statement is unidiomatic and quite unlike how you would normally write that particular logic in Go. If you really want to make a fair comparison, you might consider writing it in the way that Go is normally written, not the way that someone familiar with another C-style language might write the code if the ternary operator was taken from them.
- masklinn 9y agoPlease do go ahead and demonstrate how you would normally write that particular logic in Go. Would it perchance be this piece of finery? var v T switch { case cond0: v = value0 case cond1: v = value1 case cond2: v = value2 default: v = default }
- baby 9y agoI think GP pointed out the if () construction that doesn't make sense in Go. There are no parenthesis in Go. Shows that you pretty much never used it?
- masklinn 9y agoYes I'm sure > However, your if statement is unidiomatic and quite unlike how you would normally write that particular logic in Go. If you really want to make a fair comparison, you might consider writing it in the way that Go is normally written, not the way that someone familiar with another C-style language might write the code if the ternary operator was taken from them. meant > the parens are unnecessary and they didn't just write that because reasons, which would also be the why they commented an other half dozen times since then but just couldn't come around to showing "how you would normally write that particular logic in Go" because it would take all of 5 minutes.
- baby 9y agoMaybe because you're used to it, but your second example looks unreadable to me and I know ternaries.
- citrin_ru 9y agoTernary operators can be abused. E. g. somebody can write code with nested ternary operator and long expressions. If/else will be more readable in this case. Go is a nanny language - Go authors give to coders on Go as little foredoom as possible.
- drdaeman 9y agoAnything can be abused. There is no technological or design measure that can prevent one from writing crappy code in Go or any other language. I'm certain of this as I had personally written some. Except for esoteric languages, of course. I've yet to see someone claiming some code in Malbodge is crappy and offering a refactored version. ;) Just kidding.
- code_sloth 9y agoHave you seen what atrocities people can do with nested ternaries?
- masklinn 9y agoThe only language in which you can't see atrocities is whitespace, because you can't see the atrocities.
- bluedino 9y agoThey're easily abused: H = (C == 0 ? null : V == r ? (g - b) / C : V == g ? (b - r) / C + 2 : (r - b) / C + 4);
- deleted 9y ago[deleted]
- vesak 9y agoI've been programming in languages that have them, and almost every time I have to read code that has a ternary operator, I have to stop and think what's the condition, what's the "true" branch and what's the "false" branch. This is even worse in languages that have truthy values, since you don't even have the "== True" thing to help.
- andyfleming 9y agoThe traditional way is more readable, I agree. I think it has more to do with statefulness. let example if (someCondition) { example = 3 } else { example = 4 } In javascript, for example, like above, I'm initializing a variable (with let, allowing reassign-ability). Its value can be modified later by other code. Alternatively, I could use a ternary operator and a const to avoid potential unwanted reassignments. const example = (someCondition) ? 3 : 4 ------------ Some languages, like Kotlin, actually let you use an if-else expression in a single line... val max = if (a > b) a else b ... or even allow you to return the last line of a block to assign a value: val max = if (a > b) { print("Choose a") a } else { print("Choose b") b } *val, in Kotlin, is similar to const in javascript; it's not reassignable.
- tschellenbach 9y agoYes I don't get the authors argument. Now you need 4 lines of code instead of 1 in some cases. On the flip side it's easier to read for anyone looking at your code. The weirdest part is that the author seems to prefer Java. Which is one of the more verbose languages out there.
- vec 9y agoThe problem is that I can't declare the variable and set it's value as part of the same operation. I'm forced to declare a variable with a nonsensical or incorrect value then correct it later as part of a logically distinct statement. This introduces a window into my code where some piece of memory is known to be wrong and puts the onus on me to not use it between its declaration and it's being set. It's a small nitpick in the grand scheme of things, but it forces me to allow a class of user error (and, in some cases, concurrency issues) that other styles of programming make impossible. And also it usually adds to the line count, which is annoying.
- randomdata 9y ago> The problem is that I can't declare the variable and set it's value as part of the same operation. Care to elaborate? Consider the following: var foo = func() struct{ Bar int } { if os.Getenv("BAR") == "1" { return struct{ Bar int }{1} } return struct{ Bar int }{2} }() Is there a case where the foo variable can be assigned to something other than one of the two conditional-dependent values?
- vec 9y agoOkay, "can't" is too strong a word. But I don't know a good word for "it's technically possible but is cumbersome, is not idiomatic, and requires introducing another stack frame for no good reason".
- randomdata 9y ago> and requires introducing another stack frame for no good reason With a given implementation or is there something in the Go specification that prevents this from being compiled away? If I were to write something similar in, say, C++ with a good compiler I would expect the generated code to be equivalent to using a ternary operator.