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?
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?
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.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. var v T
switch {
case cond0:
v = value0
case cond1:
v = value1
case cond2:
v = value2
default:
v = default
}> 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.
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.
H = (C == 0 ? null : V == r ? (g - b) / C : V == g ? (b - r) / C + 2 : (r - b) / C + 4);This is even worse in languages that have truthy values, since you don't even have the "== True" thing to help.
The weirdest part is that the author seems to prefer Java. Which is one of the more verbose languages out there.
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.
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?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.
my_variable = (my_very_long_condition)
? value_if_my_condition_is_true
: value_if_my_condition_is_false;
This is 3 lines, but there is almost no noise, just the condition and alternate values. Also, the variable is never assigned a dummy value (or left unassigned).