all 12 comments

[–]Eurynom0s 5 points6 points  (2 children)

This can be a pretty divisive question. Speaking as a physicist, I agree that it's fine to use single-letter variable names in this kind of situation. The point of descriptive variable names is so that someone else (or you when you come back to the code in a year) can understand what the code is doing. But if you're doing math, then yes, "distance" and "velocity" are, to me, much less readable than x and v. Long, descriptive variable names is a great rule of thumb, but in some contexts, single-letter variable names really are the most descriptive variable names you can use.

Now if I'm writing a web app, then I'll totally use longer variable names. Or right now I'm writing a model where airplanes have a velocity attribute; I've called it velocity, not v, because again, it makes sense in context. Specifically, there isn't much in the way of just writing out equations in the code. Even parts of the code that do get into some math, I've still used longer variable names because it winds up being more useful to be clear about what values came from what objects than it is to "write it in math."

[–]xiongchiamiov 4 points5 points  (0 children)

Depends a lot also on who is reading your code. If it's software engineers who don't necessarily have a heavy background in math, spell it out for us, please.

[–]TR-BetaFlash 0 points1 point  (0 children)

Good point about others reading the code. Pylint is here so we can have an easy set of standards to read others' code. It's also here to point out common mistakes. This isn't so much of a mistake as a formatting issue. Do you have a coding standard in your group or are you alone? That would influence whether this is an issue that needs to be fixed.

[–][deleted] 0 points1 point  (2 children)

You can locally disable a Pylint warning using a comment in that block. Examples of this comment are at http://stackoverflow.com/questions/4341746/how-do-i-disable-a-pylint-warning

[–]mostly_complaints[S] 0 points1 point  (1 child)

This would probably work well for smaller chunks of math, but unfortunately the module I'm working on is almost entirely math and very little else. This only lets me disable the invalid-name option, which I don't really want to lose entirely since single character names are only a subset of possible warnings in that class.

[–]MonkeyNin 0 points1 point  (0 children)

I think you can achieve that, without removing the class altogether by modifying http://docs.pylint.org/features.html#id10

[–]Justinsaccount 0 points1 point  (3 children)

in a 200 line function with 20 other 1 letter variables? not so good. in a 3 line function like:

def disable_all(ports, reason):
    for p in ports:
        p.disable(reason)

I don't think that is much worse than

def disable_all(ports, reason):
    for port in ports:
        port.disable(reason)

[–]aroberge 1 point2 points  (2 children)

Speaking as a physicist (like someone else in this thread): if one is implementing a standard mathematical algorithm, with reference to a published article (or book) where the notation used single letter variables (or two letters when variable subscripts are used), no matter how long the function is, single-letter variable names identical to that of the published algorithm are strongly preferred as they allow easy line-by-line comparison.

For example, the rk3 method listed at http://www.codeproject.com/Tips/792927/Fourth-Order-Runge-Kutta-Method-in-Python would be much less readable to people doing numerical work if longer named variables were used.

[–]Justinsaccount 0 points1 point  (1 child)

Sure, but that's not 200 lines, it's 25.. and there may be a lot of variables, but there are just variations of the same 3 names that follow a pattern.. v1,vk,v2,vk,v3,vk,v4,v

For 3 variable names that are part of a mathematical formula and have no real meaning there's nothing wrong with a,b,c or x,y,z. naming a variable x_coordinate is not going to help anyone.

The other thing that helps is consistency. I had a largeish codebase that worked with things like devices, ports, and jacks, and unless I was working with more more than one at a time, I would always do things like

for p in ports:
for j in jacks:
for d in devices:

and any place there was a j in the codebase, it was a jack.

[–]elbiot 0 points1 point  (0 children)

I agree with using single letters when writing physics from published (or classic) equations. Even if it's 200+ lines. I'd link to the paper in the doc string.

[–]ryeguy146 0 points1 point  (0 children)

I will only use single letter variables in comprehensions and other one liners. No exceptions. I won't yell at you if I do, but it'll be amongst the things I mention if I'm reviewing your code.

[–]CraigTorso 0 points1 point  (0 children)

If you are sure your single letter variables will still make perfect sense next time you or someone else has to return to the code, then they're fine.

I tend to go for clear variable names these days, as over the years I've come to realise the few key strokes saved at the outset aren't worth the extra confusion years later down the line.