Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
mastermind ( mastermind@lemm.ee )  to Programmer Humor@lemmy.ml · 3 years ago

why not a,b or x,y?

lemm.ee

message-square
46
link
fedilink
336

why not a,b or x,y?

lemm.ee

mastermind ( mastermind@lemm.ee )  to Programmer Humor@lemmy.ml · 3 years ago
message-square
46
link
fedilink
alert-triangle
You must log in or # to comment.
  • Poob ( Poob@lemmy.ca ) 
    link
    fedilink
    arrow-up
    30
    ·
    3 years ago

    i is for index. j is simply the next letter and we’re too lazy to think up something meaningful

    • PM_ME_VINTAGE_30S [he/him] ( PM_ME_VINTAGE_30S@lemmy.sdf.org ) 
      link
      fedilink
      arrow-up
      9
      ·
      3 years ago

      jndex

      • Gork ( Gork@beehaw.org ) 
        link
        fedilink
        arrow-up
        2
        ·
        3 years ago

        kndex

        • CoffeeDev ( CoffeeDev@lemmy.studio ) 
          link
          fedilink
          arrow-up
          1
          ·
          3 years ago

          lndex

    • DraughtGlobe ( DraughtGlobe@feddit.nl ) 
      link
      fedilink
      arrow-up
      6
      ·
      3 years ago

      I always thought it stood for iterator

    • Chahk ( chahk@beehaw.org ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago

      i2?

      • piotrek416 ( piotrek416@szmer.info ) 
        link
        fedilink
        arrow-up
        3
        ·
        3 years ago

        ii

  • StudioLE ( StudioLE@programming.dev ) 
    link
    fedilink
    arrow-up
    18
    ·
    3 years ago

    A useful tip I picked up was to use ii instead of j for an inner loop. It’s far more distinct than j.

    If for some terrible reason you have even more inner loops you can easily continue the trend i, ii, iii, iiii, iiiii - or iv, v if you’re feeling roman

    • hstde ( hstde@lemmy.fmhy.ml ) 
      link
      fedilink
      arrow-up
      17
      ·
      3 years ago

      If you have the need to nest 5 levels of for-loops, I suggest taking a step back and rethinking your approach, my friend.

      Even if that other approach is just refactoring it into separate methods.

    • biscuit ( biscuit@lemdro.id ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago

      I just do i2, i3, etc

    • exu ( exu@feditown.com ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      Two or three "i"s is readable, but any more and you’re counting.
      I’Ve started using i, k, m, n that’s usually enough.

    • Gork ( Gork@beehaw.org ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      At this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.

    • barsoap ( barsoap@lemm.ee ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      When you have multiple indices you’re also bound to have multiple cardinals those indices count up to, say foo.length and bar.length, so foo_i and bar_i are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you’re dealing with width and height in which case it’s x and y but it’s not that width_i would be incomprehensible.

  • barsoap ( barsoap@lemm.ee ) 
    link
    fedilink
    arrow-up
    14
    ·
    3 years ago

    It depends. x and y are either elements or coordinates, a and b usually elements though in e.g. Haskell reserved (by convention) for type variables.

    The i j k l series is reserved for indices. n m etc. are the counts of something, as such you’ll see i counting up to n. Both are due to mathematical sum notation and general mathematical convention. Random google result:

    Let x1, x2, x3, …xn denote a set of n numbers. x1 is the first number in the set. xi represents the ith number in the set.

    …if you’re using a language in which you use i often chances are you should stop coding in C and get yourself a language with iterators. Manual loops are a bug magnet.

  • dark_stang ( dark_stang@beehaw.org ) 
    link
    fedilink
    arrow-up
    14
    ·
    edit-2
    2 years ago

    deleted by creator

    • DogMuffins ( DogMuffins@discuss.tchncs.de ) 
      link
      fedilink
      arrow-up
      8
      ·
      3 years ago

      I’ve gotten used to using the singular form as in…

      records.filter((record) => …)

      Not saying this way is better but it works for me.

      • karbonkel ( karbonkel@beehaw.org ) 
        link
        fedilink
        arrow-up
        2
        ·
        3 years ago

        I do this too. I hate using just x, because it’s so non-descriptive.

    • russ ( russmatney@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      Yes! I love using x (and xs) for functions over whatever the thing is (or things are).

  • 𝘋𝘪𝘳𝘬 ( Dirk@lemmy.ml ) 
    link
    fedilink
    arrow-up
    12
    ·
    3 years ago

    People who name iterators with one letter have no soul.

    • lowleveldata ( lowleveldata@programming.dev ) 
      link
      fedilink
      arrow-up
      5
      ·
      3 years ago

      two letters it is then

    • catfish ( catfish@lemmy.ml ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      And people who iterate over 3D space using firstDimensionIndex, secondDimensionIndex, and thirdDimensionIndex instead of x, y, z have no sense 😜

      • 𝘋𝘪𝘳𝘬 ( Dirk@lemmy.ml ) 
        link
        fedilink
        arrow-up
        1
        ·
        3 years ago

        x, y, and z are absolutely fine for spatial addressing.

  • Luvon ( Luvon@beehaw.org ) 
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    3 years ago

    I generally use a for each type loop or a map because I am usually applying some function across a collection, and in both cases I use the singular name from the collections plural.

    ’Cities.map(city -> …)’

    For (val city in cities)

    If I actually need the index for some reason I still prefer loop structures that give me the index and the item together

    *note syntax pulled out of my head and not necessarily belonging to any specific language.

    For ( city, index in cities)

    cities.map((city, index) -> … )

    If I need to double loop a matrix array I would use rowIndex and ColIndex for the indexes.

  • Spzi ( Spzi@lemm.ee ) 
    link
    fedilink
    English
    arrow-up
    10
    ·
    3 years ago

    I find it hard to read when these are together:

    • i, j, l
    • n, m, u, v, w

    From all the possible character combinations, somehow the lookalike combinations are among the most popular. Yes, probably comes from math. I hated it even more when my math prof’s i and j on the board were indistinguishable.

  • Cowabunghole ( Cowabunghole@lemmy.ml ) 
    link
    fedilink
    arrow-up
    8
    ·
    3 years ago

    It’s my understanding that i,j are conventionally used in mathematics which carried over into programming, but specifically it comes from Fortran in which all integer variables start with “I” through “N” based on said mathematical convention

  • Dohnakun ( Dohnakun@lemmy.fmhy.ml ) B
    link
    fedilink
    arrow-up
    6
    ·
    3 years ago

    for <thing> in <amount> do...

  • basuramannen ( basuramannen@discuss.tchncs.de ) 
    link
    fedilink
    arrow-up
    4
    ·
    3 years ago

    I don’t like i and j since they are commonly used for imaginary numbers. I like to start on n. Probably because I do DSP.

    • tau ( tau@discuss.tchncs.de ) 
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      3 years ago

      deleted by creator

  • Bilb! ( bilb@lem.monster ) 
    link
    fedilink
    arrow-up
    3
    ·
    3 years ago

    WTF, I have never used nor seen “j.”

    I don’t usually have to name these variables these days though. Pretty much everything I use has foreach or some functional programming type stuff.

    And like that, the off-by-one mistakes disappear.

    • karbonkel ( karbonkel@beehaw.org ) 
      link
      fedilink
      arrow-up
      3
      ·
      3 years ago

      j is for a loop in a loop.

  • Lewistrick ( Lewistrick@feddit.nl ) 
    link
    fedilink
    arrow-up
    2
    ·
    3 years ago

    chuckles in Python

  • menturi ( menturi@lemmy.ml ) 
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 years ago

    I started using the first letter of the thing I am iterating over. This is particularly helpful with nested loops so I can easily remember which index variable corresponds to which thing.

  • librecat ( librecat@lemmy.basedcount.com ) 
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    don’t mind i but personally always use index or x, y, z for games

  • Aiden ( roi@lemmy.blahaj.zone ) 
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    When my brain doesn’t work I’ll resort to naming them the single of the plural. Like keys turns into key when i don’t wanna call it “objkey” or “outrageouslylongnamethatmayormaynotbeafittongwordtodescribeakey”

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 60 users / day
  • 260 users / week
  • 323 users / month
  • 1.83K users / 6 months
  • 871 local subscribers
  • 43.6K subscribers
  • 1.25K Posts
  • 10.3K Comments
  • Modlog
  • mods:
  • AgreeableLandscape ( AgreeableLandscape@lemmy.ml ) 
  • cat_programmer ( cat_programmer@lemmy.ml ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code