10

Just had to debug a piece of code for the 5th time today and I'm starting to lose my mind. Why do programmers insist on using single-letter variable names?! Is it really that hard to come up with something more descriptive? I mean, I get it, "x" is easy to type, but so is "counter" or "iterations". Does anyone else feel like they're just constantly fighting a losing battle against the entropy of poorly written code?

Comments
  • 2
    Ask @jestdotty.
    I had a fight with her about single letter variable names a long time ago and she sees no problem with that at all. 😆
  • 4
    @marymireles you should set up a linter which checks for single letter names (all linters should do this but default) and other crimes. And don’t feel bad for lecturing devs about that.
  • 5
    In the same category as writing variable names in your native language...

    Fun until I get the program written in Czech and have to debug it...
  • 3
    @Lensflare wasn't it i and j variables

    because that's the only fights I have about single char variable names

    I'm ok with other single char variable names though. like javascript promises there's a function that accepts or a function that rejects, so I make those a and r

    I still write full variable names for things that are domain specific, or some context-holed single word name since I don't like long ones
    I have encountered a dude who wrote single char variables for everything and I think he was of the camp that believed it would give him "job security". little did he know management never looks at the code so it doesn't work like that, but I'd figure that's why one encounters deliberately unreadable code. also syntax that's correct but just fucks with your head and serves no other purpose but to fuck you up when you read it

    to be fair I think when people complain about syntax it's a skill issue. yeah it's a pain, but you're a programmer so get over it and get better?
  • 2
    Single letter variables being easier to type isn't worth much if a later version of you or someone else cannot read your code. This is a hill that I am 100% dying on in code reviews and when establishing a "style guide", not that anyone gives a shit about either, it seems. Best you can do for someone else's code is to either rename the variables (hopefully they're not being reused in too many places, but then again, that's why I learned regex in the first place), or use comments for your own benefit.
  • 0
    They think they still in school.

    I try to use at least 3 letters. For common loops I often use ind for index. I don't use index because it can conflict in other places in the code depending upon the current namespace. I use qml and index is often used in qml code.
  • 0
    I use descriptive names (sometimes to a fault) outside of specifically iterators in for loops. I'll use `tempcounter` or whatever if I need while loops that may step either way, but if it's specifically a for loop, i'm starting at i and working downwards as needed. It's a bad habit I picked up a long time ago from playing with TI-BASIC while bored in middle school.
  • 2
    @jestdotty
    "there's a function that accepts or a function that rejects, so I make those a and r"
    Everyone (including the docs) calls it resolve and reject
    If you see a line "a(1)" and have to look up the definition to even understand what it does, the name sucks
  • 2
    Single letters were used when memory was scarce. Nowadays it should be best practise to have descriptive variables, if it’s not a purely scientific context. A colleague tried to lecture me about some variable with about 5 syllables being too long and hard to maintain. Bitch this is the only way I know what this code does in a year.
  • 1
    @Chewbanacas well, today the compilers are smart enough and long variable names does not result in a bigger runtime memory size.

    So just in case there is a dev here that still uses Fortran, maybe it is now time to upgrade your code to something more modern :P
  • 4
    Don't forget you can always use two characters instead of one!

    c0

    c1

    c2

    ..

    cn.
  • 2
    @Wisecrack that’s a typical mathematician 😂
  • 4
    @Lensflare A lot of math opened up for me when I stopped tryign to learn the notation and started looking up how it is implemented in code

    Probably next steps are looking at the code algorithms and comparing them to their notation in math.

    Only real thing that trips people up beyond conceptually, is variable-use/naming conventions that largely go unwritten.

    Mathematicians have a fucking hard-on for reusing the same greek letters to mean multiple things, and this doesn't get explained like *at all* to laymen just learning their way around new fields.
  • 1
    @Wisecrack if you can be sure only mathematicians or generally scientific people will be looking at the code that’s alright imo due to arbitrarily sized matrices and whatnot

    I can see the typical js bro panicking tho
  • 3
    I'm ok with single letter variables in filter and similar methods, but not for actual variables that I have to use elsewhere.

    Array.filter((x) => x.id !== 3) ok

    const p = Array.filter((x) => x.id !== 3) not ok
  • 2
    @devJs especially if it's super obvious what's being iterated on, like

    appointments.map((a) => ...)
  • 0
    @Chewbanacas js bro looking into the mirror, losing their mind. green goblin vibes intensify.
  • 1
    @spoiledgoods In this case for me it is fine.

    In the case of @devJS it is not really clear what is in the array. What are you filtering ?
  • 1
    @Grumm it was just the example, other example would be

    cartItems.filter((cartItem) => cartItem.id !== id)

    Ah fuck, yeah, i use single letters sparringly, but seems just for monoword arrays like ...

    products.filter((p) => p.id !== id)
  • 0
    100% agree with @devJs

    x is good for oneline Array-functions.

    Especially in code where you write a lot of that stuff.
    I find that over time, if I name these variables something specific I end up regretting it and changing to x when I need more of the same code

    Like if start with

    activeUsers = users.filter(user => user.isActive)

    And then I wanna do the same for an array of admins, which have the same props as users

    Then I often change it to

    activeUsers = users.filter(x => x.isActive)
    activeAdmins = admins.filter(x => x.isActive)
  • 1
    @jiraTicket Swift and Kotlin have a solution for that:
    It’s not necessary to provide explicit names for closure arguments.
    In Swift, we can access the arguments in order with $0, $1, etc.
    And in Kotlin we use the implicitly named argument "it".
    Also, in Swift and Kotlin we have variable shadowing, so it’s not a problem when you use a name that is already defined in the upper scope. I mention this because some devs here brought this up as an argument against proper naming.
Add Comment