0

It is common to see programmers follow tacit style guidelines when writing out programs. For instance, in most languages I have dealt with, we invariably always write if x < 5 instead of if 5 > x although both are permissible expressions by the underlying grammar.

Does anyone have suggestions for what could have caused these biases to be picked up by us when we write these expressions?

Some thoughts on possible reasons -

  • It could have been a constraint posed in the grammar of an early programming language like Scheme, Algol, or even Assembly?
  • It could have been a rule enforced by some early style-checkers?
  • Any other?

    Would be great if anyone can share

  • insights tying such preferences to practices from early days of programming, or even academic references discussing such preferences.
  • help provide more examples of such preferences which they may subscribe to/have encountered.
  • Shash
    • 83
    • 7
    • 1
      Much of it comes from traditional mathematical notation -- `x < 5` vs `5 > x` goes back to Isaac Newton, if not before. FORTRAN (the original high-level language) is a shortening of "Formula Translation" -- a way of automatically translating mathematical formulas to machine code. – Chris Dodd Apr 25 '20 at 19:24
    • 1
      I think the comparison preference comes from natural language. In both the languages I use daily (tbh both Indo-European so there might be some cultural bias) you can only express the comparison in one direction. "These are the questions with more than five votes" "The temperature is below 10°". You would never say "these are the questions where five is less than the number of votes" or "10° is above the temperature". (Although those utterances are "grammatical".) – rici Apr 25 '20 at 20:09

    1 Answers1

    0

    I'm almost certainly checking the value of x, not checking the value of 5. My thought process is therefore going to be "if x is greater than 5 ...".

    And therefore I write if x > 5 and not if 5 < x.

    In short, I write the way I think.

    user13784117
    • 1,124
    • 4
    • 4