Fewer Lines ≠ Better Code

In programming, there's a common misconception that shorter code is always better. While concise code often improves readability, it doesn't always guarantee performance efficiency, especially in problems where logical optimization matters.

Let’s examine this with a classic logic puzzle and two different coding approaches: one short and straightforward, and one slightly longer but smarter in its execution.

First Approach: Longer Code with Smarter Logic

int nineBallLongCode()
{
    int a[9] = {1,1,1,2,1,1,1,1,1};  // Ball weights

    int group1 = a[0] + a[1] + a[2];
    int group2 = a[3] + a[4] + a[5];
    int group3 = a[6] + a[7] + a[8];

    if (group1 == group2)
    {
        if (a[6] > a[7])
            return 6;
        else if (a[7] > a[6])
            return 7;
        else return 8;
    }
    else if (group1 > group2)
    {
        if (a[0] > a[1])
            return 0;
        else if (a[1] > a[0])
            return 1;
        else return 2;
    }
    else
    {
        if (a[3] > a[4])
            return 3;
        else if (a[4] > a[3])
            return 4;
        else return 5;
    }
}

How It Works

  • The balls are divided into three groups of three.

  • The first two groups are compared:

    • If their weights are equal, the heavier ball must be in the third group.

    • If not, the heavier ball is in the group with the greater total weight.

  • Within the identified group, we do only two additional comparisons to find the heavy ball.

Efficiency Analysis

  • Total comparisons in worst case: 4

    • 2 group comparisons

    • 2 individual comparisons within the heavier group

  • Mirrors the human strategy of solving this with a balance scale.

  • Highly optimized in terms of logic and performance.


int nineBallShortCode()
{
    int ball[9] = {1,1,1,2,1,1,1,1,1};
    int maxWeightIndex = 0;
    for (int i = 1; i < 9; i++)
    {
        if (ball[i] > ball[maxWeightIndex])
            maxWeightIndex = i;
    }
    return maxWeightIndex;
}

How It Works

  • A simple loop compares every element with the current maximum.

  • Tracks the index of the ball with the highest weight.

Efficiency Analysis

  • Total comparisons in worst case: 8

    • Compares each element with the current max.
  • Straightforward and readable.

  • Doesn’t take advantage of the logical structure of the problem.

Performance Comparison

CriteriaLong Code ApproachShort Code Approach
Comparisons (worst case)48
Code LengthLonger (but still simple)Short and clean
PerformanceBetter (optimized logic)Slower (linear search)
Real-world analogyBalance scale logicBrute-force search

Why Longer Code Wins Here

In this particular problem, the longer code is smarter:

  • It reduces unnecessary work by eliminating two-thirds of the data after one comparison.

  • It demonstrates the power of domain-specific logic. This isn’t just code—it’s a strategy.

  • It reflects a principle often seen in algorithm design: think before you loop.

When Should You Prefer Longer Code?

  • When performance is critical.

  • When the problem has a logical structure that can be leveraged.

  • When reducing the number of operations outweighs code brevity.

As developers, we’re often tempted by elegant one-liners and minimalist solutions. But sometimes, taking the longer route—with clear reasoning and structured logic—leads to a far more efficient result.

This example teaches a vital lesson:
Don’t measure code by its length, but by the thought that went into it.

1
Subscribe to my newsletter

Read articles from Ahmad Bin Abdul Jabbar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ahmad Bin Abdul Jabbar
Ahmad Bin Abdul Jabbar