r/programming • • Dec 23 '12

Simulating a solar system with Python

http://users.softlab.ntua.gr/~ttsiod/gravityRK4.html
244 Upvotes

72 comments sorted by

View all comments

Show parent comment

5

u/zokier Dec 23 '12

what's with scientific/math programmers and single letter variable names?!

9

u/katieberry Dec 23 '12 edited Dec 27 '12

[Reddit somehow lost the original content of this post three days later, replacing it with an unrelated comment about user interfaces. It said something about using single-letter variable names because maths and science do, albeit from a larger character set.]

2

u/another_user_name Dec 23 '12

I always double any single letter variable names, so i is "ii", j is "jj" and x is "xx".

4

u/celoyd Dec 23 '12

I like this except that it’s sometimes a convenient optimization to have a squared variable around. For example, I was doing some stuff with the Mandelbrot set, where x2 appears a couple times around the innermost loop, and it was natural to call that xx. Not a dealbreaker, just an observation.

6

u/jjo241 Dec 23 '12 edited Dec 23 '12

Equations are yucky with long variables names:

ke = 0.5*m*v*v;

vs

kinetic_energy = 0.5*mass*velocity*velocity;

For longer equations its harder to keep lines to 80 columns e.g:

p = n * R * T / V;

pressure = moles * gas_constant * temperature / volume;

Anyone who would write a code like this will instantly know what each symbol stands for. The biggest ambiguity is the units used (but that's a different issue).

7

u/zokier Dec 24 '12

Actually I prefer the long forms in both cases. And even the ideal gas law one is just 56 characters, ie well below 80 character line. And for longer equations, I think breaking it down with descriptive temporary variables (or some other way) would be beneficial in either way.

4

u/kisielk Dec 24 '12

A lot of times it's difficult to adding a meaningful name to a variable without being overly verbose. Also it makes it easier to correlate the code with the original mathematical equation.

2

u/[deleted] Dec 24 '12

Most of the single letter variables are standard.

If I know someone who doesn't understand the variables will read it, I'll just write them out in a comment.