How important is it to reduce the number of lines in code?

Talk about your skill passion! Ask questions, get feedback, post something cool!

Moderator: samw3

User avatar
CDNAdmin
Site Admin
Posts: 137
Joined: Sat Dec 15, 2007 4:21 pm

How important is it to reduce the number of lines in code?

Postby CDNAdmin » Sun Apr 07, 2013 10:56 pm

Ars has an interesting article up on minimizing code:

Ankit works in J2SE (core java). During code reviews, he's frequently asked to reduce his lines of code (LOC). "It's not about removing redundant code," he writes. To his colleagues, "it's about following a style." Style over substance. Ankit says the readability of his code is suffering due to the dogmatic demands of his code reviewers. So how to find the right balance of brevity and readability?

Erik Dietrich writes:
I agree with your code reviewers, but with an asterisk. Each statement that you write in your code is a technical liability—it's a potential failure point. If you write a method with 10 statements and your coworker writes one that achieves the same functionality with 5 statements, his is likely to be 'better' as measured by likelihood of issues (there are twice as many places your code can be wrong, overly complex, or problematic).

Xion writes:
I currently work as a senior applications developer and project business analyst for a major company and never has the line count of my development been a center of interest. However, I believe that the more condensed a code can be, the better, BUT not at the cost of being able to quickly analyze and correct (or add on to) it. To me, when you are in charge of a business critical application that MUST have a vast level of scalability and be capable of on the fly alterations in a non-stop changing environment, concise, easy to read code is one of the most important elements in development.

Click here to read the whole thing.

What are your guys thoughts on the topic?
User avatar
Mene-Mene
TALKer!
Posts: 2760
Joined: Sun Dec 16, 2007 7:10 am
Location: Indiana, United States

Re: How important is it to reduce the number of lines in code?

Postby Mene-Mene » Wed Apr 10, 2013 3:41 pm

Great article and topic. I've come to think that the only purpose that SLOC as a metric serves is as an anti-pattern. When I was a younger programmer I used to get excited about 300 and 400 lines applications, then it became a few thousand lines of code. Why? Because I used it as a measurement of whether or not my code was doing something. I'm come to realize that how many lines of code it takes you to get there isn't a good measurement (good or bad) of how well you got there. What's important is how long it takes someone else to look at your code and understand what it's doing correctly.

One aside that can be really helpful, though not nearly as important, is to reduce the number of lines you can mess up on, and the amount of code you have to read to understand a system. As for what that means in practicality, I think that depends on the coder phase you're in. (pattern abuse, function pointers, recursion abuse, anti-recursion)
M^2 out-
It's Time to get Terminal!
achild
Posts: 308
Joined: Wed Jul 01, 2009 8:42 am
Location: McKees Rocks, PA

Re: How important is it to reduce the number of lines in code?

Postby achild » Fri Apr 12, 2013 8:16 am

Me too MeneMene.

I would only add to the article that sometimes solid code that has been debugged, tested, and fixed through usage can have what seem like excess LoC, but is really necessary for various side cases etc. Esp. low level critical stuff.
User avatar
ArchAngel
Posts: 1534
Joined: Sun Dec 16, 2007 12:23 am

Re: How important is it to reduce the number of lines in code?

Postby ArchAngel » Fri Apr 12, 2013 10:28 am

Reducing lines of code for the sake of reducing lines of code is inane and counter-effective.

Now, I've seen my fair share of code monstrosities. Functions spanning over 1k lines. Pages over 10k. It's pretty bad, but the problem isn't the amount of lines, it's the poor architecture. That code needs major refactoring.
If a method, say, gets over 150 lines, there's a good chance that you need to refator that into smaller methods. A function or method should have one conceivable purpose that is clearly defined by it's title.
I'm a firm supporter in the readability of code: I find documentation almost always unnecessary, and it's prone to becoming outdated or just a waste of time. A good coder should be able to read code easily, and if the code isn't easily read, it needs to be reworked. After this, the amount of lines should generally fall into place. At best, a large number of lines is a warning for a bloated function or class.
Q.E.D.
TheLittleSettler
Posts: 120
Joined: Fri Nov 19, 2010 10:08 pm

Re: How important is it to reduce the number of lines in code?

Postby TheLittleSettler » Fri Apr 12, 2013 11:39 pm

Here here ArchAngel!

I read in a book about concepts of languages a story someone said back in the sixties about that kind of thing at the first international meeting to find out how to improve coding productivity. From what I recall:

Wanting to make and sell paintings at an industrial level, a few dozen artists were hired. Not satisfied with the slow productivity, they issued larger brushes so the paint could be applied more quickly. They also promoted their best employee (a certain L. Da Vinci) to managerial. Still not satisfied, they tracked the number of strokes each painter made per day.

The story was excluded from the official record of the meeting, but later surfaced.
User avatar
PFC
Posts: 633
Joined: Sun Dec 16, 2007 5:57 pm
Location: Canada

Re: How important is it to reduce the number of lines in cod

Postby PFC » Fri Nov 08, 2013 9:47 am

Ideal line count should depend on the language and the programming style used. Generally if I need to quickly add functionality to a module I simply start writing code in a sequential order that does (mostly)everything I need usually followed by some quick testing. This usually ends up with really long and inefficient code, but it works and anyone can read it. Once the code works, I usually go back and organize it into class/functions/modules, test it, do any performance tweaks needed and then add comments/doc-strings anywhere where code functionality isn't obvious. Generally this results in more efficient code, lower line count, better readability and maintainability.

So the short answer: lower line count is ideal so long as it doesn't affect performance, doesn't sacrifice code organization(class/functions), doesn't result in the removal of proper commenting, and so long as multi-statement lines are not abused.

There generally are some tradeoffs between performance and readability, but that's another topic.

Who is online

Users browsing this forum: No registered users and 9 guests

cron