by moffkalast · 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.
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?
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.
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'.
The last post before this was titled: "MVPy: Minimum Viable Python".
So, since python doesn't have goto, you can't replace while with an if with two gotos (without adding a goto to the language).
You can replace an `if` with a while tho, easily.
While:
If X
Stuff
Goto while.
//Outside of while statement jump test
loop:
stuff
test:
test X
jump if (not-equal/greater-than/less-than/etc) loop
other stuff
It still uses two jump or branch instructions, but the loop body only uses one. It falls through if the test fails. In the case of a do/while it's the same except you remove the initial jump.>since a while is just an if with two gotos isn't it?
The if has to include at least one branch instruction so you already have 2 branches to play around with.
Plus python doesn't have a goto so my code is pseudo code so I don't think that you can declare it is a goto under the hood.
Of course on some machines everything can be predicated and the line between an if and a goto is blurred.
Then there are indirect jumps...