Tuna has asked for the wisdom of the Perl Monks concerning the following question:

I just finished a program for my boss, and his only comments were along the lines of "you could do this in fewer lines". My thought as I was writing was, sure, I could have written it in fewer lines (it's about 300 lines), but (especially as a contractor) I wanted it to be easily readable and maintainable. Where do YOU draw the line between programmer efficiency and readability?

Replies are listed 'Best First'.
Re: Code for readablity or fewer lines?
by footpad (Abbot) on Feb 27, 2001 at 21:42 UTC
    In code that may be maintained in the future, whether by myself or another, I will nearly always opt for readability. To ensure that I follow the best practices in production code, I follow my personal standards even in throw away code--primarily because I've found that I often take incorporate those bits into larger or later projects.

    In my experience, line reduction is not a good benchmark for code quality. That's not to say that you shouldn't look for tighter ways to produce your code, but you should do judiciously. Obfuscation is fine for training or entertainment purposes, but it shouldn't be used in production.

    I'm a member of the over documentation school, primarily because I have a memory like a steel sieve. If I'm not sure I'll remember why I chose a certain strategy in a week, a month, or a year, I write it down. If it's gonna confuse me, it will likely confuse the person that replaces me.

    It's that simple. We're always decrying the lack or quality of documentation with the tools we use. Why, then, would we deliberately contribute to the problem in the tools we write?

    For a better treatise on the subject, consider Code Complete. While it might be considered a bit dated by some and the examples are in C, the principles discussed are ones that every professional programmer/developer/anaylst should at least consider. (Also, if you pick up a copy by clicking that link, our tireless leader will get a small kickback.)

    --f
      I'll second that footpad, coding for clarity is best. People time is more expensive than machine time, and the divergence between the two gets wider every day.

      Optimization is good to the point where code starts to lose clarity. Unless the application has to be really optimized. It optimization is the case, then add copius comments so when you or someone else comes back a year later to enhance or fix it - again people time.

      I also agree, that I like to over-comment. A year from now I want to know what I was thinking when I go back and look at my code. Besides, comments don't add anything to the actual executable, they are discarded at compile time.

      Mike - mps@discomsys.com

      "The two most common elements in the universe are hydrogen... and stupidity."
      Harlan Ellison

        People time is more valuble than machine time.
        hehe ... that seems to be the driving motto of perl... :)

        if we were all hell-bent on maximum performance, most of us wouldnt be using perl at all but rather assembly or some language of that sort.

        i agree with you, readability almost always wins out over line count in my programs, however, dont mistake line count for efficiency line count doesnt always indicate a slower/worse script... sometimes adding a few lines into a script can substantially speed it up.
      "I'm a member of the over documentation school, primarily because I have a memory like a steel sieve. If I'm not sure I'll remember why I chose a certain strategy in a week, a month, or a year, I write it down. If it's gonna confuse me, it will likely confuse the person that replaces me"

      This rings especially true for me, as I have about a month's worth of programming experience. I am quite certain that I could've done things like use fewer variables and used $key for every single hash key, etc. I chose to use contextual variable names so, I, the inexperienced author, could go back, read, and understand what I have written! For example, $private_file(@private_list) and $public_file(@public_list). Maybe I've gone a little overboard, but my assumption was that someone with little experience could very well end up with this code in their lap. Thanks everyone your perspectives.
Re: Code for readablity or fewer lines?
by japhy (Canon) on Feb 27, 2001 at 22:18 UTC
    I advocate brevity and conciseness in my code, as well as using idioms and any little efficiency-fiend out there. (This is not to say my code is not readable or maintainable -- I just choose to code in a form that is understandable and brief.) As such, I use tr/// for character deletions instead of some s///g. I use tricks where they save time and effort. For my job, we keep information in a form of XML in files. When I need to go through the file and change a section, I don't use regexes. I use $/:
    sub updateRecord { my ($srml_file,$n,$new) = @_; my ($offset, $data, $post); local $/; # no concurrent access, so no locking open CACHE, "+< $srml_file" or return setError($!); $/ = "<!-- START RECORD $n -->\n"; <CACHE>; # get up to the good stuff $offset = tell CACHE; $/ = "<!-- END RECORD $n -->\n"; <CACHE>; # waste what's there undef $/; $post = <CACHE>; # get what's left seek CACHE, $offset, 0; truncate CACHE, $offset; # in case new data is shorter print CACHE "<!-- START RECORD $n -->\n", getSRML($new), "<!-- END RECORD $n -->\n", $post; close CACHE; return -s $srml_file; }
    Before I introduced this trick, they'd been reading through the file line-by-line, and using regexes (without using qr// objects) which got a bit expensive. So this was a speed boost, and the brief comments are all that was needed to explain the process.

    The measure of efficiency comes not from the number of lines used, but from the algorithms used. Redundancy can occur in small programs as well as large ones.

    japhy -- Perl and Regex Hacker

(dws)Re: Code for readablity or fewer lines?
by dws (Chancellor) on Feb 27, 2001 at 23:20 UTC
    When figuring out what "the right amount of code" is, you need to give the people who are liable to pick it up some credit for being intelligent, but not too much. That's a delicate balance. I think of it kind of like this:
    M/R| _*_(just right) i | / \ l | / \ i | / \ t | / \ y | *(golf) *(russian novel) +---------------- lines of code
    Too little code and maintainability/readability is lost. People will burn up project time puzzling over a marvelous little snipped of Perl Golf, and will eventually chuck it and rewrite it.

    Too much code and maintainability/readability is lost. People won't be able to see through the clutter to get at what the code is doing. Or worse, they'll "fix" it correctly, but won't bother updating the shelf-feet of commentary that's cluttering the code up, and maintainability goes down.

    Hit the sweet spot, and the code will stay viable and maintainable (or at least stands a good chance of staying that way).

    Having done this for a few years decades now, I've found that that code in the sweet spot has a couple of characteristics that are almost language independent.

    1. Short methods/subroutines, named for what the method does, not how it does it.
    2. Commentary, if any, at the top of a method describes the what, but not the how (unless the how is necesarrily cryptic).
    3. Commentary within a method is sparce. (If you find yourself needing to write a lot of commentary inside of a method, that's a big clue that you need to rewrite or refactor.)
    4. Loops are nested no more than two deep, and don't span more than a screen If you can't visually spot the top and bottom of the outter loop on a normal display, refactor.
    5. Consistent indenting, using logical tabs. (Physical tabs are problematic, especially if people have their editors set up differently.)
    6. A consistent philosophy for handling errors. The line between when to throw an exception vs. when to propogate an error code must be well drawn.
    7. Appropriate, configurable logging, particularly in bigger system with lots of hands on the code. A good log message is a signpost. It helps people find there way around the system, and doubles as commentary.
Re: Code for readablity or fewer lines?
by danger (Priest) on Feb 27, 2001 at 21:54 UTC

    For myself, programmer efficiency and readability are on the same side of the coin (if more lines makes it more readable and understandable, I'll spend less time maintaining it later). However, as you might note in this post, sometimes I find making something more readable to my eyes also shortens the code as well (in that case, reducing the number of lines in a loop by half was a side-effect of reducing the level of nested conditionals).

Re (tilly) 1: Code for readablity or fewer lines?
by tilly (Archbishop) on Feb 27, 2001 at 22:21 UTC
    Programmer efficiency and readability are different?

    I am oddly reminded of an early thread I was in here at Unique & sorted array optimization?. IMHO shorter is not the goal, it is a result.

    For thoughts about a longer (real life) example of that, one of my first posts here was a favorite story of mine from undergrad: The path to mastery.

Re: Code for readablity or fewer lines?
by mirod (Canon) on Feb 27, 2001 at 22:21 UTC

    This really reminds me of the comment in Amadeus: "too many notes".

    Seriously, why should your boss care about the number of lines? Did you complete the code in time? Does it work? Is it properly documented and tested? Can it be maintained easily? Those are the important questions.

    Now if he doesn't like your coding style maybe he should sit down with you and do a code review, pointing the constructs that he thinks would be improved by being shorter This would give you a chance to learn but also to justify why you chose a more line-consuming method. Then your next project would be better.

    But frankly, 300 lines is really a small project and I have no idea why anybody would care. The only reasons I can think of is that either your boss knows that there is a module around that would have cut your code to 20 lines or that he needs to show his boss that Perl is really powerful (in which case it should have let you know in advance).

(ichimunki) re: Code for readablity or fewer lines?
by ichimunki (Priest) on Feb 27, 2001 at 22:30 UTC
    Without seeing the code in question, we really can't answer this. Have you, for the supposed sake of readability, decided not to code certain abstract cases as abstract cases, instead enumerating over them instance by instance? This is one case where fewer lines may actually make the code easier to maintain.

    Is the program 300 straight lines of code, or have you taken the opportunity to group related functions into subroutines so that the main body of the program is short and sweet, relying on the functions to fill in the details?

    Do you use idiomatic and straight-forward Perl, or is it longer because you are trying to clarify kludgy methods?

    Of course, I'm only playing devil's advocate here. The odds are someone who thinks line count is a significant measure of anything (especially with respect to Perl) has no clue what else to say. It is also possible that said boss is under the impression that one liners and JAPHs are the be-all-end-all of Perl mastery and that code that doesn't look like that is somehow less worthy. If it doesn't look like a cool hack it must not be. Personally, I think that's a dangerous mentality.
Re: Code for readablity or fewer lines?
by TheoPetersen (Priest) on Feb 27, 2001 at 21:46 UTC
    I would have different responses to this, depending on whether the person making the comment knows Perl well or not.

    If your boss knows the language, you have something to defend, since you want good recommendations and repeat business. Point out that you are writing code at the level of those most likely to read it, good maintainable code that doesn't require a knowledge of the choicest idioms to understand.

    If on the other hand he's somehow suggesting you are padding the job by writing bulky code, tell him the same thing but add that you can tighten up the work in further assignments, now that you understand the coding practices better.

    In either case you've done the write thing, as you seem to know already.

    The line between efficiency and readability depends on the function of the code and the person who has to read it next. I write example scripts differently than profound object code that only I'm likely to read. Both make me happy, if they serve both function and audience well.

(jeffa) Re: Code for readablity or fewer lines?
by jeffa (Bishop) on Feb 27, 2001 at 22:30 UTC
    Well, as I sit here and churn through some legacy code that was placed in my lap - I mutter this mantra over and over:

    "Show me your flowcharts and conceal your tables and I will remain mystified; show me your tables, and I won't need your flowcharts, because they will be obvious."
    St. Brooks

    I think this has a lot to say about maintainability, and what is most important about documentation. Why? Because an understanding of WHAT the program is doing is so much more important in the first stages of handing a program to somebody. After that comes the details - and they should be fairly clear with a complete understanding of what the program is specified to do (barring creature feep).

    Most of my comments are based on this - I strive to make my code understandable, but only after I am sure that it is correct and efficient.

    "You can do this in fewer lines . . ."
    Well, sure - but does it improve the program? Is it faster? Does it use less resources? Is it more modular? Is it more cohesive? Is it loosely coupled enough?

    And the number one question: who is going to have to take care of this when you are done with it?

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Code for readablity or fewer lines?
by sierrathedog04 (Hermit) on Feb 27, 2001 at 23:38 UTC
    I was a boss once, briefly.

    The boss who said "you could do this in fewer lines" probably wants to say something but doesn't know what to say.

    Of course you could do this in fewer lines. You could probably do it in one line and enter it in the Obfuscated Perl contest. So what?

    I wouldn't worry about your boss's comment too much. It probably just means that he didn't see anything at all wrong with your code.

      Laughing...
      Does your boss have pointy hair? nooo nevermind...
      I would have to agree with sierrathedog04 in the fact that the boss just had to say something authoritative to remind him/herself that they were still boss.

      Back to the matter on hand, Code for readability. This helps those following in your footprints and ensures that your code will be used in the future rather than being scrapped because the next person couldn't understand it. It also will prove it was your work when you use that job as a reference.

      puts the chalk down and walks away from the board, dusting off his hands

Re: Code for readablity or fewer lines?
by davemabe (Monk) on Feb 27, 2001 at 22:19 UTC
    There was a good article somewhere on "How to be a really bad programmer" or some similar title that addressed this issue. One of the ways to be a bad programmer was never sacrifice white space for readibility. It seems like it was on slashdot a couple years ago. I can't seem to find it. It was actually very funny and informative. Does anyone know what I am talking about?

    Dave