by twawaaay · 4y ago · 0 comments
They red somewhere that goto/if/switch is bad and now mistakenly think if they only get rid of it their code will somehow be better. Which is sad and led to a lot of wasted time on my part trying to untangle their mess.
I understand the problems with `goto`, but I'd never heard a case against `if` before.
I had a colleague once that would turn all his "if" statements into "switch" clauses because "no branches means it is so much faster" and people listened to him closely.
"i-am-an-expert" syndrome often seen in 24-yo "senior" developers with sizeable egos.
If people are removing if statements at the cost of readability, I think they're probably bad candidates for replacement. The reason for removing if statements is (especially when deeply nested), they're very hard to reason about and test. Untangling these things (e.g., polymorphism) is intended to improve readability / understandability / ability to be changed. Everything in software engineering is a compromise—sometimes a simple if statement is a lot clearer than introducing polymorphism unnecessarily. Each case will vary.
do {
SomeConnection *conn = conn_open(...);
if (!conn) break;
do {
SomeSession *ses = sess_open(conn, ...);
if (!ses) break;
do {
// ...
} while(false);
ses_close(ses);
} while(false);
conn_close(f);
} while(false);
The idea was apparently to use "break" instead of "goto fail" because goto is bad.P.S. Also, why is "connection" abbreviated as "conn", but "session" as "ses"?
The problem is when people start replacing low level statements with other low level equivalents. Or low level equivalents that are masquerading as high level. Which means they did not understand why you may want to get rid of the low level construct and only caused loss of readability for no structural benefit.
I never thought this would happen, does this mean I should quit programming? I feel like you can't get much done in a computer without branching in a CPU, but maybe I misunderstand modern CPU architecture.
But in terms of a logic development perspective I guess it may be clearer to set up something like instead of your average while(true) if (boolean) callback_function() put an async on(boolean,callback_function) or something and let the language deal with the low level implementation of it. Not sure why it would be "dangerous" to use the explicit definition, but we can maybe do a bit better in terms of clarity by removing the optimizable boilerplate?
Choose abstractions and higher-order expressions when it makes sense to do so, not just to avoid low-level or ‘deprecated’ ones. I believe arithmetic ifs are higher-level than boolean ifs from the computer’s perspective, but you wouldn’t prefer that over a boolean if, would you?
Yes.
> I feel like you can't get much done in a computer without branching in a CPU, but maybe I misunderstand modern CPU architecture.
Most CPUs have jump instructions, that doesn't mean it's a good idea to have gotos in your programming language.
Even 'if' / 'while' / etc. are syntactic sugar for 'goto'.
> Yes.
Not sure what I expected, I'll turn in my resignation effective immediately.
fortunately I've went into real world code bases that saved me from those ivory tower ideas like prefering OOP approaches over control flow instructions just because
But just hiding if statements and loops under some high level functional mumbo jumbo just makes the code harder to understand for humans, and for the compiler harder to remove all the layers of 'expressiveness' and 'abstractions'.