- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
while (true) frist();
Admin
Personally i prefer the "for every" and "do until" loops.
Admin
It's harder to read and maintain, but I wouldn't call it a full-blown WTF.
[« The Master is Simplicity] As for me, I have grown habits of what loop structures to use when and how:
while-x
,do-until-x
), replace with infinite loops (while-true
) with conditional jumps (if-x-break/continue
). It's easier to grasp and gives greater flexibility, without making it painful similar to a bunch of GOTOs.for-i
loop headers in the formfor(int i = 0; i <= length; i++)
(for incrementinglength
times) andfor(int i = length-1; i >= 0; i--)
(decrementinglength
times). Declaration may reside outside the header, but condition and step parts are mostly fixed, everything can be done via index transformation (meaning we calculate different indeces with offset, scale, etc respective toi
... something along the lines ofint bodyIndex = i*2; int footerIndex = i*2+1;
ordouble radiants = 2*PI*(double)i / 360.0;
). -foreach
type loops can differ a lot from language to language. In swift for example there is EnumeratedSequence (https://developer.apple.com/documentation/swift/enumeratedsequence), which essentially gives you the index while looping on collection elements. Without that comfort when dealing with arrays/lists, it's bit painful to "downgrade" aforeach
to afor-i
with element handle. When dealing with non-linear collection types it can be even more work depending on what you're dealing with.Admin
If that's the argument then lets get rid of ALL means of wheeled transportation and just have a Ford Fiesta! They all serve different purposes and you're a bad developer if you don't use them appropriately as they give hints to the next guy....
for - non-object based fixed number of loops (e.g indexing) foreach - object based loops (e.g. for each record) while - you don't know how many times you're going to loop do - you don't know how many times but you're going to do it at least once goto 10: .... give up being a developer and go back to school
Personally I don't like the "do while" loop in C#. Much prefer Wirth's "Repeat Until" of Pascal/Modula-2 as it seems ugly reusing the "While" keyword for the beginning and the end
Admin
Don't do... while not
Still one of my favorites.
Admin
The original programmer just split apart the syntactic sugar of the for loop into what it gets compiled to anyway. The were just helping the compiler by doing that bit of work for it.
Admin
Other than a the unneeded If statement there's nothing wrong with this.
Admin
Hey, what's wrong with WATFOR? https://www.encyclopedia.com/computing/dictionaries-thesauruses-pictures-and-press-releases/watfor
Addendum 2019-05-09 14:27: Should'a written "Hey, WATTs wrong..."
Admin
I am probably guilty of things like as result of refactoring. Although the lines about it being a repeated pattern thru the code base are bit disturbing. I am pretty sure I have moved something that were counted loops, with some additional condition to places where that condition is already implict removed it from the loop test but not switched from while to for..
ie int i = 0; while ((i < 500) && (otherThing == true)) { ... ; i++;}
sure I suppose one could have written
for (int i = 0; ((i < 500) && (otherThing == true) ; i++) {...}
in the first place but that seem wrong to me because reading code quickly I would naturally assume for is a simple counted loop or one with break condition in it possibly but would not be studying the conditional expression carefully.
Admin
Until some Collections implementer thinks it is a good idea to return -1 instead of throwing an Exceptionoid when calling count() on a null or non-array parameter.
Strong typing is for people with weak memories...
Admin
| Hey, what's wrong with WATFOR?
My favorite book title was "FORTRAN IV With WATFOR and WATFIV" , which could recursively be parsed into WATFOUR and Watf_IV (Roman numeral 4).
Admin
When are our British friends going to chime in and insist on a "whilst" loop?
Admin
How about a While-That-For loop?
Admin
This will be a feature in Java 21.
Admin
Believe it or not I have written production code in RATFIV. In a rocket motor factory, no less.
Admin
There's plenty wrong with it. They took a for loop and made it less readable, more lines, and more error prone.
I"m not up on exact syntax for this language, but using C syntax you can replace it with
for( $checked = 0; $checked < $searchCount; $checked++ ) { // do your stuff }
Or even better, if the language allows foreach: foreach( $thisOne in $searchArray ) { // stuff }
The worst thing is that $checked++ at the end because someone will inevitably forget it, hello infinite loop.
Admin
They're not our friends, guy.
https://thedailywtf.com/articles/Elegant-Syntax-Error
Admin
@Sizer9: "The worst thing is that $checked++ at the end because someone will inevitably forget it, hello infinite loop."
Or include it in one branch of an if...else structure.
Sometimes I wonder if a programmer just learned his trade by imitating code snippets, and when he saw something he couldn't understand just never used it instead of looking it up. E.g., the c for statement is not obvious to someone coming from Basic, Pascal, and many other languages that use something like "for i = 1 to n step m". It takes only 5 minutes in the manual to learn to code this as "for(i=1; i<=n; m)", but first you have to RTFM.
What took much longer for me was learning to make most for loops start with 0. There may still be code out there, which I long ago translated to Turbo c from another language, that leaves the first element of an array unused (e.g. "a[0]") rather than changing every array reference to 0-based.
Admin
TRWTF is the "optimization" at the very beginning: "if ($searchCount > 0) ..."
This one is completely unnecessary, adds one more level of indentation, and finally slows down the common case (non-empty result) in favour of the uncommon case (empty result) which is the fastest code path anyway.
Admin
TRWTF is the "optimization" at the very beginning: "if ($searchCount > 0) ..."
This one is completely unnecessary, adds one more level of indentation, and finally slows down the common case (non-empty result) in favour of the uncommon case (empty result) which is the fastest code path anyway.
Admin
At my old job we had to use a scriptlanguage (built on PASCAL, but as far as I know it was only meant to be a language to teach the basics of programming. Not to be used in a production environment), which had nothing but WHILE loops. No "do-while", no "for", and don't even ask about "foreach". It all had to be done with WHILE loops.