2 lines or 20 lines of code?

The question may pop into the minds of many programmers while writing their code that what to choose a 2 lines of code to solve the problem statement or 20 lines of code to solve the problem statement. Which code or program is better one? Well, the answer is not that simple that it must be 2 lines of code because it is quick to read and understand or it should be 20 lines of code because it must have handled some extra issues that may occur. The answer is it depends.
Choosing between writing 2 lines of code or 20 lines to solve a problem isn't straightforward. The best approach depends on factors like readability, simplicity, and handling potential issues.
Let us give you all programmers a real- world example. Suppose , we have 12 planes in which 1 is lighter plane and others are heavier planes and we have to find the plane with lightest weight assuming all the other heavier planes than this plane have same weight. There can be two approaches to solve this problem as shown below.
Brute Force Approach
#include <stdio.h>
int findLighterPlane(float planes[], int size) {
float referenceWeight = planes[0];
int lighterIndex = 0;
for (int i = 1; i < size; i++) {
if (planes[i] < referenceWeight) {
referenceWeight = planes[i];
lighterIndex = i;
}
}
return lighterIndex;
}
int main() {
float planes[12] = {1000, 1000, 1000, 1000, 1000, 1000, 950, 1000, 1000, 1000, 1000, 1000};
int lighterIndex = findLighterPlane(planes, 12);
printf("The lighter plane is at index: %d\n", lighterIndex);
return 0;
}
In this approach, findLighterPlane(float planes[],int size) function takes 2 or 3 lines of code to give us the result or answer of the problem statement , which is “The lighter plane is at index: 6”.
Divide and Conquer Approach
#include <stdio.h>
int findLighterPlane(float planes[],int size){
float referenceWeight = planes[0];
float A = planes[0] + planes[1] + planes[2] + planes[3];
float B = planes[4] + planes[5] + planes[6] + planes[7];
float C = planes[8] + planes[9] + planes[10] + planes[11];
if(A == B) {
if(planes[8]==planes[9]) {
if(planes[10] < planes[11])
return 10;
else
return 11;
}
// if planes[8] != planes[9]
else{
if(planes[8] < planes[9]
return 8;
else
return 9;
}
}
// if A != B
else if(A < B)
{
if(planes[0]==planes[1]) {
if(planes[2] < planes[3])
return 2;
else
return 3;
}
// if planes[0] != planes[1]
else{
if(planes[0] < planes[1]
return 0;
else
return 1;
}
}
else if(A > B)
{
// for B < A
if(planes[4]==planes[5]) {
if(planes[6] < planes[7])
return 6;
else
return 7;
}
// if planes[4] != planes[5]
else{
if(planes[4] < planes[5]
return 4;
else
return 5;
}
}
//function body closes
}
int main() {
float planes[12] = {1000, 1000, 1000, 1000, 1000, 1000, 950, 1000, 1000, 1000, 1000, 1000};
int lighterIndex = findLighterPlane(planes, 12);
printf("The lighter plane is at index: %d\n", lighterIndex);
return 0;
}
In this approach, we get the right answer of the problem statement i.e. lighterIndex is 6 but in a fewer steps of comparisons which are 3 if statement comparisons. While, in brute force approach , steps of comparisons are more as in that approach , we visit all the indexes of planes[] array. Thus, divide and conquer approach which has more lines of code is suprisingly a better approach.
Conclusion
The better approach among the two approaches is the code with more lines of code i.e. the second approach. The second approach i.e. divide and conquer is an efficient approach. So, based on efficiency factor, that is an important factor , we can say that it is the best approach. But now the question is how we know that this has greater efficiency ? The answer to this question is by knowing the time complexity of both codes.
Time Complexity :-
It is a measure of running time of a program as the input size of the program grows. It can be used to compare efficiency and performance of programs.
If we generalize , the above two codes then the brute force approach takes O(n) time. And the divide and conquer approach takes O(log n) time. Thus, the second approach is an efficient approach.
Thus, Time Complexity make it easy to choose between 2 lines or 20 lines of code to solve the same problem as we will always choose an efficient approach.