• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One line methods and clean code

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding of writing clean code says I should change this:

into this:

because the executeMenu() method is more descriptive in the second code segment. I was wondering what you more experienced programmers thought. Thanks!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a good idea to write methods that are not too long, and especially write methods that have only a single purpose (one hint for this is: if you have a hard time deciding what the name of a method should be, then the method is probably doing too many things).

But you can also go too far - as you can see, your version with four separate one-line methods is a lot longer than one method with four lines. Especially if you're not going to re-use those four one-line methods in other methods, then there's not much of a benefit to have these four methods.

This is ofcourse something for which there are no hard rules, but my rule of thumb would be: if a method becomes longer than about 20 lines, think about how you could design it in a different way or split it up into smaller, more clear methods.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent tip

one hint for this is: if you have a hard time deciding what the name of a method should be, then the method is probably doing too many things).

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:It's a good idea to write methods that are not too long, and especially write methods that have only a single purpose (one hint for this is: if you have a hard time deciding what the name of a method should be, then the method is probably doing too many things).

But you can also go too far - as you can see, your version with four separate one-line methods is a lot longer than one method with four lines. Especially if you're not going to re-use those four one-line methods in other methods, then there's not much of a benefit to have these four methods.

This is of course something for which there are no hard rules, but my rule of thumb would be: if a method becomes longer than about 20 lines, think about how you could design it in a different way or split it up into smaller, more clear methods.


I share your concern about making the code longer which is why I asked the question, but when I look at examples in the Clean Code book they seem to do this a lot for clarity. For example part of the cleaned up version of the SetupTeardownIncluder on pg 50 looks like this:


Similar to my original example, it has several one line methods and could be shortened by making the following changes.

I assume the author would claim the first while longer was easier to follow.

I appreciate your response and am not trying to argue with you. I just wanted to get some feedback on how other programmers decided when they were going too far in shortening their methods. Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic