+ 16
Long vs Short Codes
Am I approaching coding wrong? Someone once looked at a simple 3 line code I had made and complained about how short it was. I personally think why write 10 lines when you can get the same result with 2, but am I wrong? What's the best practice?
15 Answers
+ 18
This is a really good question. Quality of code not equals Length of code.
For example:
Normally a fibonacci algorithm implemented recursive is
shorter (and more readeble) than Iterative fibonacci, But Iterative Fibonacci is more memory efficienty than recursive version.
Another example:
A lot of code's have thousands lines and thousands of lines.
But what counts in the end is the quality of code lines. This thousands lines
can have a lot of bugs or just be a non-smart implementation.
So in the end Length of code means nothing.
The best practice is if you need to write more lines to be more efficiently just
go ahead. In the end just ignore the size of your code. Just try to
code smarter.
Edit: Just want to thanks everyone who upvotes and read what i have to say. I acquired Great Answer Badge. So i still will try to work harder to provide my knowlodge and give better answers
+ 17
Another point would be readability.
Fewer lines means easier reading, right? Well, not if one line is a lambda monster of 200 letters. ;-)
If you can lose lines of code without losing anything (performance, functionality etc.), it should be good riddance.
EDIT: I actually have an example for that lines thing. Two versions, first in several lines, second in one. Which one is easier to read? I opt for the longer version.
https://code.sololearn.com/c5abg5e709nU/?ref=app
https://code.sololearn.com/c26bjhjP5V1E/?ref=app
+ 7
💧Alex Tusinean🔥 I saw his code earlier . And a can say the same thing. haha
+ 6
When you can make the same code using 3 or 10 lines for example , the one with 3 lines is better .Because if You can do a 10 lines code work using only 3 lines , that is the prove you are smarter that you think!
https://code.sololearn.com/cofKp77ihhMq/?ref=app
https://code.sololearn.com/cv4jrh1H4v2z/?ref=app
Check out my 2 versions of my new game. V1.0 is larger and it has a bigger amount of lines , and v2.0 is smaller , but better.
My code V1.0 has 47 lines and 1621 characters , and my code V2.0 has 44 lines and 1132 characters (and both are doing the same thing!)
+ 6
HonFu You are 12 times smarter that me , that is why you managed to make the same thing i did in 20 lines. (But i still like mine better than yours 👌)
Learning is the Key of succes.📚
+ 5
I don't think you are approaching coding the wrong way. It means you're smarter.
+ 5
I agree with HonFu. Readability is more important than the number of lines in your code. And also the style how you write code. To give you few examples Do you use one line when you declare more than one variable compare to separate lines for each variable. How do you use open and close brackets. There are lot of articles written about this.
+ 5
Go to the shortcuts in the code.
if you find a shortcut then it will be much easyer but advanced at the same time.
so short and big there is not a difference into both of them,
so big code you can do with small code(not in markup/HTML).
+ 4
[meta / specific interest] There are competitions amongst coders to use the fewest characters possible to solve a challenge, even if (or especially because) it's obscure.
See "code golfing" or "golf scripting".
Sample challenge forum:
https://codegolf.stackexchange.com
Definition:
https://en.m.wikipedia.org/wiki/Code_golf
+ 4
Thank you everyone, your responses have been helpful. I didn't wanna make a mistake while starting out that might have be problematic in the future
+ 3
💧Alex Tusinean🔥, on that note, take a look at this. :)
https://code.sololearn.com/cq5P97pbm7Y4/?ref=app
+ 2
depends. most of the time, less code less chance of bug/error
+ 1
less number of code lines with same functionality as expected, means you are brilliant in coding. Keep coding,try to reduce LOC as much as possible, keeping readability,commenting, indentation, etc....
+ 1
Anya Fibbonacci algo... Short code, much simpler & faster
https://code.sololearn.com/c8krUt0Rk897/?ref=app
+ 1
Ok, short code is better, (As it's my name 😂😂😂), and it should be faster or same fast, but maybe readability is the problem