Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, December 30, 2015

Java best practice - doing large math calculations on a single line?

Java best practice - doing large math calculations on a single line?


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)

Answer by Snusmumrikken for Java best practice - doing large math calculations on a single line?


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.

Answer by Guillaume F. for Java best practice - doing large math calculations on a single line?


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.

Answer by Enayat for Java best practice - doing large math calculations on a single line?


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.

Answer by RamV13 for Java best practice - doing large math calculations on a single line?


You do not have to declare variables for every part of the calculation. The most ideal way to do this is to just stick to the 80 character guideline so your calculation could look like ...

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

Answer by Agbalaya Rasaq for Java best practice - doing large math calculations on a single line?


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

Answer by jafar for Java best practice - doing large math calculations on a single line?


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?

Answer by Peter Lawrey for Java best practice - doing large math calculations on a single line?


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.

Answer by Sam for Java best practice - doing large math calculations on a single line?


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);  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.