Search code examples
algorithmpeer

How do I convince a peer that algorithms are important?


A peer of mine is working on a report that displays the weekly (Sunday to Saturday) advance of every employee in our small consultancy firm. There's a piece of code he wrote that shows the columns corresponding to the days in the target week. His algorithm is the following:

  1. Get which day of the week the first day of the month is. If it's Sunday, set a flag to zero; otherwise, set it to one.
  2. Iterate through all the days of month. If it's Sunday, increment the flag. Then, if the flag's value is equal to the week to be displayed, show the column corresponding to the current day; otherwise, hide the column.

Of course, the flag indicates what the current week is.

I suggested another algorithm:

  1. Get which days of the month are the first (F) and last (L) days of the specified week. For example, the first week of October 2009 starts on Tuesday 1st and ends on Saturday 3rd.
  2. Iterate through the columns corresponding to days 1 to F-1, and hide them.
  3. Iterate through the columns corresponding to days F to L, and show them.
  4. Iterate through the columns corresponding to days L+1 to DaysOfMonth, and hide them.

The "difficult" part in my algorithm is part 1. I mean "difficult" as in "difficult to understand", because the algorithmic complexity of doing it is constant. And my algorithm has the advantage of having tighter loop. My peer's loop does a comparison for every day of the month. Mine doesn't.

This was a little example and you might say that over-optimizing here is a bit too paranoid. But his programming style doesn't change a bit when we write actual performance-critical code.

His code is also full of these tests:

/* doSomething() doesn't change the state of the relevant variables. */
if (condition)
{
    flag++;
    if (flag > test)
        doSomething();
}
else
    if (flag >= test)
        doSomething();

When, of course, it can be done like this:

if (flag >= test);
    doSomething();
if (condition)
    flag++;

What do I do?!?!?!

EDIT: I corrected the comparisons in the code samples.


Solution

  • Is this your task, or his? If its his, let him do it. I am a person who thrives on efficiency, in fact I become very frustrated when something seems inefficient and is out of my control.

    There are well over 200 people on SO who could put my most 'efficient' ideas, algorithms and code to shame. Probably more, you can't go by rep alone. If Linus Torvalds himself signed up, he'd start at 1 like the rest of us.

    What you have to consider is that people need to be able to maintain the code that they write. This means, they have to understand it as if they gave birth to it. Even if someone demonstrated another algorithm as much more efficient than my own, I would not use it unless I was comfortable with it.

    If this is a joint project, write it your way, demonstrate the speed up and then spend some very, very patient hours with your peer to help him really grasp it.

    Look back on stuff you wrote 5 years ago, everyone has to learn by doing and everyone does stuff at their own speed, especially learning.