By Rohit Yadav on June 5, 2025
5 min read
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.
Imagine you're looking for the meaning of the word "Garrulous" in a dictionary. You can try two ways:
Start from page one and keep flipping pages until you find the word.
Open the dictionary somewhere in the middle. Check the words on that page.
Which way is faster?
Clearly the second one, because you skip a lot of unnecessary pages. This is how binary search works.
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.
for (int i = 0; i < 10; i++) {
if (arr[i] == key) {
element = arr[i];
}
}
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;
}
}
Even though the second method has more lines of code, it is faster and more efficient, especially when dealing with large amounts of data.
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.
Drop me a message and let's bring your idea to life.
Drop your email below and I will get back to you soon.
Made with Love by Rohit Yadav
Portfolio inspired by Manu Arora