Multiple variable declared on the same line
27
28 public void updatePosition(List<Agent> agents) {
29 // Update the position of the agent based on the positions of the other agents
30 double dx = 0, dy = 0;31 for (Agent other : agents) {
32 if (other != this) {
33 double distance = Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
Multiple variable declared on the same line
18}
19
20class Agent {
21 private double x, y;22
23 public Agent() {
24 x = Math.random() * 100;
Description
Multiple variables (or fields) should not be declared on the same line.
Declaring more than one variables (or fields) on the same line makes the code harder to read. Things might get more confusing if some of those variables are initiliazed and some of them are not.
Bad Practice
class Klass {
private int a, b = 20;
private void method() {
double d1, d2 = 3.5, d3;
// ... rest of the code
}
}
Recommended
Consider declaring one variable per line.
class Klass {
private int a;
private int b = 20;
private void method() {
double d1;
double d2 = 3.5;
double d3;
// ... rest of the code
}
}