• eth0 (unregistered)

    while (true) frist();

  • (nodebb)

    Personally i prefer the "for every" and "do until" loops.

  • post as a guest (unregistered)

    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:

    • Don't use head/tail controlled loops (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.
    • Only use for-i loop headers in the form for(int i = 0; i <= length; i++) (for incrementing length times) and for(int i = length-1; i >= 0; i--) (decrementing length 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 to i ... something along the lines of int bodyIndex = i*2; int footerIndex = i*2+1; or double 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" a foreach to a for-i with element handle. When dealing with non-linear collection types it can be even more work depending on what you're dealing with.
  • Wizard (unregistered)

    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

  • Rene (unregistered)

    Don't do... while not

    Still one of my favorites.

  • (nodebb)

    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.

  • ZZartin (unregistered)

    Other than a the unneeded If statement there's nothing wrong with this.

  • (nodebb)

    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..."

  • James (unregistered)

    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.

  • (nodebb) in reply to ZZartin

    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...

  • LCrawford (unregistered)

    | 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).

  • Argle (unregistered)

    When are our British friends going to chime in and insist on a "whilst" loop?

  • Kayser (unregistered)

    How about a While-That-For loop?

  • (nodebb) in reply to Kayser

    How about a While-That-For loop?

    This will be a feature in Java 21.

  • medievalist (unregistered) in reply to LCrawford

    Believe it or not I have written production code in RATFIV. In a rocket motor factory, no less.

  • sizer99 (google) in reply to ZZartin

    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.

  • Ulysses (unregistered) in reply to Argle

    When are our British friends going to chime in and insist on a "whilst" loop?

    They're not our friends, guy.

    https://thedailywtf.com/articles/Elegant-Syntax-Error

  • markm (unregistered)

    @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.

  • Atomizer (unregistered)

    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.

  • Atomizer (unregistered)

    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.

Leave a comment on “What For?”

Log In or post as a guest

Replying to comment #:

« Return to Article