0

I've recently started on a new development team. I spend a lot of time coding individual projects in my free time, and the last team I worked on at this company my work was mostly individual.
At the moment I'm spending most of my time maintaining and refactoring our applications and trying to get used to being in a collaborative software environment.

Our applications have a lot of if statements structured like this:

boolean foobar = false;
if(condition){
    foobar = true;
}

Writing code individually, I'd be more inclined to this structure:

boolean foobar = false;
foobar = condition;

For those of you working on collaborative development teams, do you think the latter option would sacrifice readability? Is the elimination of superfluous code worth this sacrifice? Are there other factors I may not be taking into account?

For what it's worth, we use Sonar for source code quality management, and a Sonar flag I've fixed without protest is almost exactly this - that boolean methods should return the condition, not 'true' or 'false'.

Indent
  • 4,675
  • 1
  • 19
  • 35
NickSS
  • 35
  • 7
  • Well the two pieces of code actually do something different in some cases right? The first one only overwrites foobar if condition is true. In other words if foobar is already true, it will not overwrite it to false. If that distinction is irrelevant in your case, I really think it just comes down to a matter of opinion. – nhouser9 Apr 12 '16 at 15:33
  • @nhouser9 You're correct, but in this case, the code would appear exactly as I've shown it hear. We're assuming there's no code between the local boolean declaration of false and the if statement, so there's no scenario where foobar would already be true. – NickSS Apr 12 '16 at 15:48
  • If the code appears exactly as described I would be more inclined to do a third option: `boolean foobar = condition;` Either way it is still just a matter of opinion and style when it comes down to it though. – nhouser9 Apr 12 '16 at 16:00
  • @nhouser9 thank you, I'll keep that in mind. – NickSS Apr 12 '16 at 16:12

1 Answers1

0

You can directly write this without sacrifice readability:

boolean foobar = condition;

To increase readability, the most important things is the name of your variable.

You can use a name like isValid, isComplete

Don't use negative name, prefer isLoser to isNotWinner or inverse your condition to determine isWinnerand use your boolean using if (!isWinner)

Indent
  • 4,675
  • 1
  • 19
  • 35