Igor's Blog

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

, , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Recent Blog Posts

How to install Panel Glide Blinds from Blinds Online

Showing whitespace characters in vi

Using Jackson to automatically convert Java POJOs to JSON as output of a Jersey service

Enable quick access to the keyboard and emoji viewer in macOS

How to tell if your site is served via CloudFlare

Googe Cloud Platform doesn't support CloudFlare's free edge certificates

Fixing the invalid private key error in the Google Cloud Platform App Engine app Settings page

Cleaning the sticky PlayStation controller analogue thumb sticks

Invoking interactive shell scripts non-interactively

Using variables as column numbers with Bash and Awk when processing text files

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again