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

New HTTP Logger Grails plugin

I've wrote a new Grails plugin - httplogger. It logs:

  • request information (url, headers, cookies, method, body),
  • grails dispatch information (controller, action, parameters),
  • response information (elapsed time and body).

It is mostly useful for logging your REST traffic. Full HTTP web pages can be huge to log and generally waste your space. I suggest to map all of your REST controllers with the same path in UrlMappings, e.g. /rest/ and configure this plugin with this path.

Here is some simple output just to give you a taste of it.

17:16:00,331 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,340 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,342 INFO  filters.LogGrailsUrlsInfoFilter  - 17:16:00,731 INFO  filters.LogOutputResponseFilter  - >> #1 returned 200, took 405 ms.
17:16:00,745 INFO filters.LogOutputResponseFilter - >> #1 responded with '{count:0}'
17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,800 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,801 INFO  filters.LogOutputResponseFilter  - >> #2 returned 404, took 3 ms.
17:18:55,802 INFO filters.LogOutputResponseFilter - >> #2 responded with ''

Official plugin information can be found on Grails plugins website here: http://grails.org/plugins/httplogger or you can browse code on github: TouK/grails-httplogger.