Clean Code Tips: Methods

Methods should be small.  Ideally, they should hardly ever be 20 lines long.  This forces the developer to create more methods that can shared and tested more easily.  Smaller methods will also result in less unit tests for each method since there’s less logic applied.

Keeping method small also helps to enforce the concept that methods should only do one thing.  The more logic that exists in a method will likely limit it’s ability to be shared and reused.  It also ensures that changes to the method will only affect the single purpose of the method.

One level of abstraction per method is also a good rule to follow.  This helps others understand what the method is doing which prevents bugs from getting into the software.

Following the above tips will result in more methods, but it doesn’t necessarily result in more code since methods can be shared more easily.

A Few Good Resources

These are some recent books I’ve read related to software.

Clean Code:  A Handbook of Agile Software Craftsmanship

I highly recommend this book.  Go to the Amazon link above to read more about.

The Design of Design – Essays from a Computer Scientist

An interesting book that discusses the various design approaches.

The Art of Unit Testing

The definitive book on how to properly unit test.

Head First Design Patterns

A straightforward reference to many design patterns with easy to understand examples and excellent descriptions.

Design Patterns:  Elements of Reusable Object-Oriented Software

Another excellent reference for many design patterns used in software today.