0
Is there any single right way of coding?
Some coders prefer shorter and more cryptic methods which are confusing to some whilst others prefer longer and more clearer methods of coding. How does short or long code affect the execution of an app, and also in terms of code maintenance?
7 Answers
+ 2
It really depends on what the code is.
No, longer codes are not automatically going to take longer to execute, we would need to compare the algorithm complexity to determine that. Shorter codes can easily take longer.
As far as maintenance goes, I'd say the same thing.
Depends. Concise code can just be commented.
Or, if it really starts to hinder readability, then you may want to space it out more or use methods to make it clearer.
Ex/
x++ % 10;
// keep x in range of 0 to 10
instead of:
x++;
if(x > 10)
x = 0;
+ 2
Yea it could be preference.
I find the % 10 portion easier.
When it comes to algorithms like linear search vs binary search, binary may be a bit longer but is incredibly faster (Assuming array is sorted). So, I suppose for big things it matters (Can be shorter or longer) but small things like my previous example are more preference.
You should avoid repetitive code though.
Repetitive Ex/
for(int i = 0; i < someArray.length; i++)
System.out.println(someArray[i]);
someArray[0] = 5;
someArray[someArray.length - 1] = 12;
for(int i = 0; i < someArray.length; i++)
System.out.println(someArray[i]);
With Methods:
printArray(someArray);
someArray[0] = 5;
someArray[someArray.length - 1] = 12;
printArray(someArray);
// where printArray is the for loop code
It may not be to much of a difference here, but if we simply add a few more lines of code in the for loop it quickly becomes apparent.
+ 1
Dude its simple the short ones with complex lines take less time to execute but its harder to other coders to understand/maintance. And the long blocks of code take more time to execute but its easier for Humans to read so easier to mantaince!
+ 1
Faith, thanks for the explanation, but I find the "instead of: " portion more clearer and easier to understand. If I had to maintain code then the latter would make more sense unlike the prior. Is it safe to say that it all boils down to preference?
0
Yes code keep it simple dont worry about complex nonsense. Simpler the better will give you less headaches later down the road.
0
André, we have fast processors today, so would one really notice any performance issues with long lines of code as opposed to short ones?
0
Anthony, are you an industry professional? If so, do you go the long or short route?