Back

Why 100 Lines of Code Can Be Better Than 2 Lines of Code?

By Rohit Yadav on June 5, 2025

5 min read

Why 100 Lines of Code Can Be Better Than 2 Lines of Code ?

In programming, we often hear that shorter code is better. But that's not always true. Sometimes, writing more lines of code can actually help us write faster and more efficient programs.

Let's understand this with a simple example.


Real-Life Example: Searching in a Dictionary

Imagine you're looking for the meaning of the word "Garrulous" in a dictionary. You can try two ways:

1. Search from the first page:

Start from page one and keep flipping pages until you find the word.

2. Use a smarter way:

Open the dictionary somewhere in the middle. Check the words on that page.

  • If the words are after 'G', go back.
  • If the words are before 'G', go forward.
  • Repeat until you find 'G', then do the same for the next letters ('a', 'r', etc.).

Which way is faster?
Clearly the second one, because you skip a lot of unnecessary pages. This is how binary search works.


Coding Example: Searching in an Array

Let's take a sorted array of numbers:

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

We want to find a specific number (called key) in this array.


Method 1: Linear Search (Simple but Slow)

for (int i = 0; i < 10; i++) {
    if (arr[i] == key) {
        element = arr[i];
    }
}
  • Easy to write
  • Up to 10 comparisons

Method 2: Binary Search (More Code, Much Faster)

int start = 0;
int end = 9;
int mid;

while (start <= end) {
    mid = (start + end) / 2;

    if (arr[mid] == key) {
        element = arr[mid];
        break;
    } else if (arr[mid] < key) {
        start = mid + 1;
    } else {
        end = mid - 1;
    }
}
  • More lines of code
  • Much fewer comparisons (only around 3 for 10 elements)

So, Which One is Better?

Even though the second method has more lines of code, it is faster and more efficient, especially when dealing with large amounts of data.

The key takeaway:

Don't judge code by its length. Judge it by its performance.

Sometimes, 100 lines of well-thought-out code are better than 2 lines of slow code.


Hire me for your next project - Let's chat!

Drop me a message and let's bring your idea to life.


Drop your email below and I will get back to you soon.