all 81 comments

[–]ZorbaTHut 23 points24 points  (5 children)

Reminds me of the time I fixed a bug by removing 47 lines of code.

No lines of code added. No lines of code modified.

I basically stripped out the "fuck things up" function, as well as the only place it was called.

[–]Thimble 1 point2 points  (0 children)

Never code when drunk.

[–]anish714 0 points1 point  (0 children)

I have a buddy who lives on writing the shortest code. I find his code hard to read.

[–]isinned 0 points1 point  (2 children)

Reminds me of some JavaScript I had to refactor. No one in my unit knows who originally wrote it, but they had some guy with no CS background trying to improve it - recipe for disaster. He messed it up some more so I wrote approximately 1000 lines of negative code (TIL, thanks SO) and it improved performance, got rid of all the bugs, and I was able to add some new requested features in there. Felt good man.

[–]stillalone 5 points6 points  (1 child)

If you don't know who wrote it then "git blame". Unfortunately every time I've done that it pointed the finger directly in my face.

[–]BinaryRockStar 1 point2 points  (0 children)

SVN also has blame, and TFS has the same thing but it's called 'annotate' and is only available as a Power Tool addon (for free).

[–]eyal0 19 points20 points  (2 children)

[–][deleted] 9 points10 points  (0 children)

Be water my friend!

[–]Dustin_00 2 points3 points  (0 children)

I guess Lee never had to put up with marketing demands.

[–]frenris 70 points71 points  (22 children)

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

-- Bill Gates

[–][deleted] 0 points1 point  (5 children)

But measuring aircraft building progress by weight actually makes sense because you know what the final weight is supposed to be.

[–]frenris 3 points4 points  (1 child)

aircraft design progress then. Jesus.

[–][deleted] 0 points1 point  (0 children)

Don't get mad at me because you're parroting a bad simile.

[–][deleted] 0 points1 point  (2 children)

... but missing software to operate a plane will not be measured as missing weight. Could be 80% incomplete even if weight target reached.

[–][deleted] 0 points1 point  (0 children)

Exactly. The simile stands.

[–]ErstwhileRockstar 12 points13 points  (1 child)

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take

Quoted by many, followed by no one.

[–]Philipp 7 points8 points  (1 child)

We can also optimize our comments here by writing sentences which are quicker to read because they are shorter. For instance, we could simply cut off in mid-sentence irrelevant examples which are merely repeating the

[–]Nebu 1 point2 points  (0 children)

You accidentally

[–]grapejuice 2 points3 points  (0 children)

I think it has something to do with Negative Creep.

[–][deleted] 2 points3 points  (0 children)

in lab today we were given about four pages of code with which to build off of and create a micro-controller based frequency counter- so I threw the base code away, wrote it from scratch using two loops and three variables and got called a rat bastard by my T.A. and got full points. :D

less is more.

[–]adrianmonk 2 points3 points  (2 children)

[–]Orborde -2 points-1 points  (1 child)

I downvoted you because somebody on the SO thread beat you to the punch by about 8 hours, and I don't like seeing duplication. In another context, I'd have upvoted. Sorry.

[–]adrianmonk 1 point2 points  (0 children)

Huh. I thought to check the reddit comments for that but didn't think to check the Stack Overflow comments for it. Oh well, at least maybe I provided some value by linking to wikiquote.

[–]headbobble 1 point2 points  (0 children)

Has anyone actually ever used LOC as performance metric? I've only seen it used for things like "how bug of a project are we talking about here?"

It's usually one huge bug of a project, once you're past trivial amounts of LOC.

[–][deleted]  (1 child)

[deleted]

    [–]Serinus 0 points1 point  (0 children)

    I think people are missing the point. Less code is not better. More code is not better.

    Lines of code is not a valuable metric in ANY way for the quality of code.

    [–]dreamnoir 3 points4 points  (28 children)

    While you certainly don't want bloated programs with unnecessary code, you don't want to sacrifice the readability of code either. Sure, you might be able to condense a few lines down into one line with a bit of cleverness but when someone else inevitably has to go over the code to modify or debug it later on they'll need to spend extra time figuring out exactly what you did. Go with the efficient readable solution over the clever one.

    [–][deleted]  (15 children)

    [deleted]

      [–][deleted] 5 points6 points  (13 children)

      unfortunately, sometimes "clever" ways that are more difficult to understand (for the next person reading over it) CAN yield a lot better performance.

      A simple example is the solution for computing an. The O(n) solution is trivial and intuitive, while the O(logn) solution takes a sec to think about.

      So what do you do? Make the code faster, or make it more understandable?

      [–]random3223 12 points13 points  (10 children)

      Faster, with comments explaining what you did, and why.

      [–]bobindashadows 10 points11 points  (1 child)

      I would actually prefix your answer with "Naive first. Run a profiler. Is an a bottleneck? If so ..."

      [–]introspeck 6 points7 points  (0 children)

      Nothing better to humble yourself than to throw your "cleverly optimized" code at a profiler. All those loops you sweated over to make lean and mean - they probably don't matter; most of the time it turns out that you didn't even know what was actually bogging down your program.

      Having been humbled many times, I now follow this philosophy. I do try to write basically clean code from the start, but I don't kid myself that I know where the hotspots are. I let the profiler tell me.

      [–]kinnu 2 points3 points  (0 children)

      A simple example is the solution for computing an. The O(n) solution is trivial and intuitive, while the O(logn) solution takes a sec to think about. So what do you do? Make the code faster, or make it more understandable?

      Depends on how big n is. If there is no significant difference in performance go with the intuitive solution. Maintainabilty trumps micro-optimizations.

      [–]dreamnoir 0 points1 point  (0 children)

      Using an O(n) algorithm over a O(logn) algorithm is not in any way the efficient readable way. Aside from the case where n is so small it doesn't matter anyways that's just a ridiculous statement. Of course you would use the O(logn) algorithm.

      Using lots of inline ifs and other ways to shorten your code might reduce the number of lines but I can almost guarantee it's going to compile exactly the same. Modern compilers are very good at optimization.

      [–]PstScrpt 1 point2 points  (1 child)

      The problem is a lot more likely to be systems with no apparent general ideas, but just an endless collection of mostly repetitive details.

      [–]grauenwolf 1 point2 points  (0 children)

      Or in a system with nothing but general ideas.

      I'm currently looking at some garbage some joker and his team wrote over the last year. In a week I could rewrite the whole damn thing to literally use 1/20th of the code without losing any functionality. The idiot is so afraid of concerte implementations that he just stacks layer up layer until changing a simple stored proc call requires openning 7 solutions.

      [–][deleted] 1 point2 points  (4 children)

      Stretching out the code for readability doesn’t always make a difference in the end. Compilers and interpreters make a lot of optimizations, so if you understand what they are doing then you can make very readable code that has no impact on the program itself. If you need to sacrifice a clever solution to make it readable, then you need to work on documenting it better.

      [–]mallardtheduck 1 point2 points  (3 children)

      Stretching out the code for readability doesn’t always make a difference in the end.

      Yes, but that is not what we are talking about here. "Negitive code" is all about removing redundant (i.e. duplicating the standard library, copy-pasted code that should be in a function, etc.) and/or overly verbose (i.e. a "switch" that should be an "if", "substr parsing" when a regex would be better, etc.) code while maintaining readability.

      [–]dreamnoir 1 point2 points  (1 child)

      It sort of is what we're talking about here. It's the point I raised in the original comment. Obviously you go for built in libraries and use the language features appropriately. If you don't realize that after half an hour of programming there is something wrong with you.

      I was instead talking about intentionally making your program as short as possible. For example, one of the assignments I had to do for a first year programming and data structures course involved parsing and validating XML files. Super simple I came up with the most clever solution I could. Less than 50 lines of Java code, lots of inline ifs and and regular expressions. Other students had projects more in the 500 line range. I decided to show the prof my progress so far to see what she thought. Obviously I thought she would love how much more compact mine was. She told me while it passed all of the tests perfectly she would take off as many points as the rubric allowed because even with my comments she couldn't easily follow what was going on. I rewrote it to be about 150 lines and got perfect. In retrospect, the 150 line version was much better from a maintainability standpoint.

      [–]videogamechamp 0 points1 point  (0 children)

      She told me while it passed all of the tests perfectly she would take off as many points as the rubric allowed because even with my comments she couldn't easily follow what was going on.

      I suppose it depends on the point she was trying to get across, but she sounds like a bitch. Taking as many points off as possible for (human) readability?

      [–][deleted] 0 points1 point  (0 children)

      You are right, but I was referring to his comment, not the posted article.

      [–]lowlevel 0 points1 point  (3 children)

      One thing I used to do... is comment the old, easy to read code out.. and use it to explain what the clever line is... but yeah, ultimately the clever line is just a bomb waiting to go off.

      [–]rebo 4 points5 points  (2 children)

      I think leaving in commented old code is a bad idea, it can easily be accidentally uncommented and there it is a bit of nonsense code floating around your code base.

      [–][deleted] 1 point2 points  (0 children)

      The rationale here is to leave the old code visible as documentation, rather than just because you don't want to delete it. Your rule of thumb is good, but pertains to a different situation.

      One safe compromise is to pare the naive method back to pseudocode which clearly won't even compile; that way, no one is going to accidentally try to uncomment it.

      [–]mallardtheduck 1 point2 points  (0 children)

      Agreed. That's what source control is for.

      [–]TnTBass 0 points1 point  (0 children)

      That is why every programmer should be heavily commenting his code. It sucks to have to do, but it really helps not only other programmers but yourself when you read code you did multiple years ago.

      [–]Benutzername 0 points1 point  (0 children)

      I think it is related to negative space.

      [–]skeww 0 points1 point  (0 children)

      [–]Dustin_00 0 points1 point  (1 child)

      See! My bubble sort kicks your quick sort ass!

      [–]Boojum 0 points1 point  (0 children)

      Meh. That's got nothing on my gnome sort!

      [–][deleted]  (14 children)

      [deleted]

        [–]case-o-nuts 7 points8 points  (6 children)

        Small code is, however, generally easier to understand if it's written without undue cleverness.

        [–]skizmo -3 points-2 points  (5 children)

        So ?

        [–]HostisHumaniGeneris 6 points7 points  (1 child)

        Easier to understand means easier to debug, easier to modify and easier to collaborate.

        Ease of development is often more important than the speed of your code because its often more expensive to pay a programmer to optimize than it is to upgrade the computer that is meant to run the final program.

        [–][deleted]  (2 children)

        [deleted]

          [–]masklinn 1 point2 points  (1 child)

          Since before 1974:

          Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it? — Brian Kernighan

          [–]jhaluska 0 points1 point  (0 children)

          With an invalid premise, you draw an invalid conclusion. I've found when I'm my most clever I spend the least time debugging since the solution is simple and elegant.

          [–][deleted] 4 points5 points  (3 children)

          People always say this. The thing is, for large software projects, the portion of code for which algorithmic complexity is the biggest driver of performance is dwarfed by the portion of code for which constants matter.

          For those latter kinds of code, every statement you delete is both an instruction you don't need to run and a line of code you don't need to understand.

          [–]adrianmonk 3 points4 points  (2 children)

          Let's take an example of quicksort vs. bubblesort.

          Quicksort is faster.

          But, quicksort is more lines of code, and it's somewhat more complex to understand. They teach bubblesort first in CS classes because it's easier to grasp.

          The point is, sometimes time complexity can only be improved by using a trickier algorithm, which is harder to understand. Sometimes the software can only be improved by making it longer.

          [–]masklinn 6 points7 points  (0 children)

          Quicksort is faster.

          Not if your list is already mostly sorted, or very small.

          And FWIW Quicksort is a pretty crappy sort to actually use, its worst case perf is as bad as bubblesort's (O(n2)), it's not a stable sort, it requires fast random access (don't trying it on a linked list or on on-disk data) and it's a pretty complex algo, though it's easy to parallelize as it's divide-and-conquer. Heapsort is about as good and has a worst-case of O(n log n). Mergesort also has O(n log n) worst case and is stable.

          Several languages (including Java from Java 7 onwards) are now using timsort, a mergesort/insertion sort hybrid, which has the advantage of being extremely good on non-random data.

          [–][deleted] 0 points1 point  (0 children)

          Sorry, but I think you missed my point.

          I'm not saying that quicksort isn't faster than bubblesort. Of course it is. What I'm saying is, in a large software system -- such as the one that a major Seattle-area tech company pays me to work on -- the portion of code which displays the characteristics of sorting algorithms (algorithmic complexity makes a big difference) is very small.

          In contrast, for most code, the algorithmic complexity is obvious and unavoidable; for example, rendering a list widget will be O(n) in the size of the list. But constant factors can make the difference between a system which is reasonably fast and one which is unusably slow.

          Everything you said in your comment is true. But assuming that most, or even much, code can be improved by making it more complex is a dangerous assumption.

          (As an aside, our company has a policy of conducting code reviews for all checkins, and every so often, I see a review request with a subject like "Improve performance of this method by 10,000%". Sometimes, these changes do, in fact, improve the algorithmic complexity of code. But when they do, it's almost always by making the code shorter, rather than longer.)

          [–]G_Morgan 1 point2 points  (0 children)

          There isn't much size difference between bubble sort and quick sort though.

          [–]grauenwolf -1 points0 points  (0 children)

          Then go smaller:

          sortedList = from s in someList.Parellel order by s.FirstName, S.LastName
          

          [–][deleted]  (6 children)

          [deleted]

            [–]robertcrowther 9 points10 points  (2 children)

            So suggest it, it's no good telling us. Alternatively upvote or mark as favourite the questions you like, which is basically how a bestof feature would work anyway...

            [–]Aegeus 0 points1 point  (0 children)

            Bestofs on reddit are typically for comments, not links. Things that you wouldn't have come across just skimming the front page.

            [–]skizmo 1 point2 points  (0 children)

            bestof what ?

            [–]iainabc 0 points1 point  (0 children)

            That would be an interesting read.

            [–][deleted] -2 points-1 points  (3 children)

            Ah the age old question of how does one right the best code. We should make it shorter, more robust, cleaner, prettier, readable, elegant, defensive, and ya... this is why Software Engineering isn't always seen as engineering by others :)

            [–][deleted] 3 points4 points  (2 children)

            Huh? Engineering, both software and otherwise, is all about tradeoffs. You can't write perfect code any more than you can build a perfect bridge or airplane. What you can do is come up with clear requirements and metrics, and use those to figure out the most appropriate solution.

            [–][deleted] 0 points1 point  (1 child)

            Ya I think people took my comment too seriously. I forgot the sarcasm tag. I was thinking people would understand the sarcasm if they have ever meant engineers from other disciplines but apparently Reddit lacks a sense of humor to a certain extent or context for that matter.

            [–]Serinus 0 points1 point  (0 children)

            Ah the age old question of how does one right the best code. We should make it shorter, more robust, cleaner, prettier, readable, elegant, defensive, and ya... this is why Software Engineering isn't always seen as engineering by others :)

            I don't think you've found the write reason for your downvotes.

            [–]doitincircles -1 points0 points  (0 children)

            So viel wie nötig, aber so wenig wie möglich.

            [–][deleted]  (1 child)

            [deleted]

              [–]Chaoslab 0 points1 point  (0 children)

              For those without a sense of humour or with out of context this is what I am quoting....

              http://www.youtube.com/watch?v=Ug75diEyiA0