Unravelling 'if' statements
snarky.ca
snarky.ca
fn main() {
let () = return ();
}[1] https://esolangs.org/wiki/Brainfuck_algorithms#if_.28x.29_.7...
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.
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.
> Yes.
Not sure what I expected, I'll turn in my resignation effective immediately.
Even 'if' / 'while' / etc. are syntactic sugar for 'goto'.
Technically not true, a Turing machine in the original sense only moves its head one position at a time (and I can imagine some early drum memory CPUs might have operated like that in real life). Of course one can emulate a processor that has jumps on a machine like that.
> Even 'if' / 'while' / etc. are syntactic sugar for 'goto'.
Structured programming is not just syntactic sugar; it gives you important properties that you can't get from using goto directly, hence why so many languages discourage or ban goto.
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.
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.
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'.
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
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 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...
Guard clauses are a good thing and you should use them.
goto's are good because the label documents why the path was taken.
There are two kinds of programmers, those that love programing as math and people that want to get shit done.