The hidden benefit of writing short methods

A method should do only one thingQuoting Uncle Bob:The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. Functions should not be 100 lines long. Functions should hardly ever be …

A method should do only one thing

Quoting Uncle Bob:

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. Functions should not be 100 lines long. Functions should hardly ever be 20 lines long.

From one of Martin Thompson’s presentations I have learned another – more easily available and intuitive – method of judging wether a method is small enough. What you are supposed to do is to physically cover the part of your screen on which the method is displayed with your hand. If you can’t do that, then it means that your method probably is not small enough and you should consider refactoring it.

The obvious advantages that you gain by following these rules are improved readability and maintainability of your code.

But there is one – less obvious – benefit of writing short methods

And that is that Java HotSpot VM’s JIT compiler uses a compilation technique which is called inlining. 
What the compiler does is it substitutes the body of a method into places where this method is invoked thus saving the cost of calling the method. 
Current default for HotSpot is set at 35 bytes, which means that the compiler will inline a method if it contains less than 35 bytes of bytecode.

How do I know the bytecode size of a given method?

The easiest way is to dump the class file containing your method with:
javap -c mypackage.MyClass

which returns bytecode of decompiled class and the size of each method (well actually the size is equal to the byte offset of the last instruction – you can read more about javap HERE).

You May Also Like

Visualizing GIS data in JavaFX 2.0 beta using GeoTools

Geographic data mostly comprises of polygon coordinates sets along with attributes, like country or city name, etc. This is quite easy to visualize in JavaFX, which supports rendering for SVG paths. In the article, I show how to read such GIS data from...Geographic data mostly comprises of polygon coordinates sets along with attributes, like country or city name, etc. This is quite easy to visualize in JavaFX, which supports rendering for SVG paths. In the article, I show how to read such GIS data from...

Devoxx 2012 review


I'm sitting in a train to Charleroi, looking through a window at the Denmark landscape, street lights flashing by, people comming home from work, getting out for a Friday night party, or having a family dinner. To my left, guys from SoftwareMill are playing cards.
I don't really see them. My mind is busy elsewhere, sorting out and processing last two days in Antwerp, where 3400 developers, from 41 different countries, listened to 200 different sessions at the Devoxx, AFAIK the biggest Java conference this year.