• Tinkle (unregistered)

    So, they know that k gets clobbered, but still use it to control the loop? Sounds safe...

    This could have been avoided by using meaningful variable names and/or not using magic variables in macros.

  • Bob (unregistered)

    Is it just me or, while the "l" ensures the output numbering is sequential, it will still not properly iterate through the customers... Hopefully they use the "l" rather than the "k" as index, but at the very least they cannot control the duration of the loop, it might stop too soon, or worse, go on too long. I feel the really critical part was not shown.

  • (nodebb)

    If you believe there to be a "memory violation" in the code, shouldn't you be fixing it before going any further? I guess not, it's not like it could cause serious security issues or something.

  • MaryD (unregistered)

    I used to use "ii" as the first loop control variable. Then "jj", then "kk". If the flow went deeper than that it probably needed refactoring. You can search a file for "ii" but searching for "i" isn't helpful. (Days before automated development environments - using whatever text editor was available.)

  • Snarkenstein (unregistered)

    My go-to loop integers: ix, jx, kx (for index)

  • PlasticHound (unregistered)

    I think even using a loop local for (int k = 0; ...) would generate enough noise from the compiler as this would result in the outer k being shadowed by the inner one. If -Wshadow or -Wshadow=local is used.

    And if -Werror is on, then the compiler grinds to a halt.

    Of course, those ifs are working hard.

  • Oracles (unregistered)

    It's such a shame that no one has developed a language in which you could use more than one character to name a variable.

  • Mr Bits (unregistered) in reply to PlasticHound

    In codebases like this, those compiler flags are such a nuisance that the developers just turn them off.

  • (nodebb) in reply to MaryD

    It's really not that much extra typing to have useful loop index names, e.g. "isize" " jrow" , "kcolumn" etc.

  • (nodebb) in reply to cellocgw

    "It's really not that much extra typing..."

    Bless you. Variable names like I, J, k, kk, kkkkkk are automatic PR comments for me.

  • Hmmmm 🤨 (unregistered)

    The big fun would be sifting through the loop to find that customers is also getting clobbered!

  • A Human (unregistered)

    I prefer calling the loop variable "index".

    Or, you know, using iterators for what they were made for, instead of abusing a bloody for loop to iterate over an array like a madman.

  • 516052 (unregistered) in reply to A Human

    Indices and iterators are two separate mechanisms that are used for different use cases. There are legitimate uses for both and times when you want one over the other.

    As for variable names I explicitly prefer i, j, k etc. because that immediately screams that said variable is something that you are only supposed to use as the index operator and NOT modify. If the variable had a smart looking name like say "selected_item" or "current_picture" or whatever some clever idiot down the line (likely me with my luck) might look at that and go "oh look". With i you know what it is.

    But that's entirely my personal code convention and has no bearing on objective reality.

  • Neveranull (unregistered)

    The real problem is using “k” as anything other than a loop index. “I,” “j,” and “k” should only ever be used for loop indexes, to avoid being reassigned elsewhere.

  • to (unregistered)

    The only proper loop counter name is "iWhateverYoureCounting". There.

  • A Human (unregistered) in reply to 516052

    As for variable names I explicitly prefer i, j, k etc. because that immediately screams that said variable is something that you are only supposed to use as the index operator and NOT modify.

    It's 2026. Why on earth are you using a variable name to convey that the variable should not be mutable, instead of using the type system to ensure you cannot modify it?

    Oh right. Because for loops work by incrementing that variable after each iteration, so it has to be mutable. How on earth are these stupid things not deprecated?

    Maybe because for loops are easy to fix: just replace the c-style (declaration; condition; increment) with a single declaration: (const item in array). Not only is this simpler, it's more explicit about what we are looping over (especially in c where there's no index < array.len to infer the array from).
    But how are we going to do ranges like (int index = 0; index > 6; i++) then? Well considering we don't care about the entire array, just one item and then the next item, let's make it accept any type with a next method. What should we call this interface? How about Iterator ❨Unless you're Java, that is❩!

  • enkorvaks (unregistered) in reply to A Human

    But how are we going to do ranges like (int index = 0; index > 6; i++) then? Well considering we don't care about the entire array, just one item and then the next item

    Why the assumption that a for-loop is only for iterating over arrays (or parts of them)? There are other reasons for "do this a fixed number of times", and in some cases, you still need to know the value of the iterator/count variable. Naive versions of 'fizz-buzz' come to mind, but there are others.

    Besides which, being able to modify the index value of a for-loop (along with switch/case) allows you to design a finite state machine that has a 'normal' sequence and the ability to change the next state based on whatever conditions are required. Of course, most uses of the 'for-switch' are stupid, and can have both the 'for' and the 'switch/case' removed, but the point remains that it is actually occasionally useful.

  • 516052 (unregistered) in reply to A Human

    And than you want to mutate the collection while iterating over it and iterators don't work any more.
    They are different tools for different jobs.

  • A Human (unregistered)

    Why the assumption that a for-loop is only for iterating over arrays (or parts of them)?

    Re-read the last four sentences of what I wrote. The whole point of an Iterator type is that one can use it for more than just arrays.

    Besides which, being able to modify the index value of a for-loop (along with switch/case) allows you to design a finite state machine that has a 'normal' sequence and the ability to change the next state based on whatever conditions are required.

    That's terrifying. Purposely modifying the index variable so that it doesn't always follow the pattern defined by the increment block? That is TRWTF. Please find a more normal way to write such code.

    And than you want to mutate the collection while iterating over it and iterators don't work any more.

    Variable mutation is impure and therefore evil. All variables in a program should be immutable.

  • enkorvaks (unregistered) in reply to A Human

    Variable mutation is impure and therefore evil. All variables in a program should be immutable.

    Choose a different programming language. I recommend LISP, Scheme, or Haskell. Most functional programming languages expect variables to be of the mathematical sense (that is, they don't actually vary), and almost everything is immutable once declared.

    That's terrifying. Purposely modifying the index variable so that it doesn't always follow the pattern defined by the increment block? That is TRWTF. Please find a more normal way to write such code.

    That is the normal way to write such code. For a finite state machine, a loop with a switch is the simplest way of doing it. Most people don't need to write finite state machines, though.

    As for an example where an Iterator is of limited (if any) use: How would you calculate the 11th power of 23, without resorting to a math-library, if you can't use a bare for-loop (using a while-loop with an incrementing counter counts as using a for-loop)? With a for-loop, it is this:

    int result = 1;
    int base = 23;
    int exponent = 11;
    for (int i = 0; i < exponent ; i++) {
      result *= base;
    }
    return result;
    
  • A Human (unregistered) in reply to enkorvaks

    Choose a different programming language.

    I know I need to, but all languages suck. C is simple, but has no built-in support for closures. C++ and rust are both stupendously bloated. Python has one of the worst syntaxes ever. Javascript is, well, javascript. Haskell's compiler is a joke. Java is a nice language, but its ecosystem is crap. Gleam and C3 are pretty nice, but C3's low-level simplicity can be a weakness in addition to a strength, and gleam has no metaprogramming.
    I probably just need to make my own language.

    That is the normal way to write such code.

    Well then I guess TRWTF is that enough programmers think that that is a good way to program. Not super surprising given the code I see at work and on this website, but still worrying.

    Why not instead represent all the possible states in an enum, implement them as functions that return the next state, and then just use a while loop, for example:

    current_state = INITIAL_STATE
    while (true) {
        current_state = get_function_for_state(current_state)()
    }
    

    How would you calculate the 11th power of 23, without resorting to a math-library?

    There is no reason to not resort to a maths library. Even the C standard library has a power function.
    Unless one is writing embedded C with no standard library.

    If you want to know how I'd implement an exponentiation function despite there being almost no reason to, then I agree iterators are completely useless here. Most forms of repetition in a program are either iterating over a sequence (which iterators are great for) or until a condition is met (which while loops are great for), but the rest can be handled with recursion, for example:

    integer power(integer base, integer exponent) {
        return switch (exponent) {
            case x if (x < 0) -> todo("negative exponents"),
            case 0 -> 1,
            case x -> power(base, exponent - 1) * base
        }
    }
    
  • 516052 (unregistered) in reply to A Human

    All variables in a program should be immutable.

    Yea, I think we are done here. At this rate you are going to claim calculations are bad too.
    I suppose if your entire world is just wordpress sites and hello world you really don't need advanced features.

  • A Human (unregistered) in reply to 516052

    ...if your entire world is just wordpress sites...

    Close. I'm a liquid developer.

    If you haven't heard of liquid, you're very lucky. I've tried a lot of html templating engines and it is by far the worst one out there.
    If you haven't tried it, don't.

    It has one possible error message it can output: "Liquid error: internal". That's all it gives you; no line numbers. It's like a segfault, but because it's usually used in shitty saas products, you can't use a debugger or access any core dump, and syntax errors can trigger it too!
    Not that the specific shitty saas product I support helps the issue by using a version of safari from 2012 with js disabled to convert html to pdf.

  • A Human (unregistered) in reply to A Human

    At work that is. At home I write random things, like web servers, file formats and their parsers, js frameworks, and heaps of bash to glue all that together.

    So yeah, I have to (unfortunately) write code that actually does something instead of being pure >:(

  • 516052 (unregistered) in reply to A Human

    My sympathies.

Leave a comment on “Oh, K?”

Log In or post as a guest

Replying to comment #705941:

« Return to Article