-2

I've worked on multiple projects that were started by other people, and one thing I still don't really understand..

1) Sometimes I see a code-snippet like this:

public String retrieveSomething(){
  String result;
  result = doSomethingAndReturnTheResult();
  return result;
}

2) Personally I've used to this instead:

public String retrieveSomething(){
  return doSomethingAndReturnTheResult();
}

3) But from a fellow developer I've heard it's better to do this instead regarding debugging / breakpoints, which I think is indeed a fair point:

public String retrieveSomething(){
  String result = doSomethingAndReturnTheResult();
  return result;
}

I can understand the third option, but I see the first option pretty often from multiple developers and I just don't get it.. :S The only reason I could think of is alignment in the IDE where it's a difference between:

String result = 
  doSomethingAndReturnTheResult();

versus:

String result;
result = doSomethingAndReturnTheResult();

But apart from that I don't see the purpose. Does anyone else do as displayed in the first example, and if yes: why? Or do you have a plausible reason why developers might do this? Personally I think it only adds distractions and makes it less readable than the third example, but that could be just me..


I know this might be fairly opinion-based / lead to discussions, but you can basically translate the question to: What is the extra purpose of using example 1 over example 3 (if any)?

There also isn't really a suitable tag for this on SO.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 3
    Maybe they're former C devs? I don't see any advantage to the first. I use the third in case need to quickly check the values, or add another step of computation later. – Carcigenicate Apr 12 '17 at 12:28
  • I can only assume that this is something people do who have a background in languages where it is considered best practice to do declaration and initialization separately (C, maybe?) – domsson Apr 12 '17 at 12:28
  • 4
    My personal opinion: making code more complicated or harder to read in order to aid debug-ibility just means more bugs to debug. Make code as simple as possible first, then worry about debugging. – Brandon Apr 12 '17 at 12:29
  • Also, check this: http://stackoverflow.com/questions/31733811/local-variables-before-return-statements-does-it-matter (*"Avoid unnecessarily creating local variables."*) – domsson Apr 12 '17 at 12:30
  • I totally agree with @Brandon, readability is a very important thing ! – Dorian Apr 12 '17 at 12:31
  • It can also be a leftover from code that previously had a `try-catch` block, although that's a pretty broad speculation. However, that kind of code would be left after removing the `try-catch` if no other beautification were made. – Kayaman Apr 12 '17 at 12:35
  • I feel like you've answered your own question. Maybe it helps with debugging (if so, I don't know why you wouldn't change the declaration *at the point* you need to debug it). Anyway, no one can tell you why some random developers do that. I personally don't and I don't know any decent Java developers who do. – Michael Apr 12 '17 at 12:36
  • 2
    Have you asked one of the devs that you worked with why they did this? – Steve Smith Apr 12 '17 at 12:44
  • @azro, this asker is very good at providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Don’t take the example as coming verbatim from production code. – Ole V.V. Apr 12 '17 at 12:57
  • @SteveSmith Unfortunately I come across these kind of code-snippets in legacy projects from the middle ages. So usually the developers responsible for this kind of code are already gone for 3+ years. – Kevin Cruijssen Apr 12 '17 at 13:26
  • Thanks all for the comments. I personally do the same as @Carcigenicate. And I also agree with Brandon. Why I ask this question is already living proof of that, because if it was done as example 2 or 3 I wouldn't have looked at it again, now I'm just confused and it makes things overly complicated for no good reason. As for Kayaman's explanation: it's indeed possible other code was later on removed, but I see it in a lot of different projects and on different places in the code from different developers, so I just find it odd.. I'll just use example 2. – Kevin Cruijssen Apr 12 '17 at 13:30

1 Answers1

1

I'm pretty sure those 'patterns' are not only found in java code ;D

1) and 3) are not necessary. I think most of the dev's do not follow those patterns. I have used those constructs too, but not on purpose, I just "ended up" with those. For instance in 3), there could have been another method call where you have used the result, maybe just a logging statement, which was removed later. In example 1), there might have been an if-else block and result would have been assigned from two different methods, but has been removed, or moved to a separate method. So basically, I just didn't take the trouble.

Code Reviews are awesome to get rid of those small ugly details. Even just the thought, that someone will look at it will reduce the dev's laziness ;D

slowy
  • 227
  • 1
  • 6