2

My question is general. When should I consider splitting a statement to multiple lines?

I'm writing code on my own, and never worked in a team. I always prefer to make my code as compact as it can get.

For instance, instead of writing:

depth = depth - randomNumbers.nextInt(depth) -1;
Expression expA = createRandomExp(depth);
Expression expB = createRandomExp(depth);
SubtractionExpression subExp = new SubtractionExpression(expA,expB);
return subExp;

I will just write:

return new SubtractionExpression(createRandomExp(depth - randomNumbers.nextInt(depth) - 1), createRandomExp(depth - randomNumbers.nextInt(depth) - 1));

The pros as I see it are:

  • Less lines of code.
  • No need for declaration of variables.

Cons:

  • Can be less readable
  • Some stuff are written multiple times, like: randomNumbers.nextInt(depth) -1

What are the standards in the industry? And what should I consider when writing statements? Some guidelines might help.

I came over this, but it doesn't really answer my question.

Community
  • 1
  • 1
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • I think it's a good practice to avoid unnecessary declaration of variables – Reto Mar 12 '16 at 19:31
  • 3
    I'd always prefer multiple steps for the sake of readability. But that's an opinion based answer ;) – Yassin Hajaj Mar 12 '16 at 19:33
  • 2
    This question isn't really a good one. You are asking for opinion based answers; such things do not belong here. But well; I think balancing is required. The really ugly thing about 3, 5 statements on one line is the fact that exception stack traces with line numbers all of a sudden are not so helpful any more. And using a debugger is also less fun. – GhostCat Mar 12 '16 at 19:36
  • 1
    Why is "Less lines of code" better than readable code? Developing software is (in most cases) not a contest for writing fewer lines of code! – Nitek Mar 12 '16 at 19:40
  • @Jägermeister Thanks, didn't even think about the debugger part and Exception. – Alex Weitz Mar 12 '16 at 19:42
  • Quite - only someone who has other prople test and debug their code would prefer one, big line. The five lines, with temp vars, is very, very good. The one-liner is apallingly bad, and anyone who writes code like that should be fired for gross imcompetence. Yes, debugging IS that important. – Martin James Mar 12 '16 at 20:04
  • It is procedural approach from C and Pascal. See [Redundant variables are evil](http://www.yegor256.com/2015/09/01/redundant-variables-are-evil.html) – ByeBye Mar 12 '16 at 20:25

2 Answers2

7

Multiple lines make easy to read when another developer needs to read your code, and use comment lines to clarify functions, variables, classes, etc. Also, You may leave the company you work and someone needs to improve your code or there may be a dysfunctionality in your code so in case of those reasons, you should write your code with multiple lines in order to makes it easier to read and understand. There should not be any standarts to my knowledge but above reasons will be enough to write codes with multiple lines not in a one line.Additionaly, when you get compiler error while writing single line codes, compiler will say "error found in X line" and you may not be able to understand where is the error in that line. However, multiple lines will ease the situation. I suggest you to write multiple lines instead of single line.

Y.Kaan Yılmaz
  • 612
  • 5
  • 18
  • 1
    btw: same is true for ordinary text. Visual vertical spacing (called paragraphs) makes it much easier to digest textual input ... maybe something you might consider for future postings ;-) – GhostCat Mar 12 '16 at 19:50
  • This is just an introduction, that is why there is just one paragraph :P – Y.Kaan Yılmaz Mar 12 '16 at 19:57
  • 1
    If I could upvote this 100 times, using my own rep to do so, I would. Only someone who has never written any meaningful software, or who has other people to do the debugging for them, would prefer the one-liner. I would fire anyone who wrote 'clever code' like that:) – Martin James Mar 12 '16 at 20:11
  • Do you even use stream in java? – ByeBye Mar 12 '16 at 20:36
0

It is good practice to not declare one-use variables. I really like aproach, when you use your "(" bracket as "{" bracket, like:

return new SubtractionExpression(
    createRandomExp(
        depth - randomNumbers.nextInt(depth) - 1
    ), 
    createRandomExp(
        depth - randomNumbers.nextInt(depth) - 1
    )
 );
ByeBye
  • 6,650
  • 5
  • 30
  • 63
  • It is also good practice to "do not repeat yourself". Which is exactly what the code above is doing. You would be going one step further and create a method that does the `createRandomExp(depth - randomNumbers.nextInt(depth) - 1` for you. – GhostCat Mar 12 '16 at 19:38
  • I think that create random couldnt be extracted to variable, its random thing. – ByeBye Mar 12 '16 at 19:40
  • @ByeBye it is, randonNumbers in this case is SecureRandom object. – Alex Weitz Mar 12 '16 at 19:43
  • What I am saying is: you do not repeat code. If you need "expr x" twice, then you write some method `Whatever getExpr(bla) { return expr x }`; and then you call that method twice; instead of repeating the expression itself. Code duplication is the root of all evil. – GhostCat Mar 12 '16 at 19:47
  • I disagree. You don't need to create method for everything you can use twice. If you want to compute `x - 1` you dont need to write another method for it. It is simple itself. – ByeBye Mar 12 '16 at 19:57
  • Maybe. But depth - random - 1 is already more than that. – GhostCat Mar 12 '16 at 19:58
  • 4
    'It is good practice to not declare one-use variables' - have you ever actually written, tested and debugged any software? – Martin James Mar 12 '16 at 20:07
  • 2
    SO is FULL of posts from devs. who have neglected to use intermediate temp vars. Result - they cannot debug their code because they cannot see clearly what is going on. Worse, they then post their rubbish here, instead of splitting up their over-complex compound expressions, and expect SO contributors to fix it all for them:( – Martin James Mar 12 '16 at 20:14
  • Yes, if you have good Object Oriented application design, and good designed method is not a problem to have nested method calls and object creation. I think that you just think in procedural way. – ByeBye Mar 12 '16 at 20:17
  • @ByeBye that won't fly - I've been writing multithreaded code for 30 years:) – Martin James Mar 12 '16 at 20:49