Functions – Clean Code Video

While watching the episode 3 of the clean coders video I noted down some important points.

Functions are the first line of code organization in your application.

Small Functions

  • The first rule to having clean functions is that they should be small.
  • Typically classes hide in large functions.
    • If you find functions that can be divided into several functional areas and you have variables that are used by all those areas then what you really have is a class. After all, a class is a group of functions that use a common set of variables.
  • Having lot’s of small well named functions is good. They act as sign post which help the reader to navigate through your code easily.
  • We should not be worrying about the function call overhead since the current computers are so fast that this overhead might be less then a nanosecond. Instead we should take the advantage of such high speed hardware to improve our code for readability first.

Do One thing

  • Function should do only one thing, it should do it well and it should do it only.
  • If your function is composed of many sections then its doing more then one thing (at least from the point of view of the reader).
  • If the function manipulates more then one level of abstraction then its doing more then one thing. By manipulating multiple levels of abstractions we mean, combining the technical details with business concepts in a   single method.
  • How do we do know that a function does one thing
    • Continue extracting out functions until you cannot do it any more. (This is called the “extract till you drop” rule). If you can extract out a function from another then by definition the original function did more then one thing.
Advertisement

Posted on October 29, 2011, in clean-code and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: