What did I end up looking for when reviewing code

Interviewing someone is a great opportunity to get to know the person. I think it is also an equally good opportunity to get to know oneself. Over the course of two and half years, I’ve learnt programming principles organically and only when I was given a piece of code written by a potential candidate for reviewing, did I realize what exactly are those things that I have learnt about good code. They might not be much, but I’ll list them out anyway.

When reading the code, I found myself looking out for the presence of following properties.

Scoping

The fundamental to constructing good software is to reduce the scope of variables. Clear identification of the expressions which will be affected by the variable, and the expressions which will not be affected by the variable will go a long way in helping us write understandable code.

Choosing the correct technology could also play a major part in helping us scope the variables properly. Many would remember ( may be not so fondly ) the old JSP way of declaring and using variables. <% String x = “Hello” %>. Such a variable could potentially impact all the expressions in the page which is a readability nightmare.  Clearly identifying functions and creating user defined types with operations on them help reduce the scope of variables in programs.

Programming to predicates

We provided some candidates with a program to optimise the cost of delivery of a product from two possible store houses, given local costs and transportation charges. A significant number of candidates would make a decision like this if ( nRequestedProducts > 43 ) then do. Here the number 43 which was possibly computed by hand seems to be the threshold of the number of requested products, above which the program chooses to deliver them from a different store house. We call this, programming to specifics or programming to propositions. This program is based upon the logical proposition of the form

nRequestedProducts > 43 -> cost Of Delivering From Storehouse A Is Minimum

This is a poorly constructed code because, once the cost of delivering a product from an local store house increases, the proposition would no longer hold. If 43 were to computed using a function, the program will work for all possible cost structures and hence it is based on the predicate of the form

For all threshold such that threshold is the output of fn(cost structure), nRequestedProducts > threshold -> cost of delivering from Storehouse A Is Minimum

Micro testability

Micro testing is the process of testing in isolation. Each test should test for a specific condition and also, one unit should have only one “kind of” test. A unit which is micro testable is said to follow Single Responsibility Principle. As an example, consider the following function

String parse(String input){
  String[] words = input.split(",");
  StringBuilder result = new StringBuilder();
  for(String word : words){
    if("Hello".equals(word)) result.append("1");
    else result.append("0");
  }
  return result.toString();
}

The tests for this function must span across two input spaces. One set of inputs to test parsing another set of inputs to test of the conversion of word to integer. This function does not follow SRP. It could be split into two functions, viz

List<String> parse(String input)

String convert(List<String words>)

This effectively leads to smaller tests.

Type leverage

Type leverage of a function is the ratio of, ( the number of unique types defined in the parameters + the number of types that are returned form operation on parameters)/ total number of types used in the function. Essentially, if the type leverage is low, it means that we are doing operations that could be moved into the parameters themselves by extending their type there by increasing encapsulation. This is the property that is informally described by the law of demeter.

Described above are some properties that will help us write programs made of crisp abstractions, clear encapsulations and good division of responsibilities. After all, fundamentals never go out of style.

Advertisements

One comment

  1. Pingback: My most valuable learnings on MoTech | Jiraiya's Blog


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 )

Google+ photo

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

Connecting to %s