7 ms·
Unravelling 'if' statements
- gumby 4y agoThe real brain damage in most programming syntax is the distinction between expressions and statements. Expressions are all you need.
- ChadNauseam 4y agoMy favorite thing about rust is how it encourages you to keep `return` at the end of your function. `return` is one of the most annoying syntax elements to me because you mostly don't need it, and it kind of has "side effects" in a way that's not obviously visible in the structure of the function.
- throwaway2j94 4y agoThey are expressions in rust fn main() { let () = return (); }
- moffkalast 4y agoSeems weird to replace an if sentence with a while, since a while is just an if with two gotos isn't it?
- twawaaay 4y agoFor some strange reason there exists a subpopulation of developers who hate goto/if/switch etc. statements and feel compulsion to replace it with an equivalent solution that does not use goto/if/switch even if this is done at the cost of readability. 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.
- throwaway2j94 4y agogoto/if/switch have their uses (especially in C) but I consider them low level and deprecated in favor of pattern matching, expression-based loops, and fmap / map/reduce combinators.
- twawaaay 4y agoOf course, I do prefer higher level constructs. Even then goto/if/switch are effective, readable solution especially if you use them within idioms. 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.
- ok_dad 4y ago> 2022: "If/else considered dangerous." 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.
- moffkalast 4y agoNah, if I understand the guy correctly it's not about the low level stuff. That's hardly going to change any time soon. 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?
- anikom15 4y agoAt least with the while version I have complete control over the loop. With the async version I have to figure out what the timing model is by reading documentation and assume it’s accurate. Async can make things easier but it’s not going to magically make code more maintainable. It’s hard to debug async problems. With a loop I can debug it on a sheet of paper. 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?
- faho 4y agoThe idea behind this series of posts is to see how much of python, concretely, is syntactic sugar and how much is necessary. How much could you do with e.g. doing macro transformation on top of a minimal core? 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.
- moffkalast 4y agoTbf python isn't exactly a language that's high in sugar to start with, it doesn't even have switch statements. Not sure why you'd want to take away even more until there's nothing usable left. People have the weirdest hobbies hah
- milliams 4y agoNo, but it has the `match` statement which is even better: https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching https://docs.python.org/3/whatsnew/3.10.html#pep-634-structu...
- moffkalast 4y agoI wouldn't exactly call something only available in 3.10 as something python "has". Most of us haven't moved past like 3.8 at best, some still using 2.7 for legacy stuff even.
- rgmerk 4y agoWaiting for the solution that involves transforming Python code into an encoded Turing machine and implements a minimal UTM in some tiny subset of Python.
- benj111 4y ago2 gotos? While: If X Stuff Goto while. //Outside of while statement
- deleted 4y ago[deleted]
- Jtsummers 4y agoThat actually shows two gotos (as you've written it) because the if is itself a goto, under the hood. The way to get it down to one is this pseudo-code: 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.
- benj111 4y agoTrue, but The parents comment >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.
- gpderetta 4y agoI would call an unconditional jump inherently different from a conditional one, i.e. both if and goto are primitives. 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...
- itsnickjames 4y agoI can barely manage to keep nested if's straight... Trying to make sense of that is giving me a headache haha. I'm becoming a proponent of guard clauses, but its still a shift in thinking as it is. (also, first post here.)
- Gibbon1 4y agoNested if's are a sign the code is bad. 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.
- lifthrasiir 4y agoYou have just discovered Brainf*ck, where every control flow is implemented via while. `if (current cell) { ... } else { ... }` for example would have to be implemented like `temp = 1; while (current) { ...; temp = 0; current = 0; } while (temp) { ...; temp = 0; current = 0; }` (there are many other approaches [1]). [1] https://esolangs.org/wiki/Brainfuck_algorithms#if_.28x.29_.7B_code1_.7D_else_.7B_code2_.7D https://esolangs.org/wiki/Brainfuck_algorithms#if_.28x.29_.7...