Tuesday, March 18, 2014

The law of least surprise

Extreme programming argues that documentation is not needed, since code is the best description of what the program does. In order for this to be true, code has to be readable. Very readable.

It is important to be able to tell what is the direction of data in every line you read. In the following case, which variable is the source and which is the destination ?

    Array *names;
    struct Detail details;
    ...
    set_name_details (names, &details);

Here is a simple rule to make the caller code clearer...

Tuesday, March 11, 2014

The Roll - Unroll Pattern

The pattern that is most predominant in programming is:
  • Setup some state
  • Do some work
  • Clean up
Manual housekeeping of this repetitive task requires too much care and energy, from the programmer’s side. Repetitive tasks sounds like a work that should be automated, rather than maintained manually.
Read how you can create an elegant piece of code, while reducing the maintenance load.

Saturday, March 8, 2014

Algorithms should read like pseudocode

Algorithms should read like pseudocode
When an algorithm is described like pseudo-code, there is hardly ever any mention about the underlying data-structures. No statement is made on whether a list, a dynamic array, or a binary tree should be used.
With C++, it is possible to code in such a way.