Tuna has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Code for readablity or fewer lines?
by footpad (Abbot) on Feb 27, 2001 at 21:42 UTC | |
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 | [reply] |
by unixwzrd (Beadle) on Feb 27, 2001 at 22:09 UTC | |
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."
| [reply] |
by howard40 (Beadle) on Feb 28, 2001 at 02:54 UTC | |
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. | [reply] |
by Tuna (Friar) on Feb 28, 2001 at 01:10 UTC | |
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. | [reply] [d/l] [select] |
|
Re: Code for readablity or fewer lines?
by japhy (Canon) on Feb 27, 2001 at 22:18 UTC | |
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.
| [reply] [d/l] |
|
(dws)Re: Code for readablity or fewer lines?
by dws (Chancellor) on Feb 27, 2001 at 23:20 UTC | |
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
| [reply] [d/l] |
|
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). | [reply] |
|
Re (tilly) 1: Code for readablity or fewer lines?
by tilly (Archbishop) on Feb 27, 2001 at 22:21 UTC | |
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. | [reply] |
|
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). | [reply] |
|
(ichimunki) re: Code for readablity or fewer lines?
by ichimunki (Priest) on Feb 27, 2001 at 22:30 UTC | |
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. | [reply] |
|
Re: Code for readablity or fewer lines?
by TheoPetersen (Priest) on Feb 27, 2001 at 21:46 UTC | |
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. | [reply] |
|
(jeffa) Re: Code for readablity or fewer lines?
by jeffa (Bishop) on Feb 27, 2001 at 22:30 UTC | |
"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." 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 . . ." And the number one question: who is going to have to take care of this when you are done with it? Jeff
| [reply] |
|
Re: Code for readablity or fewer lines?
by sierrathedog04 (Hermit) on Feb 27, 2001 at 23:38 UTC | |
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. | [reply] |
by joee (Beadle) on Feb 28, 2001 at 00:25 UTC | |
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 | [reply] |
|
Re: Code for readablity or fewer lines?
by davemabe (Monk) on Feb 27, 2001 at 22:19 UTC | |
Dave | [reply] |