Why writing 100 lines of Code can be better than just 2!!?

Ritu RajRitu Raj
3 min read

Why sometimes writing 100 lines of code is better than 2 line of code!!?

Being a new computer science student, I always used to think that writing 2 lines of code is better than writing 100 lines of code. As executing 2 lines of code will take fewer resources i.e time, storage e.t.c but i have no shame in admitting that i was wrong!! Let’s be honest i wasn’t wrong i was being naive.. And how was i wrong in it let’s talk about that.

Time Complexity!

But before we discuss how was i wrong!? I would like to talk about this Time Complexity. So, what is this time complexity!? As the name suggests it is not the amount of time taken to compile or execute the code yup! it isn’t!! Rather Time Complexity is “the Rate at which the time taken increases with request to the input size..” now now lets break it down “jaise-jaise aap input badhate ho, ek algorithm ya process ka workload kaise badhta hai, aur is growth ko hum time ke comparison mein measure karte hain”

Time Complexity != Time taken...

What’s the use of time complexity!?

When ever we write any program first we see if the code is doing its job.. after that we see is the complexity of the code which means is the code written in the best possible format or did we use the best possible algorithm!? Why? because we are writing code or making an application for the general public we want to give them hassle free experience i.e the code shouldn’t take much time to execute and should take storage but not that much cause let’s be honest in today’s world storage is not a very big issue!!

Two programs doing the same things

Lets say you are playing “Raja mantri chor siphai” with 6 of your friends and as we know that the siphai has to predict who the chor is!! Now since chor is the odd one out we can represent it by 1 and the rest as 0. You have to write a program so that even when you guyz are far apart the 6 of you can play.. Now there are two possible ways to make the programm..

Approach 1:- Scan each and every single one of them..

int players[6] = {0, 0, 0, 0, 1, 0};

for (int i = 0; i < 6; i++) {
    if (players[i] == 1) {
        return players[i];
    }
}

Here in this approach the software will have to scan each and every single one of them which is simple to do but no very optimal.. here this whole code will be executed 6 times as each times it need to be checked for loop is running..

Approach 2:- Divide and rule

int arr[6] = {0, 0, 0, 1, 0, 0};

int A = arr[0] + arr[1] + arr[2]; 
int B = arr[3] + arr[4] + arr[5];

if (A > B) {
    if (arr[0] != arr[1])
        cout << "Odd one out at index 0 or 1" << endl;
    else
        cout << "Odd one out at index 2" << endl;
} else {
    if (arr[3] != arr[4])
        cout << "Odd one out at index 3 or 4" << endl;
    else
        cout << "Odd one out at index 5" << endl;
}

Here,in this approach we are dividing the entire possibilities into 3 groups then by using if-else statements we are taking the odd one out(\chor)* here there are 2 execution steps 1st the element is divide the it is being checked in which group it is after that it was printed!!!

Conclusion

So after that going through that example we can now see that it is not always necessary that 2 lines of code is better than writing 100 lines of code period!!!

Thank You Pratik Sir… Thank you Hitesh Sir

0
Subscribe to my newsletter

Read articles from Ritu Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritu Raj
Ritu Raj