Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

This is an instalment of my Code Odium series of articles talking about bad programming practices with focus on Java. In this article I will talk about why splitting up variable declaration and assignment is unnecessary.
code_issues.jpg


I happen across a surprising amount of code that can be improved and recently I've been noticing in quite a few places where variable declaration and assignment has been split up. This ends up with code that looks something like this...
 Java
private String myMethod() {
String myString;
myString = "myString";
...
}


That got a raised eyebrow response from me so I did some investigation and it turned out that quite a number of IDEs actually flag single line declaration and assignment and offer to 'fix' it...
var_splitting.png




So why is splitting up declaration and assignment like this unnecessary? It really comes down to the number lines you end up writing and the complexity cues you leave. For a simple assignment like above, splitting up declaration and assignment makes code harder to follow - you have to read two lines instead of one to understand the meaning. It also implies that a more complex or conditional assignment should be in place and maybe there's something missing since it's just a simple assignment instead.

So I would always write something like this...
 Java
private String myMethod() {
String myString = "myString";
...
}


One line declaration and assignment. There is no way to misinterpret the intention there.

I did mention complex or conditional assignments earlier. In this situation it is ok and in fact mandatory to split up declaration and assignment. These cases are the only places where splitting like that should be done though. For example...
 Java
private String myMethod(boolean codingIsCool) {
String myString;
if (codingIsCool) {
myString = "Coding is cool";
}
else {
myString = "Get outta here!";
}
...
}


So there you go, the basic rule of thumb is do not split up variable declaration and assignment unless there is some complex/conditional logic that governs the value assigned to that variable.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Mini review of the Sonoff B1 WiFi light bulb

Coherence cache performance optimistic vs replicated schemes

Coherence and weblogic.xml in different types of J2EE web app deployments

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 1 - introduction and case disassembly

jPhotoFrame updated to version 0.3.1 with an image rotation correction utility

iOS 11 pre-GM mini review before it gets revealed next week

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity