Showing posts with label Philosophy. Show all posts
Showing posts with label Philosophy. Show all posts

Wednesday, July 9, 2008

Musings about Refactoring

For the last couple of weeks I've been paired with Lior on a large refactoring task that was long overdue. We did not have any specific bug to fix, but rather wanted to simplify some of the dustier corners of our codebase. This really helped me realize the importance of refactoring, and specifically of doing it frequently.


The first thing I noticed was that both of us struggled when reading the code; it was not badly written - there were messy parts and elegant parts like anywhere else - it was just alien to us. We did not know what the author meant when writing the code, the naming convention was confusing to us and the reasons behind some of the trickier stuff were a mystery to us.


Now, after major renaming, commenting out, deleting, simplifying and extracting, we believe the code is simpler and more readable. However, I believe that if we leave it at that in a couple of years another pair may be in the exact same situation - our code may be mystifying to them, no matter how self descriptive we tried to make it. By refactoring frequently this can be avoided, or at least minimized.


Refactoring is easy to neglect; in the TDD cycle we never forget to write tests, implement code and test it, but it's easy to leave it at that once all tests passed. I intend to put emphasize on doing another refactor + test run on any code I product, in order to make sure I did my best to keep it simple. Also, when fixing something I fully intend to look beyond the immediate fix (which may be something small like adding an 'if' condition), and go over the entire method/class I modified. You may write code that flows like poetry, but after patching it a couple of times the rhymes will be off and the punch line is gone. This may not seem important when trying to get as many features as possible out the door, but missing it may have severe effect on the quality of the code base, the time it takes new people to learn it and how testable it is.

Tuesday, April 22, 2008

Can Num3rs Lie?

In a series of posts Oren (Ayende) and Sasha have debated the need to design for performance. Both parties make some very compelling arguments and its real interesting reading material. Since I still need to sort my thoughts on the matter (which will be presented in a future post) here's just a teaser.

Oren has rose to the challenge, and in a series of experiments have tested the performance difference between the two design approaches. Here's one of his experiment result

And the results are:

  • IList<T> - 5789 milliseconds
  • List<string>, NullList - 3941 millseconds

Note that this is significantly higher than the previous results, because no we run the results two hundred million times.

Individual method call here costs:

  • IList<T> - 0.0000289489
  • List<string>, NullList - 0.0000197096

We now have a major difference between the two calls. When we had a single impl of the interface, the difference between the two was: 0.00000406 milliseconds.

With multiple implementations, the difference between the two methods is: 0.0000092393 milliseconds. Much higher than before, and still less than a microsecond.

which Oren concludes that although there is a performance difference, the actual time difference even in this close to "worse" case is so small so there is not much point to consider it.

is there?

lets try a different interpretation:

Reading the numbers I can also say that using the List<String> is 31% (0.0000092393/0.0000289489) faster then using the IList<String>.

Question - How much time and money do you think that Intel&AMD will invest in the next couple of years to achieve a CPU which runs 31% faster then current technology? (actually I don't know either, but I sure wouldn't mind putting my hands on a small part of it).

Point is, that in cases where performance is an issue (which is not true for all cases), I would most definitely go down the design road which offer a 31% speedup.