Category Archives: clean-code

Function Arguments – Clean Code

As an extension to the last post on functions, below are some points about function arguments.

Function Arguments

  • Function arguments take a lot of conceptual power while reading and understanding code.
  • They should be at the same level of abstraction as the function name. For e.g. String createTestableHtml(PageData pageData). Reading this function signature forces you to know the detail which isn’t particular important at that point in time.
  • The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.
  • Output arguments

    • Arguments are most naturally interpreted as inputs to a function.
    • Output arguments should be avoided. If your function must change the state of something, have it change the state of its owning object

    Read the rest of this entry

    Advertisement

    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.
  • Test Driven Development – Clean Code Video

    While watching the episode 6 of the clean coders video I noted down some important points.
    • Design and Code rots lead to fragile, rigit and in-mobile code base. Debugging becomes difficult, estimates mount and implementing any new changes becomes difficult. Uncertainity in the code increases.
    • Maintaining a test suite with a high code coverage has below benefits

    1. Keeps defects under control 
    2. Gives us the courage to change the code in order to clean it 
    3. Unit tests act as code examples for your API. They are low level design documents. For e.g. The test cases helps us to know all the possible ways to call a particular API. 
    4. Writing tests first, makes the production code testable. To write unit tests you would have to make your functions decoupled from each other. This leads to an improved design.

    Read the rest of this entry