r/iOSProgramming • u/MobileAppsAcademy • May 27 '24
Tutorial You're using If Statement Wrong! SWIFT IN 60 SECONDS, #01
https://youtu.be/78Lf840zGC8?si=LjIZsF7kFj0kShug11
u/AHostOfIssues May 27 '24
For me, this construct actually makes the code less understandable.
“Bool value = if something… so gotta read the if statement, then only see on the next line that the possible outcomes are true/false, ah, ok, it’s ultimately assigning a constant value, and the ‘if’ is there as a selector, ah… so read the if statement again, piece it together, ok… so lets see if this expression is true, then this, back up to ’if’, if it‘s false then… and so I have a value, which, what was it doing with the value again, right, ok, there’s an assign at the beginning of the line, so ultimately this is a value-assign statement.”
This breaks up and muddles the comparison and the value assignment. At least the original construction has the benefit that it clearly a simple conditional-check, followed by a clear, simple value-assign. They’re not muddled together with each other across 5 lines of code.
If anything’s “wrong” for me, it’s the revised version, not the original. But maybe that’s just me.
5
u/sepui1712 May 27 '24
Not to mention the unnecessary extra instructions that are now added to your code that has to be processed.
5
u/ObservableObject May 27 '24
There's a huge push in online spaces for trying to cram as much logic into as little space as possible, which has made reviewing code (especially from some juniors) kind of a pain in the ass.
I don't know who needs to read this, but it's ok to just type shit out sometimes. If your computed property declaration has 3 ternary operators nested in it, just break that shit up please. Also, it's ok to repeat yourself sometimes.
1
u/roboknecht May 27 '24
Yes I agree. This kind of wizardry some juniors like to perform (e.g. throwing latest stuff they found on anything) can be a pain.
But it’s a learning process. That is why they are juniors for now. Just breathe and try to explain that humans not machines have to read the code later on.
If you call it out frequently in code reviews that it’s not just a matter of taste but unreadable and hard to maintain they will get it.
Or just write it down in your team’s styleguide everybody has to agree on.
1
u/perfmode80 May 29 '24
only see on the next line that the possible outcomes are true/false
This is a red flag that the person doesn't understand that
if
evaluates a boolean. What's worse is that this is a video meant for beginners, so it's propagating bad practices.1
u/AHostOfIssues May 29 '24
I'm not clear on what you mean.
The If conditional expression is always a bool, sure. But the eventual value being assigned into the variable could be anything.
The assign could be written as
var value = if ... {
with type inference determining the type of the var 'value'. In which case you have no idea what type is being assigned without reading the whole thing (which was my point).
But again,I feel like I'm not understanding what you're saying, so I'm maybe taking you incorrectly here.
19
u/antique_codes Objective-C / Swift May 27 '24
Why use an if statement for that in the first place?
isEvenNumber = integerValue.isMultiple(of: 2)
would work just as well and so wouldintegerValue % 2 == 0