Igor's Blog
Programming, DIY, Games, Hacks, and Tech

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 the practice of declaring variables in a method parameter list only to use them as local variables.
code_issues.jpg


This is the kind of code that I've come across a number of times during some of my code reviews...
 Java
public void myMethod(MyObject myObj) {
/* code that doesn't reference myObj */
myObj = new MyObject();
/* code that manipulates myObj variable */
}


Then of course there is the method call to myMethod() that looks like this...
 Java
myMethod(null);


So what's wrong with this?

1. No parameter scoped variable should ever be reassigned inside a method and in fact your parameters should be declared as final to prevent this.

2. Declaration of a variable should be done within its correct scope - if you intend to use a variable as a local variable, declare it so. In this case declaring the variable as a parameter implies that it's an input value that the method requires to run.



The problems identified above are quite severe, but I think the next problem with this code is the most important in this case -

3. Readability and maintainability. If someone comes along who has never seen this code before, they will naturally assume that the method requires some sort of input. This of course would be an incorrect assumption in this particular case, which in terms increases the complexity of what the developer has to keep in mind while working with this code. If this issue isn't fixed, the next developer that comes along faces the same challenge. This decreases productivity and makes it more difficult to maintain a good code base.

4. Of course don't forget the fact that you still need to pass some value when calling the method - a null in my example! This does mean the value goes on the stack and there is a little bit of memory and CPU cycles used for that, I wouldn't consider this as a real performance problem however, but it is wasteful.

So a few things to learn from the above, and just remember - declare variables only when you need to use them and not beyond that.

All code examples shown above have been obfuscated from their original versions but still show similarities in terms of style.

-i

comments powered by Disqus
Other posts you may like...

Recent Blog Posts

A hack to create an uber jar with the Maven Shade Plugin using local jar files

Is it worth it? Apple USB-C Digital Multi AV adapter vs a cheap eBay clone

Running X11 graphical applications after changing to another user using 'sudo su'

How to stop Facebook using ad images as post sharing thumbnails

Picking lists for Atari Lynx capacitor replacement kits

Performance - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Look and feel - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Using math to work out the diameter of hard to reach water cooling pipes

How to get the unreachable shard in the Hinterlands in DragonAge Inquisition

Measy RC12 wireless 2.4Ghz keyboard touchpad review

Recent Galleries

Monument Valley 2 is released and does not disappoint

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

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

Blogs and Friends

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

Blog Activity

Blog Activity
Don't forget to
me for more great articles!
Don't show this again