3

I'm wondering about whether it is good to declare multiple unneeded variables to make my code more readable. Which of the following snippets is better coding? it calculates the force between two masses.

    // 1
    double dx = xPos - b.xPos;
    double dy = yPos - b.yPos;
    double r = Math.sqrt(dx*dx + dy*dy);
    double F = G * mass * b.mass / (r*r);

    // 2
    double Fx = G * mass * b.mass / Math.pow( Math.sqrt(Math.pow(2,xPos-b.xPos) + Math.pow(2,yPos-b.yPos)), 2);

How do I balance readability and performance? Is doing it all in one line with a comment okay?

(I realize that the Math.pow( Math.sqrt( in the second example could be removed but this is just an example)

pyjamas
  • 4,608
  • 5
  • 38
  • 70
  • 9
    Do it in the way that is *most* readable. I think option 1 is more readable than option 2. – Elliott Frisch Dec 30 '15 at 23:22
  • 2
    Always start by optimizing for the programmer. Only if you have proof that there is a performance issue, should you try to optimize for the computer. In any event, any decent compiler will produce substantially equivalent code for both your alternatives. – John Hascall Dec 30 '15 at 23:25
  • 1
    I agree with Elliott--and I question whether there's any difference at all in performance. It would be even better if the intermediate variables had names that reflected what they meant. In a math-formula case, short names are probably OK if they're the same kinds of names that would appear in a formula. But when you're breaking up a long expression in a business application, for readability, it also helps to use meaningful variable names when possible. – ajb Dec 30 '15 at 23:26
  • http://stackoverflow.com/questions/4019180/obsolete-java-optimization-tips – azurefrog Dec 30 '15 at 23:27
  • 1
    You could also define a vector class for those operations. (i would be so happy to have value classes in java) – Tamas Hegedus Dec 30 '15 at 23:27
  • and how 1 achieves better performance? I am sure you are a victim of some myths :) – Sleiman Jneidi Dec 30 '15 at 23:28

7 Answers7

5

How do I balance readability and performance?

You have provided a good example of why readability is more important than performance.

Math.pow(2,xPos-b.xPos)

This is

2^(xPos-b.xPos)

and not

(xPos-b.xPos)^2

as I imagine you intended.

Also

Math.pow(Math.sqrt(x), 2)

is just

x

Either way your expression is too complex and you should just simplify it.

double dx = xPos - b.xPos;
double dy = yPos - b.yPos;
double F = G * mass * b.mass / (dx*dx + dy*dy);

Note: both Math.pow and Math.sqrt are expensive operations so by making the formula simpler, it will also be much faster.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

Always write your code simple, the most readable way (alternative 1). You can use the variable names themselves to clarify your code.

It also makes it easier to debug later with the available variables if you need to watch them. And if you get an Exception, it will point precisely the set of instruction failing, instead of the huge, confusing line of alternative 2.

It won't affect performance since the Just In Time compiler will optimize your code automatically.

Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
1

I would have chosen alternative 1 based on the fact that it makes debugging easier if you get an exception since you will have one operation per line and the stack trace will include the line number where the error occurred. I also personally find it easier to read. The thing I would have changed is your variable names to something more meaningful.

Snusmumrikken
  • 300
  • 1
  • 9
1

How do I balance readability and performance? Is doing it all in one line with a comment okay?

Well, I do not think that it really effects on performance. I recommend doing it as much as readable you can. Commenting on a long calculation formula does not help much.

Enayat
  • 3,904
  • 1
  • 33
  • 47
1

First code for correctness, then for clarity (the two are often connected, of course!). Finally, and only if you have real empirical evidence that you actually need to, you can look at optimizing. Premature optimization really is evil. Optimization almost always costs you time, clarity, maintainability. You'd better be sure you're buying something worthwhile with that.

Premature optimization is the root of all evil (or at least most of it) in programming... Donald Knuth

Should a developer aim for readability or performance first?

Community
  • 1
  • 1
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
1

When you declare each variable not only does it make the code more readable, it can also help to prevent errors. Declaring individual variables can also be useful when solving for different parts of the equation.

double dx = xPos - b.xPos;
double dy = yPos - b.yPos;
double r = Math.sqrt(dx*dx + dy*dy);
double F = G * mass * b.mass / (r*r);

By declaring each variable you can display the distance in the x-direction, y-direction, radius, and the force of gravity between the two objects.

Code that is crushed into one line can often be confusing and lead to silly mistakes.

double Fx = G * mass * b.mass / Math.pow( Math.sqrt(Math.pow(2,xPos-b.xPos) + Math.pow(2,yPos-b.yPos)), 2);
TechSam
  • 11
  • 3
0

NO writing complex code on one line is not ok, imagine working in a real life project in which not only you will be making changes to your code, it will cause calamity if may be your boss cannot understand the code talkless of making changes to it , how do you even want to know where your error is from if you have one

Agbalaya Rasaq
  • 125
  • 1
  • 10