Refactoring code into fewer lines does not necessarily reduce its complexity

Developers are almost universally aware that they should try and write code that is simple to read and maintain.  Complicated code can be a stumbling block for another developer (or even yourself at a later date) when he/she needs to fix bugs or modify functionality.

So refactoring existing code in an effort to reduce its complexity is definitely a good thing.

But refactoring code into fewer lines does not necessarily mean that its complexity has been reduced.  In fact, the opposite can occur.

Take this example posted by an Intel blogger.  While I fully agree that lambdas are awesome, and I eagerly await them in C++0x, I don’t agree with his assertion that the lambda version of his example is simpler.  Regarding his first non-lamda version, he concludes, “The code is indeed complex. It contains too many lines and three methods.”

Certainly the first example has more lines than the lambda equivalent.  But on its own I wouldn’t call the first example complex at all.  It has a function event handler for the control, a function for the thread, and a function for the UI callback.

The lambda version just puts the thread function and callback function into anonymous functions inside the control’s event handler.

Syntax-wise, I’d argue the lambda version is more complex than the first.

2 Responses to “Refactoring code into fewer lines does not necessarily reduce its complexity”

  1. Spanky Says:

    Well done captain obvious.

  2. Simon Jones Says:

    Hehe.

Leave a Reply