Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Thing is, I always found this shit ultimately stupid (unless some corner cases related to multithreading with have better solutions anyway).
Why would you branch on an assignment?
Just fucking branch on the RHS to begin with...
Locality of the variable? Maaaaaaybe. But I still think branching on an assignment is a terrible code smell.
Fucking branch on y and operate on y. Unless y is a reference type (in which case equality is always false (unless it's the same object which has its use cases but most likely is a debauchery) or custom defined).
In C it's not poor style, it's downright dangerous. Dunno about swift, but from what I've heard, shouldn't be a problem, but it's still a poor choice. -
lorentz1565855mI don't see how this is worse than the C version. I don't know swift specifically, but I would assume that optional is either a tagged union or a nullable reference, both cases should result in exactly the same optimized machine code as the corresponding C technique for value and reference types respectively.
-
lorentz1565850mI also don't see why either is bad form. They hide a minuscule amount of behaviour, much less than is lost or gained from arbitrary reordering of non-inlined calls to functions that use different registers, they don't hide any usercode dispatch, they branch on the structure of a container. You can also use the C construct incorrectly like everything in C but its correct use involves a trivial datastructure containing either a 1-based ID such as a pointer or nothing
-
atheist1085144m@CoreFusionX I think it's pretty nice to be able to assign in a loop, I kinda see the point with conditional but less so.
-
lorentz1565830mBooleans are exactly as real as optionals too. The dumb tagged union implementation of an optional is literally a boolean, and the nullable reference implementation is created the same way a boolean is created, by assigning one meaning to one subset of the possible values of a number, and a different meaning to another. The main difference is that optionals are a distinct concept so the compiler can choose even more efficient implementations for special cases, such as by niche elimination.
Related Rants
In my latest installment of "Swift, WTF?", we look at the "if" conditional in terms of the Swift convention of:
if let x = y { /* ... */ }
so what this does :
1. declares x in the scope of the braced code
2. sets x to y (an ahem, "optional")
3. decides if x is not *nil*, then executes the braced code.
This is very similar in both the visual and the operation to the C code of:
if (int x = y) { /* ... */ }
1. declares x in the scope of the braced code
2. sets x to the value of y
3. if x is not zero, then executes the braced code
which is considered *exceptionally* poor style.
Neither the C nor the Swift construct result in a legitimate boolean value of "true" or "false", although C comes closer than Swift.
In the Swift case the *imaginary* "nil" value has to be interpreted as "false" and thus there must be extra code is for the conditional to check on whatever constitutes the **actual** value of nil in Swift and then set the condition to "false".
(remember boys and girls, "optionals" are not real, they are an imaginary language construct of Swift and have no legitimate counterpart in the CPU operations with memory and registers)
At least in the case of C, if the value of x is zero or NULL (which is 0) then it is technically a "false" which in C is 0. Regardless, it is really poor programming and anyone doing that on my team gets an ear full.
But in Swift this obfuscation of code is common and condoned! Well, why not put more of the program in the condition of the if? In fact, stuff the whole thing in there.. why not? 🙄
This just reenforces my opinion that Swift is not a bird but the stuff that comes out of the underside of the bird. 🐦💩
devrant
swift