How can 100 lines of code be better than 10 lines of code?


By not moving directly on code let’s discuss a real life scenario,
Suppose you have two ways to go to your house and you are in a hurry.
Short root but consists of traffic due to market area and in that way, you have so many known people whom who need to greed when you come from that way
It is a long way but has very little traffic and the root only a few known people to them you don’t need to greet
So, which way you choose?
In my understanding, you may try to choose the 2nd option while you know the first root is looking short and if you are in the vehicle you may lose less petrol but there you have to face so many people and traffic which by default increases your time to come home in an easy way we can say first root consist a lot of obstacles.
Meanwhile, the second way looks long but due to fewer obstacles to face, we will reach home faster.
You may think that is why I am discussing the way to your home. Don’t worry I am not wasting your time let’s convert the same in code sense,
Let’s move to Tech
Let’s think of the logic for finding a different size ball in a bunch of 9 balls, let the 8 balls have the same weight and only one have a higher weight, let’s find the weight of the ball through 2 different approaches:
Approch 1:
let’s compare the first ball with the second, the second with the third, and so on by comparing all the balls.
#include <iostream>
using namespace std;
int main(){
int waightOfBalls[]={1, 1, 1, 1, 1, 1, 2, 1, 1};
int maxBallIndex=0;
for(int i=0; i<sizeof(waightOfBalls-1); i++ ){
if(waightOfBalls[i]>waightOfBalls[maxBallIndex]){
maxBallIndex=i;
}
}
cout<< "Ball with different weight is at " << maxBallIndex+1 <<"th place"<< endl;
return 0;
}
Approch 2:
Let’s divide the all balls into 3 sets and compare 2 sets at a time if in comparison 1st and 2nd anyone will be heavier then we can conclude that higher weight balls exist in that set and if both are 1st and second sets are equal then the higher weight ball exist in 3rd set.
Now in the selected set, we compare balls in the same manner.
#include <iostream>
using namespace std;
int main(){
int waightOfBalls[]={1, 1, 1, 1, 1, 1, 2, 1, 1};
int maxBallIndex=0;
int setOne= waightOfBalls[0]+waightOfBalls[1]+waightOfBalls[2];
int setTwo= waightOfBalls[3]+waightOfBalls[4]+waightOfBalls[5];
int setThree=waightOfBalls[6]+waightOfBalls[7]+waightOfBalls[8];
if(setOne>setTwo){
// When max weight ball exist in first set
if(waightOfBalls[0]>waightOfBalls[1]){
maxBallIndex=0;
}else if(waightOfBalls[1]>waightOfBalls[0]){
maxBallIndex=1;
}else{
maxBallIndex=2;
}
}else if(setTwo>setOne){
// When max weight ball exist in second set
if(waightOfBalls[3]>waightOfBalls[4]){
maxBallIndex=3;
}else if(waightOfBalls[4]>waightOfBalls[3]){
maxBallIndex=4;
}else{
maxBallIndex=5;
}
}else{
// When max weight ball exist in third set
if(waightOfBalls[6]>waightOfBalls[7]){
maxBallIndex=6;
}else if(waightOfBalls[7]>waightOfBalls[6]){
maxBallIndex=7;
}else{
maxBallIndex=8;
}
}
cout<< "Ball with different weight is at " << maxBallIndex+1 <<"th place"<< endl;
return 0;
}
Ohh no!! Approach 2 consists of lots of lines and for the same task approach 1 takes fewer lines of codes.
You may wonder why we write approach 2 if we can solve the problem in fewer lines but if you see closely if the ball exists in the last place then the first take will need to execute 9 times but in the second we are excluding the 2 sets in one comparison. second code takes more lines but there are fewer executable lines for the computer means less computation. This is the same as our previous examples (to reach home)
Fewer things to face =Faster the move
Hope now the starting statement sounds true:
100 lines of code can be better than 10 lines of code
Subscribe to my newsletter
Read articles from Pinki Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
