15

In my Java class i am declaring variable like this

BigDecimal sumFeeBilled = new BigDecimal(0), sumPaid = new BigDecimal(0); 

Or we have to declare like this in multiple line

BigDecimal sumFeeBilled = new BigDecimal(0);
BigDecimal  sumPaid = new BigDecimal(0);

Which one we should follow ?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • 1
    It's a matter of taste and code readability. – Silviu Burcea Nov 21 '13 at 09:52
  • 10
    It's a matter of opinion, but I prefer the second way since it's clearer. – Kayaman Nov 21 '13 at 09:52
  • 4
    @Kayaman Completely agree. My eyes are trained to stop after the first declared variable is assigned a value. – Kevin Bowersox Nov 21 '13 at 09:54
  • 1
    I prefer the 2nd way because it is more readable... – Rakesh KR Nov 21 '13 at 09:54
  • 2
    I think it's a bit more than opinion. Saying that, IMHO, suggests a very individualistic view on the world. There has to be a standard to some things and if you worked for me your opinion on this would be null as I would want, and require, my code base to be equally and dependably consistent in terms of readability and structure. If your code is limited to you only then I suppose it would be whatever you like, but the only reason I could think of doing this is out of laziness. – ChiefTwoPencils Nov 21 '13 at 10:46

6 Answers6

11

The less things that happen on a single line the better. If you choose the second option the code will be easier to debug(you can put a breakpoint on the line where the second initialization happens skipping the first one for instance) and easier to read(IMHO). The only thing that this costs you is a single line. I think it is worth it.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
8

This is simply a matter of taste and preference. However if you don't set guidelines it will become a hotbed of endless debate/arguments in most development teams, alongside Vim vs Emacs or IntelliJ vs Eclipse.

What I would recommend is setting coding standards for your team, and the simplest way to do this is to reference already-existing ones such as the Sun (now Oracle) Java Guidelines which in this case suggest using one declaration per line.

Here what Sun's definitive guide says about declarations[1]:

6.1 Number Per Line

One declaration per line is recommended since it encourages commenting. In other words,

int level; // indentation level
int size;  // size of table

is preferred over

int level, size;

Do not put different types on the same line. Example:

int foo,  fooarray[]; //WRONG!

[1] http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2991

Olly
  • 7,732
  • 10
  • 54
  • 63
6

This is your choice, but I often use the second one when using local variables, and for globals, I use the following to reduce clutter:

public static final String CONSTANT_A = "constant a",
                           CONSTANT_B = "constant b",
                           CONSTANT_C = "constant c";

instead of

public static final String CONSTANT_A = "constant a";
public static final String CONSTANT_B = "constant b";
public static final String CONSTANT_C = "constant c";

With this example it is not that significant, but imagine having 40+ constants and tell me which of both you prefer...

tilpner
  • 4,351
  • 2
  • 22
  • 45
  • 1
    If you end up with 40+ constants in one place, you've got some design problems and this way of "reducing clutter" is only a painkiller, not a remedy – Konrad Morawski Jun 17 '15 at 14:24
  • @KonradMorawski - If I don't err, I was thinking of magic numbers for a protocol, but obviously that's hard to say at this point. I still don't know a better solution to that, than this. – tilpner Jun 17 '15 at 14:29
  • 2
    Having a bunch of constants isn't a "design problem", @KonradMorawski. In order to talk about design problems, we need to know the context. A Java file full of constants is just some data, we can't judge it productively. – DavidS Mar 02 '16 at 23:18
  • @DavidS having so many *in one place* is at least a strong indicator of a possible design problem (likely a violation of SRP, for starters) – Konrad Morawski Mar 14 '16 at 08:56
5

Second variant more readable. Think about people who will read your code

Oleksandr H
  • 2,965
  • 10
  • 40
  • 58
2

Java best practices state "one declaration per line".

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
1

Declaration of multiple variables per line can reduce code readability and lead to programmer confusion. It could also cause confusion about the types of variables and their initial values. In particular, do not declare any of the following in a single declaration:

1. Variables of different types
2. A mixture of initialized and uninitialized variables

In addition, You can declare multiple variables, and assign multiple variables, but not both at the same time

Linga
  • 10,379
  • 10
  • 52
  • 104