Why Writing 100 Lines of Code Can Be Better Than 2 Lines Of Codeš”


Hello Techiessss!!
When we first start learning to code, we often hear this advice "Keep your code short the shorter, the better!" But is short code always the smartest choice? š¤ Imagine your friend tells you "Take the shortcut, it's faster." But that shortcut lands you in a traffic jam ā annoying, right? š The same thing happens in coding. Short code might look cool, but when it comes to understanding, fixing, or updating it later ā it can turn into a real headache.
In this blog, weāll explore why writing more lines can actually make you a better coder. Youāll see how clean, readable, and structured code can save time in the long run. So letās break the āshorter is betterā myth... and start understanding.
Lets go⦠Letās break it down in a simple way.
š” What Does "2 Lines of Code" Mean?
Some people love writing very short code. It looks something like this :
#include <stdio.h>
using namespace std;
int main() {
copy(istream_iterator<int>(cin), istream_iterator<int>(), ostream_iterator<int>(cout, " "));
}
This program reads numbers from the user and prints them. Just one line inside main()
. Wow! Cool, right ?
But wait a secondā¦
Can you actually understand whatās going on here?
No?
Donāt worry ā most people canāt, including beginners. Even I would need a moment to read it carefully. š
ā Letās Make It Longer⦠and Easier to Understand
Hereās a version of the same thing, but this time, weāll write it in a clear step-by-step way :
#include <iostream>
using namespace std;
int main() {
int numbers[100];
int num;
int count = 0;
cout << "Enter numbers (type -1 to stop): ";
while (count < 100) {
cin >> num;
if (num == -1) {
break;
} else {
numbers[count] = num;
count = count + 1;
}
}
cout << "You entered: ";
int i = 0;
while (i < count) {
cout << numbers[i] << " ";
i = i + 1;
}
return 0;
}
š§ What's the Difference?
Easy to understand
Shows each step
Can be modified easily
Better for debugging if something goes wrong
ā ļø The Hidden Costs of Short Code
Short code can be:
Hard to read
Hard to debug
Hard to modify
Hard to test
Sometimes, trying to be clever actually makes your program worse.
š§® Example :
Finding the Maximum Value in a List
2-Line Version :
#include <iostream.h>
using namespace std;
int main() {
cout << *max_element(istream_iterator<int>(cin), istream_iterator<int>());
}
Longer Version :
#include <iostream>
using namespace std;
int main() {
int numbers[100];
int num;
int count = 0;
cout << "Enter numbers";
while (count < 100) {
cin >> num;
if (num == -1) {
break;
} else {
numbers[count] = num;
count = count + 1;
}
}
if (count > 0) {
int maxVal = numbers[0];
int maxIndex = 0;
int i = 1;
while (i < count) {
if (numbers[i] > maxVal) {
maxVal = numbers[i];
maxIndex = i;
}
i = i + 1;
}
cout << "Maximum value is " << maxVal << " at index " << maxIndex << endl;
cout << "Numbers greater than 10: ";
int j = 0;
while (j < count) {
if (numbers[j] > 10) {
cout << numbers[j] << " ";
}
j = j + 1;
}
} else {
cout << "No numbers were entered." << endl;
}
return 0;
}
This code is a bit longer, but :
Itās clear
You can edit it easily
š§© Real-World Analogy
Think Like a Teacher, Not a Magician
Imagine youāre teaching your junior how to code. If you give him a two-line program with no explanation, he will be confused. But if you walk him through the logic ā step by step ā he will learn.
The same applies to your future self and your teammates. Clear code helps everyone.
If you enjoyed this blog, share and comment! šāØ Happy coding!
Subscribe to my newsletter
Read articles from Haseeb Ahmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
