Slashdot Mirror


The P.G. Wodehouse Method of Refactoring

covertbadger notes a developer's blog entry on a novel way of judging progress in refactoring code. "Software quality tools can never completely replace the gut instinct of a developer — you might have massive test coverage, but that won't help with subjective measures such as code smells. With Wodehouse-style refactoring, we can now easily keep track of which code we are happy with, and which code we remain deeply suspicious of."

133 comments

  1. Grok it. by symbolset · · Score: 5, Insightful

    It's only 30k lines of code. This is no problem.

    First, take ownership. This is your project. Identify your resources, name the gates you must get through to succeed. If you have help make sure they understand their changes must hit the corner cases or it's junk, then give them ownership of their piece explicitly. Create a safe environment for testing changes, with forward and backward versioning.

    Define success. So many projects skip this essential step. If you cannot identify the destination you cannot tell when you've won.

    Skip the 50,000 foot view and proceed directly to "what does this do and how can it be done better"? Believe it or not flowcharts and Venn diagrams are not obsolete. Create tree views of function calls. Identify processes that should be libraried. Create policies like "maximum function call depth", "Maximum process share", etc.

    If you're the lead, look at issues like memory allocation and process management. Do your profiler due diligence.

    If you're the lone ranger on this just absorb the whole thing and integrate it. Force feed your brain huge quantities of what-ifs until it gives you the right answer in self defense - and then have somebody else check the result.

    30 days development and 60 days testing. Remember to give a nice presentation at the end and sell it!

    Good luck.

    --
    Help stamp out iliturcy.
    1. Re:Grok it. by jonaskoelker · · Score: 3, Insightful

      Skip the 50,000 foot view and proceed directly to "what does this do and how can it be done better"? While your post has many good and clearly expressed ideas, I'm not quite sure where you're driving at right here. The question "how can this be done better" can be asked at each node in the call graph, and the question is very broad.

      To ask this question near the root, for architectural purposes, I think what you want is exactly the 50 kilofoot view. There's of course utility in asking the same question closer to the leaves, but I think it's a mistake to overlook the big perspective in favour of going low-level.
    2. Re:Grok it. by blahplusplus · · Score: 3, Interesting

      "Believe it or not flowcharts and Venn diagrams are not obsolete."

      Believe it or not I use mindmapping software to help plan out the structure of a program and draw relationship lines arbitrarily, I wish someone made these mindmapping programs and made them more accessable to programs and programming.

      http://www.thebrain.com/

      Also great flowchart drawing tools:

      http://www.smartdraw.com/

    3. Re:Grok it. by Like2Byte · · Score: 4, Informative

      I totally agree. Even if you are a developer for a large, corporate, multi-vendor project - knowing how components that feed components you directly interface with will allow you to become a better developer for the project and to point out problematic architectural design issues.

      And if I hear one more project manager say, "Let's not worry about this corner case" (usually said with no idea how this is going to negatively effect the entire process tree) I'm going to punch them in the colon.

      There are two ideas of thought about corner cases (and the GP pointed out one).
      Thought #1) (GP) There's no such things as a corner. It is a requirement - it may be that fewer people/fewer processes use it; but, it is still a section of the total solution that must be designed to overcome some problematic section. Otherwise, why is the code being written?

      Thought #2) Corner cases only effect a small number of your user-base; therefore, code to satisfy 95%-99% of your customers. The underlying principle here is that the manager will wait for another release. This approach is usually taken when the project manager failed to account for something and says (and I quote), "We'll just re-design it after the first release."

      If you find yourself in an environment where #2 (hehe) permeates the thought structure of management you have few options available to you.
      a) Kindly (because wrapping your hands firmly around their neck is just not understood these days) explain to them the flaw in that kind of thinking. It usually involves educating the manager to a level they've never even considered before. Completion of this project will be long and arduous. Good luck to you.

      b) If step 'a' fails - inform management. Project Managers (in large corps) are not, usually, the final decision maker. Elevate this threat (to the project) to the PM's manager - a Director, perhaps.

      c) If you're able, move to a new project within the company where the project manager in case 'a' has no influence. I know that's not feasible in most segments.

      d) Find a new job.

      If the project is sufficiently high profile enough then recourse option 'a', above, is your only solution. Mitigate the damage by engaging the offending PM and try to keep them under thumb by sharing your expertise with them. Good luck with that brick wall. YMMV.

    4. Re:Grok it. by cbart387 · · Score: 4, Informative

      Doxygen is my favorite tool for C/C++/Java programming. It also handles some other random languages as well. Its main purpose is to create documentation (think javadoc but Open Source, handles more than just java, and better results). Here's an example of what it can do.

      Anyways, related to your post, doxygen can map out the call graph from functions and dependency/include graphs of files. It may be helpful in understanding the structure.

      --
      Lack of planning on your part does not constitute an emergency on mine.
    5. Re:Grok it. by Anonymous Coward · · Score: 2, Insightful

      moderated -1, Asshat

      If your engineer wasn't a perfectionist you would probably be a failure

    6. Re:Grok it. by Anonymous Coward · · Score: 0, Flamebait

      This thread seems to be full of asshat managers. Your job is to provide the developers with management not to guide them like sheep. Despite the shepard-like delusions you've picked up from the authority granted by your role, the programmers have insight into the project that you lack and they're generally much, much smarter than you when it comes to engineering. A good manager realizes this and listens to the developers when they say this needs to be done right and there is no good enough solution that won't result in catastrophe down the road.

      Asshat managers love to say things like, "this engineer lacks business sense" or "he's very smart but he's not business smart" - as if business smarts is a superset of, say, engineering smarts? The notion is preposterous and is nothing more than a delusion you asshat managers reinforce with each other because your narcissistic personality disorders demand superiority to your "underlings". Oh, how they put our company at risk with their refusing to do things half-assed! Listen to yourselves!

    7. Re:Grok it. by Anonymous Coward · · Score: 0

      "30 days development and 60 days testing." - You're either writing code for life-or-death systems such as airplanes, or you're living in a fantasy world.

      In my experience, if 90 days are budgeted, the estimate only allowed for 15 days of testing, and it was about 45 days short on the coding.

    8. Re:Grok it. by Anonymous Coward · · Score: 0

      Moderated -1, Broke

      Congratulations, your company is now broke because your customers will no longer foot the bill for your army of perfectionist engineers. You ran off all the people with business smarts who would have tried to assign some restraint to your overzealousness. You spent the amount your company would have dedicated to buying legislation to mandate your product and raise entry barriers to possible competition on a re-design.

      Your market has been taken over by Asshats Inc. who build an inferior product that breaks occasionally, but costs one-half of yours.

      But that's ok. You're "engineer smart". Your children are enjoying the leftover pizzas you bring home from your interim employment.

    9. Re:Grok it. by Anonymous Coward · · Score: 0

      I can see that you have a firm grip on that belief that the developers are overzealous perfectionists to the detriment of the project and the company. I'm telling you that there is nothing not obvious in whatever business genius it is that reduces all of your problems to merely restraining the engineers' overzealousness. No engineer is going to sacrifice a project for the sake of being overzealous. You see, finding compromises between doing it perfect and doing it quickly is an engineer's responsibility too.

      Somewhere you have malfunctioned and you think your job is to tell the engineers when and where to cut corners. When they say they can't because *insert technical reason that's over your head* you ignore what you don't understand and presume the engineer is a selfish, overzealous brat that is driving your company into the ground. This makes you an Asshat.

      Your hypothetical reply where the engineers have bankrupted the company makes me wonder just how fucked up your business sense is. Obviously, deciding how much money is allotted towards non-technical solutions to business problems in your space should be done independently of the budgeting for technical solutions you plan to payroll engineers to build in-house. I think any company ran by an engineer is going to be more successful than a company ran by you - not saying it takes an engineer to run a successful company, but it certainly doesn't hurt!

    10. Re:Grok it. by fractoid · · Score: 1

      And I almost forgot, he had his latest feature pulled from the last release because he couldn't figure out minimum ship and has put the latest release in danger. Yes, it'll be less problematic code than most but releasing something is better than releasing nothing. He's wiser than you. Releasing nothing will lead to customers being frustrated. Releasing something that's not ready for release will lead to your company building a well-earned reputation for shoddy work.
      --
      Rampant carbon sequestration destroyed the Dinosaurs' tropical paradise. I'm here to help repair the damage.
    11. Re:Grok it. by angel'o'sphere · · Score: 1

      Believe it or not flowcharts and Venn diagrams are not obsolete.

      Believe it or not, Venn diagrams have absolutely nothing to do with software development.
      See: http://en.wikipedia.org/wiki/Venn_diagram

      angel'o'sphere

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
  2. How can anyone turn down a refactoring story! by iluvcapra · · Score: 1

    Just burning up the comment threads on this one.

    --
    Don't blame me, I voted for Baltar.
  3. Here's how I tell what needs refactoring by Cecil · · Score: 4, Funny

    Code that was written while drunk, high, or half-asleep I will be deeply suspicious of, and probably needs to be refactored immediately. Anything else probably needs refactoring as well, but less urgently.

    1. Re:Here's how I tell what needs refactoring by Anonymous Coward · · Score: 0

      Code that was written while drunk, high, or half-asleep I will be deeply suspicious of, and probably needs to be refactored immediately. Obviously you don't get drunk, high, or sleep deprived. (:

      Wait.. how else would you know it was suspicious code!?
    2. Re:Here's how I tell what needs refactoring by ByteGuerrilla · · Score: 2, Funny

      It's suspicious if you're reading it and you don't realise it's your code because you keep thinking "What the hell is this guy doing?"

      --

      A block of code, sufficiently well-written, is indistinguishable from magick.

    3. Re:Here's how I tell what needs refactoring by fatnutz · · Score: 1

      Unless you were at your Balmer Peak.

    4. Re:Here's how I tell what needs refactoring by 0123456789 · · Score: 2

      It's suspicious if you're reading it and you don't realise it's your code because you keep thinking "What the hell is this guy doing?"


      Ah, someone else who writes perl...

    5. Re:Here's how I tell what needs refactoring by kaens · · Score: 2, Insightful

      This happens to me pretty regularly if I write a section of code, wait three months, and then read it again.

      It's happening less frequently though, so perhaps my skill level is leveling out.

      Either that or I'm stagnating.

    6. Re:Here's how I tell what needs refactoring by Cecil · · Score: 1

      I used to know a guy who always did this, except he did it quite intentionally, as a challenge to himself (I guess he was chronically bored or something). His goal was to write programs in as few lines as possible, where "line" was synonymous with "semicolon". Insane recursive functions with enormous nested chains of trinary "condition ? result : else" operators were the norm.

  4. qualitative vs. quantitative by Doviende · · Score: 0

    Nice article! I thought it was an interesting way to bring a qualitative feel back into software development. In a word of mathematics and code, we often lose sight of those qualitative things in favour of hard numbers. I think developers too often live in the analytical world like european Chess when they should be combining intuition with analysis like in Go / Weiqi.

    --
    "The value of a man resides in what he gives,
    and not in what he is capable of receiving."
    --Albert Einstein
  5. This sure beats the other literary refactorings by plover · · Score: 4, Funny

    I hate the e.e. cummings method of refactoring, which is to run all your code through a lower-case filter. Never seems to help very much.

    --
    John
    1. Re:This sure beats the other literary refactorings by Stanistani · · Score: 5, Funny

      I prefer the Raymond Chandler method - if you're having a problem with a section of code, have a man come through the door holding a gun in his hand.

    2. Re:This sure beats the other literary refactorings by martin-boundary · · Score: 3, Funny
      How about the Joel Spolsky method of refactoring?

      10 Never throw old code away.

      20 If code is broken, GOTO 10.

    3. Re:This sure beats the other literary refactorings by Chemisor · · Score: 1

      > if you're having a problem with a section of code, have a man come through the door holding a gun in his hand.

      Unfortunately, if you are having problem with spaghetti code, like I am, your man would have to crawl on his belly for several miles in twisty passages all alike before reaching the actual problem.

    4. Re:This sure beats the other literary refactorings by gbjbaanb · · Score: 5, Funny

      I say Jeeves, cancel my engagements for the morning, Aunt Agatha has decided that I must refactor my code so the Drones Club annual 'ship without testing' party will have to wait.

      Adversity strikes when one least welcomes it Sir.

      She claims my code 'smells'. I'll have her know my code smells as spiffly as a, as a, well, as a whatnot Jeeves.

      Indeed sir.

      Yes, a whatnot. I check my code against the very latest coding practices, and sometimes I even run it through unit tests!

      Admirable qualities in a coder, if I may say, Sir.

      Yes you may Jeeves. Now. to work! beastly testing.

      Sir, perhaps one could use some automated tool or other method of achieving the requisite level of quality desired.

      You know Jeeves, you've hit it right on the head there. I'll get Bernie Smetherington-Smythe to do it, he's such a ghastly bore but, well, when it comes to code review testing, there's no-one that can cut the mustard quite like him. Zip the source up Jeeves, we're to go pay Bernie a visit.

      Certainly Sir, but what if Aunt Agatha finds out?

      Pish Jeeves, pish! The auditors won't be around for months, no-one'll be any the wiser, and I can go to the ship-without-testing party after all. Life just falls into place sometimes doesn't it Jeeves? After all, What could go wrong?

      Yes Sir.

    5. Re:This sure beats the other literary refactorings by The+Fun+Guy · · Score: 4, Funny

      There's someone at the door, Jeeves.

      Very good, sir. Mr. Fink-Nottle, sir.

      What ho, Gussie.

      Oh, Bertie, thank heavens you're here! Someone is appropriating the prose style of the greatest author the English language has ever produced, and doing it in the most dreadful manner! He's even capitalizing the word "sir", and having Jeeves make interrogatory rather than simple declarative statements!

      Sorry, Gussie, he did a simple what?

      Oh Bertie, you ass, Jeeves would never actually question you! He would never say, "Certainly Sir, but what if Aunt Agatha finds out?" because that's a flat out question! Besides, he certainly wouldn't refer to your relative as "Aunt Agatha"! He might say, "Certainly Sir, but I might draw attention to the fact that Mrs. Gregson would take a dim view of such an approach." Bertie, you have to do something!

      All well and good, Gussie old thing, but what am I to do? The hands of the Woosters are tied, as it were.

      Not you, you fathead. We want Jeeves for this sort of thing!

      Ah, of course. Jeeves?

      Yo, Mr. B, what up?

      Jeeves, if you could forego the anachronistic and inappropriate argot for the moment, we have a problem, or rather a sort of quandry which requires your attention.

      Word. I talk my talk, yo.

      Sharpen your wits, Jeeves, for this is unlike any you have faced before, and I fear that even you may not be up to the task.

      De nada, boss. I got yer solution right here.

      Jeeves? Do I hear correctly? We've not yet set the problem before you, and you have an answer for us?

      Damn, bitch, didn't I just say that? Can't I hear my own self talking? Sh*t, I know what the problem is and I got the answer. It's self-referential code, dude. The problem is the solution, and vice versa. Get the code to recognize it's own faults, and set it to modify itself.

      And we would then end up with...?

      Undying prose, sir.

      Yes, Jeeves. How appropriate.

      With sincerest apologies to the Master, P.G. Wodehouse, whose writings gave me so much pleasure over the years, until I tried to write novels myself. Then they made me want to kill myself for my inadequacies as a writer.

      --
      The man who does not read good books has no advantage over the man who cannot read them. - Mark Twain
  6. The idea of physically printing code... by nullchar · · Score: 5, Interesting

    ...is a neat idea. Besides the mentioned practice of raising and lowering pieces of code that the developers are happy and dissatisified with, hanging code encourages peer review.

    Perhaps not in-depth code review, but physically hanging code in your office might "scare" developers into adhering to their organization's standards for fear of their coworkers mockery of poor code.

    It might be difficult to hide shitty code when anyone can walk by and look at what *you* think is good.
    (At least it might take just as much effort to hide bad code as it does to make it good.)

    1. Re:The idea of physically printing code... by Anonymous Coward · · Score: 0

      One day at work I was digging through some old documentation on one of the network drives and found a 2500 page scanned, OCRed, PDF file of previously printed code from one of the older systems. You haven't lived till you've browsed through 2500 pages of Z80 assembly language.

    2. Re:The idea of physically printing code... by Whiteox · · Score: 1

      Virtual Papier Mache is good for that kind of stuff.
      My favourite is to blow up a virtual balloon, cover it with 2500 sheets of mangled and drenched Z80 assembly, let it dry and paint it. My last one was of Pluto....
      Changed jobs after that. :)

      --
      Don't be apathetic. Procrastinate!
  7. I know it's archaic, by symbolset · · Score: 4, Insightful

    But the screen resolution of fanfold paper hanging on the wall cannot be beaten by the best modern monitors.

    Sometimes just printing the stuff out, papering the floor with it and literally crawling over it yields answers that otherwise escape.

    If the line width won't fit on the paper at a reasonable pitch, there's a clue right there.

    --
    Help stamp out iliturcy.
    1. Re:I know it's archaic, by cheater512 · · Score: 1

      For some reason I dont think Linus uses that technique. ;)

  8. BTW by symbolset · · Score: 4, Interesting

    I'm agreeing with you. 30k lines is 500 pages. That's roughly 8' high by 50' wide. Definitely doable.

    Not about the scaring though -- just about it being useful. Anxiety isn't something I'd want to deliberately introduce to a working programmer. Most of the ones I've known had enough performance anxiety issues of their own without adding any.

    Hanging the code makes some errors more visible. Not all errors are bugs. Some are structural. Structural fixes sometimes repair "pernicious" bugs.

    --
    Help stamp out iliturcy.
  9. Even better idea! by johannesg · · Score: 4, Funny

    I have an even better idea: instead of printing the code on paper, maybe we could represent it by making corresponding holes in little cards. The cards you could hang in front of the window. As the classes get simpler, the holes can get bigger (because less total space is needed) and they get spread around more easily, so more and more light filters through. This way we can emulate the "sun rising on the project", "light at the end of the tunnel" feeling we all love so dearly.

    Need a status update? Just look into the room - if you can see sunlight, the work is done!

  10. Big Visible Charts by EponymousCoder · · Score: 5, Interesting

    I really like the concept, and it fits in with a bunch of techniques we've been using at work in line with the "Big Visible Charts" ideas. Things like this and Agile stories written on index cards and pinned to the wall do sound hokey. A number of people like Johanna Rothman http://www.pragprog.com/titles/jrpm however point out, that these techniques are a lot more inclusive and (as I've found) you get much more animated discussions than the pm/architect/team lead writing a document "for discussion."
    If nothing else it's fun to watch management trying to cope with your walls being covered with sheets of paper, cards and string when they've paid all this money for MS Project and the Rational Suite.

    1. Re:Big Visible Charts by anomalous+cohort · · Score: 1

      Also, here is the obligatory link to the Martin Fowler Refactoring catalog.

      http://www.refactoring.com/catalog/index.html

  11. Wow, I like it! by S3D · · Score: 4, Funny

    My code is not ugly. It's battle-scarred

    1. Re:Wow, I like it! by mcmonkey · · Score: 1

      My code is not ugly. It's battle-scarred

      Sounds like something out of the Klingon rules of software development.

      What is this talk of 'release'? Klingons do not make software 'releases' Our software 'escapes' leaving a bloody trail of designers and quality assurance people in its wake.

  12. The art by www.sorehands.com · · Score: 3, Insightful

    Most of the books and documents that I read in the last 20 years go towards metrics, statistical analysis of code. This ignores the Zen and art of coding and debugging. While much of coding is science, there is a part of it that is feel. If it is only science, then code generators would have already eliminated programmers.

  13. Don't do minor cleanups by tinkerton · · Score: 1

    When refactoring dirty code, avoid doing minor cleanups on the other code. This way the places where you still need to work on stand out from the rest. In any case, as soon as minor cleanups go beyond layouting, it also means you're doing changes in code without test coverage. Even straightening out if/else clauses easily leads to errors.

    1. Re:Don't do minor cleanups by petermgreen · · Score: 1

      the problem with that is without doing minor cleanup it is sometimes rather hard to work out what a peice of code is trying to do. I'm talking the kind of function that has a cryptic name, one or two letter parameter/variable names and no comments.

      --
      note: i'm known as plugwash most places but i screwd up registering that here somehow in the past and now can't register
    2. Re:Don't do minor cleanups by tinkerton · · Score: 2, Insightful

      Agreed. And you have to change code anyway when you're moving functions that are defined elsewhere, so the code does change.
      The key idea though is, you have an array of visual cues that tell you instantly this code still needs to be refactored. These cues often can be removed in bulk, even automated with scripts. Indentation for example. Or use of deprecated functions. Certain types of comments. It's attractive to do these bulk cleanups because they give the overal code a healthier outlook. But they remove the cues. The actual rule would more be something like "don't work on cosmetics".

  14. Visual perception is "easy" by jonaskoelker · · Score: 5, Insightful

    The article highlights a principle which we all know (either explicitly or implicitly): we are highly vision-oriented creatures; visual perception is (relatively) easy for us. A quick convincer: coloured and neatly indented code is easier to read than monochromatic unindented code, right? So perception of colour and position is faster than that of symbols and their relationships.

    The methods in the article plays right into this: by viewing the code zoomed out greatly, one can readily see the density of code, and get a visual "fingerprint" of each chunk. By coupling printout position to satisfaction with the printed code, one can readily see which piece of code needs the most work.

    Interesting additions: adding colour to each class and method based on how memory they allocate (or how many objects they construct); or colouring functions relating to their position in the call graph, or their in-degree.

    1. Re:Visual perception is "easy" by DerekLyons · · Score: 1

      Interesting additions: adding colour to each class and method based on how memory they allocate (or how many objects they construct); or colouring functions relating to their position in the call graph, or their in-degree.

      Careful, down that path lies dragons. Adding too much detail not only raises the temptation to waste time by fiddling with the presentation, it also risks turning your 'visible display' from simple line art into a pointillist painting, where you can not only no longer see the broad details - but also where the eye can fool you into seeing something that isn't there. KISS.
    2. Re:Visual perception is "easy" by Anonymous Coward · · Score: 0

      Although I may agree with you, you have a logical flaw in what you conclude and I'm not 100% sold.

      "we are highly vision-oriented creatures"
      Let's grant you this.
      "visual perception is (relatively) easy for us."
      Let's grant you this.
      "coloured and neatly indented code is easier to read than monochromatic unindented code"
      Let's grant you this.

      "So perception of colour and position is faster than that of symbols and their relationships."
      Woah, hold on, back up. How did color slip in there? You want it in there, provide some support.

      Color (note the correct spelling) *may* make things clearer, but it also may distract. I find (personally) that the extreme use of color in what passes for email clients these days makes things difficult to understand, where old-style indenting (and correct adding of text in a linear vs. POTAS fashion) makes it very clear.

      I find (personally) that limited use of color can be helpful, but what I believe you are talking about (color1 for this, color2 for that, colorN for this other thing) turns into a hodgepodge. Again, this is my personal view. But I didn't drag color into this. And if you want it to stay you have to earn it.

      polemic is the image word.

    3. Re:Visual perception is "easy" by jonaskoelker · · Score: 1

      you have a logical flaw What's the flaw? Where is it?

      So perception of colour and position is faster than that of symbols and their relationships. Woah, hold on, back up. How did color slip in there? You let it slip in there:

      coloured and neatly indented code is easier to read than monochromatic unindented code Let's grant you this.

      Color (note the correct spelling) Please file a bug report against my dictionary, then. Until it says color, I'm saying colour. I'm thinking one is American, the other is British. You know, like grey versus gray, trivialize versus trivialise and potato versus potato.

      I find (personally) that limited use of color can be helpful, but what I believe you are talking about (color1 for this, color2 for that, colorN for this other thing) turns into a hodgepodge. What you believe I'm talking about is limited by N. If the limit on the use of colour you find helpful is finite, then there will exist an M such that your way of doying this uses color1 for this, color2 for that, colorM for this other thing. I know I'm picking nits here, but I'm not doing it for its own sake; I'm trying to explain why I don't understand your point.

      But I didn't drag color into this. And if you want it to stay you have to earn it. You let colour in, as I pointed out above, so I have earned what I needed to earn. If you want colour out know, you have to earn that. Isn't proof obligation shifting just fun? ;)

      Also, "drag into this"? Let's be clear: I said two things about colour:

      1. perception is fast and easy.
      2. these are my ideas of how one might use colour: [...]

      You've agreed with number 1, yet argued against my position. If you want to argue against number 2, please start by reading one or more posts that already do that. Otherwise, what are you saying?

      To spell it out, I supported number 1 with the code example. If you want more support, listen to mit_ocw::intropsych::visual_perception (I'm too lazy to find the link for you).
  15. Refactoring by ettlz · · Score: 1

    ...takes a very long time on the product of two large prime codes.

  16. Divide and conquer by Anonymous Coward · · Score: 0

    I agree with assigning ownership, but I don't think it is enough. How do you aggregate the combined efforts of all the teams into a birds eye perspective and how do you know where the boundaries of the project lie? If the project is big enough and the requirements are complex enough, you really need an evolutionary method to control the cost of adjusting direction. The number of questions you propose will simply be too many to deal with. On the other hand, if it's all about producing a release (prototype, proof-of-concept, save-my-butt-development) and then move on you may have the benefit of never having to taste your own dog food and the investors will pick up the bill instead and draw strange conclusions about the lifetime of an invention.

  17. Or you could just ask Jeeves to do it for you. by IainMH · · Score: 4, Funny

    Talk about decoupled classes..

    What ho.

  18. Only 30K lines anyway... by johannesg · · Score: 5, Insightful

    I was under the impression that "large projects" started somewhere around the million lines of code mark, not at a mere 30K lines. But here is what I do, and none of this require any special insights into the source code (note that I do this primarily for C++):

    1. Ruthlessly delete lines. Get rid of ***anything*** that does not contribute to correct operation or understanding. Even including things like version history (that's why you have the damn tool, use it already (1)!), inane comments (but keep the stuff that actually helps with understanding), code that is commented out (if you really need it, it will be in the aforementioned version tool), code that is not called, and code that is not doing anything at all (such as empty constructors or destructors).

    2. Decrease the scope of everything to be as tight as you possibly can. Make everything that you can private, static, or whatever else your language offers to decrease scope. Declare variables in the innermost scope. Make them all const if possible.

    3. Anything that belongs together should be in one file (even if that files becomes 5000 lines long). Anything that *doesn't* belong together should be split into separate files (but don't make a file for just a single function - instead create a file with "leftovers").

    4. Anything that has a non-descriptive name is to be renamed to what it really represents. No more "int x; // x is the number of blarglewhoppers" - just use "int NumBlargleWhoppers" instead.

    5. Keep an eye open for duplicate code. Get rid of the duplicates.

    6. Any special insights gained, write them down as comments in the appropriate place. Anything you do NOT understand, also write them down as comments. Mark those with something you can grep for.

    7. Any homegrown version of something that is available in STL or boost, to be replaced by its "official" alternative.

    8. And that goes double for string operations! No more "char *" anywhere; it is the 21st century, use strings already! I'll make an exception for functions that allow "const char *" to be passed in, but only with the "const". If I find a "char *" without the "const", I *will* come to your office and bash your head against the wall. Repeatedly. Just so you know.

    9. Any error handling through error return codes, probably to be replaced by exceptions, unless it turns the calling code into a wild mass of try/catch blocks.

    10. Pointers, to be replaced by references where possible.

    11. Negative logic and names, to be replaced by positive logic and names. Don't have "if (!NoPrinterAvailable()) {A();} else {B();}" - instead do "if (PrinterAvailable() {A();} else {B();}".

    12. Anything that looks like it was written by drunk lemurs or the French, to be deleted on principle and replaced by something sane.

    So there you have it. In my experience, doing this will remove about half of the lines of code (more if there was a significant number of lemurs on the team), at the gain of considerable clarity and usually performance.

    (1) And honestly, I don't give a flying fuck which one of you messed up on the 29th of february 1823 or why you thought it was a good idea in the first place. I'm concerned with what the code will be doing in the future, not how it came to be in this sorry state. Chances are, whatever you thought at the time is long obsolete anyway. Get rid of the cruft. Get rid of anything that doesn't help - it just clutters the mind.

    1. Re:Only 30K lines anyway... by Anne+Thwacks · · Score: 1
      Of course you could just chuck all this object-disoriented stuff and write in good, old fashioned C, like the rest of us.

      If its too big to fit in the address space of a 6502, then you are doing it all wrong. (or maybe it should have been done in SNOBOL in the first place.)

      --
      Sent from my ASR33 using ASCII
    2. Re:Only 30K lines anyway... by siride · · Score: 4, Insightful

      > 9. Any error handling through error return codes, probably to be replaced by exceptions, unless it turns the calling code into a wild mass of try/catch blocks.

      Exceptions should be used to mark, well, exceptional failure. I really really hate this pattern that Java (and perhaps from elsewhere) has foisted upon us where we get frickin exceptions because we reached the end of the file. That is technically an error condition in the reader function, but it is not exceptional and it shouldn't require me to write the "wild mess of try/catch blocks" just to read in data from a file. Exceptions say "we are really in a mess and have to abort this operation, and potentially the program. They do not say "could not find element x in array".

      If that's what you were saying, however, then I apologize.

    3. Re:Only 30K lines anyway... by Enleth · · Score: 4, Interesting

      I'd disagree on pointers and references. If you pass something in by reference, you need to know it goes in there by reference, it's not visible in the calling code. If something's not visible - well, that's a bug just waiting to crawl in there. If you pass something by pointer, the calling code shows it clearly and you know that whatever was passed is likely to be changed by the called function. That's the rationale used by Trolltech and it is quite convincing to me.

      Besides, using char * is a must sometimes, when using C libraries that accept, modify and return strings or just some chunks of arbitrary data as char *.

      --
      This is Slashdot. Common sense is futile. You will be modded down.
    4. Re:Only 30K lines anyway... by CaptainPinko · · Score: 2, Insightful

      12. Anything that looks like it was written by drunk lemurs or the French, to be deleted on principle and replaced by something sane.

      I'm sorry but I find all this French bashing racist. Unless, of course, you have some information on the coding tendencies of the French that I do not. But having worked with a handful French people and I can say nothing bad about them. I know French "jokes" may be acceptable in the U.S. but this is the Internet and try to behave yourselves. As a rule of thumb: replace the word 'French' with 'Blacks' or 'Jews', if you wouldn't get away with saying the resulting expression at work, the don't say the original.

      Disclaimer: Not French, no French heritage, no French family, don't speak French (though I plan to learn one day), have no French friends, never dated anyone French, and don't live in France.

      --
      Your CPU is not doing anything else, at least do something.
    5. Re:Only 30K lines anyway... by dubl-u · · Score: 1

      I really really hate this pattern that Java (and perhaps from elsewhere) has foisted upon us where we get frickin exceptions because we reached the end of the file. That is technically an error condition in the reader function, but it is not exceptional and it shouldn't require me to write the "wild mess of try/catch blocks" just to read in data from a file. Exceptions say "we are really in a mess and have to abort this operation, and potentially the program. They do not say "could not find element x in array".

      If you're writing in Java, I couldn't disagree more. Trying to read past the end of a file, trying to reference elements in an array that aren't there? That should not happen, making them exceptional conditions. If you are doing them, that means that you messed up, and things should indeed blow up.

      If you're writing in a DWIM language like Perl, then yeah, nothing should really blow up ever. Reference something that doesn't exist? Sure, you must have meant for that to exist, so we'll create it. And return you an nice undefined value, that other things will handle gracefully. And give you a cookie, just 'cause Larry Wall loves us all.

      Note also that in Java those catch blocks for reading a file are not for the usual case, or generally even the retarded-programmer case. You have to catch IOException when trying to read a file because IO can always fail. You may be writing a program where it's ok to sail on past a disk or network failure, but some of us read and write data that people actually care about.

      In that kind of coding, I'm me grateful for a well-designed set of checked exceptions: it makes me consciously think about and explicitly handle all of the weird little failure cases that I would rather pretend were impossible.

    6. Re:Only 30K lines anyway... by Wrath0fb0b · · Score: 1

      I disagree on your dislike of references - when you type the function name, intellisense (or whatever) pops up the relevant prototypes and you can see immediately whether the parameters are type& or const type&. This gives the benefit of uniform function calling, since passing by reference or const reference should be the default unless there is an explicit need for pass-by-value.

      As to char*, there are very few places I can see it being necessary (ifstream.read((char*)(&data),sizeof(data) does NOT count). You can pass char* to the string constructor and you can get back with string::c_str() -- what else do you need?

    7. Re:Only 30K lines anyway... by siride · · Score: 1

      What's wrong with reading until you get to the end of the file? That's how the idiom seems to be done in every other language I've used. Why is that an exception in Java? If it's the end of the file because there was an IO error, that's one thing. But in the case I'm talking about, it's not.

    8. Re:Only 30K lines anyway... by Enleth · · Score: 2, Insightful

      That means you are dependent on a big, clunky IDE for writing your code. Not everyone uses them even for big projects - for example, KDE's Kate is sophisticated enough to handle those, yet still lightweight. Even worse if you are writing an API for a library: you are forcing everyone using it to memorize where the references were or use a big, clunky IDE. And even if you use an IDE, you sometimes need to read a piece of code and see such things without retyping the paren to force a dumb IDE to display the prototype or even hovering the mouse over each function for a smart IDE to give a hint.

      --
      This is Slashdot. Common sense is futile. You will be modded down.
    9. Re:Only 30K lines anyway... by jsebrech · · Score: 5, Insightful

      So there you have it. In my experience, doing this will remove about half of the lines of code (more if there was a significant number of lemurs on the team), at the gain of considerable clarity and usually performance.

      I work on a 2 million line code base, written by a few dozen people, most of them off-shore, that is poorly commented, poorly documented, and has many modules of code that no one in our team understands well. In other words, a typical large commercial code base.

      At first, I would routinely aggressively clean up sections of code as I made changes in them. But then I started to notice a pattern: there were bugs in the functionality of that code that weren't there before I "cleaned it up". When you refactor highly convoluted code, it is seductive to make assumptions about the working of that code (especially in how the code interacts with the rest of the system), because it is hard work to actually figure it out completely. Those assumptions have a nasty tendency to be wrong.

      Nowadays I approach code changes like this: if I don't understand the code 100 percent, I make my changes as low impact as possible, even if it means uglifying the code. If some part of the code base needs refactoring to allow implementing a new feature, I first figure it out fully, document its existing behavior (often line-by-line, call-by-call, class-by-class), look at every place in the entire code base where it is called (and document those places), and only then do I refactor it.

      The point is this: if code is ugly and slow, but it works, it is better code than clean, fast, beautiful code with bugs. Better in the sense that it makes the user happier, and the user is one of only two metrics that truly matter in software development (the other being cost). Always resist modifying code just for the sake of cleaning it up. If it works, don't touch it.

    10. Re:Only 30K lines anyway... by johannesg · · Score: 1

      You know, *I* have worked with French code for over ten years; from a multitude of companies and individuals. I feel fully qualified to state that most of the time, it is REALLY BAD.

      Have *you* worked with any code written by the French? Or are you just randomly bashing people because that is such a fun, safe thing to do on the internet?

    11. Re:Only 30K lines anyway... by CaptainPinko · · Score: 1

      No I haven't and I said as much in my post. But I see French bashing everywhere and it was a general comment, and not a direct response to yours. That said I've never worked with anything but shit north american code... and I live there. So the question comes up as to whether you have a wide enough sample to make such a claim. And the point is if you had worked for 10 years with african-american code and it was all shit would you make such a comment? Unlikely.

      --
      Your CPU is not doing anything else, at least do something.
    12. Re:Only 30K lines anyway... by matfud · · Score: 1

      Cos the size of the file can change while you are reading it.

    13. Re:Only 30K lines anyway... by Anonymous Coward · · Score: 0

      Actually, saying it's always bad is flawed. I don't think there's any significant concept in programming that applies 100% of the time. For instance, if you make your in-parameters constant references & out-paramters just references then that's consistent too.

      And as for being visible in code, most modern IDE's let you look up the function prototype inline with the code as a tooltip or whatnot. Sure if you're using a text-editor, then that doesn't help you, but even VIM & Emacs have ctags support.

    14. Re:Only 30K lines anyway... by shutdown+-p+now · · Score: 1

      I really really hate this pattern that Java (and perhaps from elsewhere) has foisted upon us where we get frickin exceptions because we reached the end of the file. That is technically an error condition in the reader function, but it is not exceptional and it shouldn't require me to write the "wild mess of try/catch blocks" just to read in data from a file.
      It depends on what you're doing. Quite often, getting EOF while reading a file of some known format is an exceptional situation - it means that file is corrupted, and you'd want to have a single try-catch block around the whole parsing routine to catch that. On the other hand, sometimes you actually expect it to happen often, and be handled like another valid branch of code. That's why I like the .NET pattern of having pairs of methods like Foo() and TryFoo(); the first one throws exceptions on soft failures, the second one has some more efficient way to report them (usually an out parameter).
    15. Re:Only 30K lines anyway... by siride · · Score: 1

      Something like that would be nice in Java. And it follows the pattern I described above much better. If you expect errors and failures or end of file as part of normal processing, it's not an exception and exceptions should need to be used. I wish over-use of exceptions was the only thing that bothered me about Java...

    16. Re:Only 30K lines anyway... by shutdown+-p+now · · Score: 1

      If you really want the out parameters to be distinctly marked at call site (and I agree that it is a good idea, but it may be just my C# experience), just write a simple ref-wrapping class with an explicit constructor, and use it everywhere. Using pointers can lead to subtle problems not just because they can be null, but also because they have plenty of operators defined on them, and it can be all too easy to forget to dereference sometimes, with very interesting results that might manifest themselves a long way down the line (consider the difference between (p - q) and (*p - *q); then, note that there are more possible valid combinations).

    17. Re:Only 30K lines anyway... by dubl-u · · Score: 1

      What's wrong with reading until you get to the end of the file? That's how the idiom seems to be done in every other language I've used. Why is that an exception in Java? It's not. The typical Java idiom for reading lines from a file looks like this. (Actually, in production, you would actually handle the errors in that exception block rather than swallowing them. I would personally put the close() call in a finally block, not the main try block, as you still want to close even if a read fails. But you get the idea.)

      As you can see, you read until it comes back empty, and then you're done. No exceptions will be used except when things are exceptional.

      That's still slightly ugly, but that should rarely matter in Java or in any well-built OO system. You never read files for the sake of it; you're always up to something specific. Even when I'm dealing with files all the time, I almost never write code like this, because I'm reading XML or reading a properties file or something where all the details are taken care of in a method that I almost never see.
    18. Re:Only 30K lines anyway... by ralphdaugherty · · Score: 2, Informative

      I know French "jokes" may be acceptable in the U.S. ...

            only for a small conservative subset of the U.S., which as we know have proven themselves to be a joke.

        rd

    19. Re:Only 30K lines anyway... by dubl-u · · Score: 2, Interesting
      I agree with most of your comments, and especially the spirit of keeping everything shipshape and avoiding the endless game of "who can be blamed". Two minor improvements:

      Any error handling through error return codes, probably to be replaced by exceptions, unless it turns the calling code into a wild mass of try/catch blocks. Sometimes instead of return codes, there are other good options. For example, you can spin a state machine out into an object, which in effect keeps the return codes safely in one place until you want to check them. In some cases, I love the Null Object Pattern. And sometimes it makes sense to have a request object and a response object, with the response carrying possible error-related info.

      Anything that *doesn't* belong together should be split into separate files (but don't make a file for just a single function - instead create a file with "leftovers"). For functions, maybe. But a lot of good objects really can have just one method. Ruby does that all the time with anonymous methods, for example, and sometimes that pattern is worth using in a more explicit language.

      Also, more generally, I feel like unit tests are a much better place to store knowledge than comments.

      Other than that, I agree completely!
    20. Re:Only 30K lines anyway... by ralphdaugherty · · Score: 1

      Always resist modifying code just for the sake of cleaning it up. If it works, don't touch it.

            This of course is wisdom of the ages which appears to have been lost somewhere where the term "refactoring" became vogue.

        rd

    21. Re:Only 30K lines anyway... by Limerent+Oil · · Score: 1

      Cos the size of the file can change while you are reading it.
      Sine the size of the file can change too, but (sin^2 + cos^2) won't.
    22. Re:Only 30K lines anyway... by Anonymous Coward · · Score: 0

      Refactoring as a concept was based on code transformations which do not alter semantics of the transformed code. Somewhere, along the way, that semantic part was somehow lost in the noise.

    23. Re:Only 30K lines anyway... by chromatic · · Score: 1

      ... only if your "refactoring" doesn't include a comprehensive, fully-passing, and regularly run test suite.

    24. Re:Only 30K lines anyway... by Wrath0fb0b · · Score: 1

      First off, I'd hardly call Visual Studio either clunky or big - I have an XP VM almost solely for VS and it has always been responsive. The interface is fairly customizable and the debugging is top-notch (much better that SunStudio, the only other one I've tried).

      If you don't like IDEs (which is apparent), then use your favorite text editor or shell to search for "functionName(*);" in all files. Shit, `grep -e WriteForce*\&* *.h*` ought to get you less than a screenfull of results, one of which is the prototype (regexp wizards can do better, I'm sure).

      Finally (and I think most importantly), if you have to wonder whether a function changes a passed variable then either it is incorrectly named or you have more serious issues with program flow. A sensible naming scheme makes it clear what data is input versus output.

    25. Re:Only 30K lines anyway... by ralphdaugherty · · Score: 1

      ... only if your "refactoring" doesn't include a comprehensive, fully-passing, and regularly run test suite.

            in a non-trivial world, "works" by definition is regularly running a comprehensive test suite, otherwise known as production, and fully passing.

            but in the spirit of your comment, one will not get to "works" by debugging with production, not when every change in a non-trivial world has to be signed off on by a suite of high level people who are now personally responsible for everything you changed.

            I don't know about "refactoring"/rewriting in a trivial world, but sounds like fun. Especially the part where you don't have to get every director impacted by a change to sign off on your test results.

        rd

    26. Re:Only 30K lines anyway... by radish · · Score: 1

      Reading a file in Java is much the same as any other language. You call read() in a loop until it returns null, that indicates EOF. If you call read() again however, well you screwed up and you will get an exception. Makes perfect sense to me.

      --

      ---- Den ene knappen er powerknapp, den andre er Bender voice knapp "Bite My Shiny Metal Ass"

    27. Re:Only 30K lines anyway... by mbrod · · Score: 1
      Excellent list. I have been programming professionally in C++ for about 10 years. You see a number of these items you have listed that go contrary to what an Academic or a "puritan" would advocate but are essential in a large project. Particularly:

      3. Anything that belongs together should be in one file (even if that files becomes 5000 lines long). Anything that *doesn't* belong together should be split into separate files (but don't make a file for just a single function - instead create a file with "leftovers").

      4. Anything that has a non-descriptive name is to be renamed to what it really represents. No more "int x; // x is the number of blarglewhoppers" - just use "int NumBlargleWhoppers" instead.

      8. And that goes double for string operations! No more "char *" anywhere; it is the 21st century, use strings already! I'll make an exception for functions that allow "const char *" to be passed in, but only with the "const". If I find a "char *" without the "const", I *will* come to your office and bash your head against the wall. Repeatedly. Just so you know.

      10. Pointers, to be replaced by references where possible.

      One can argue against some of these for one person doing a project. However, when you have a large project with many developers, some with 6 months experience, some with 25 years of experience, it is a different paradigm and that is a good over all list.

    28. Re:Only 30K lines anyway... by Anonymous Coward · · Score: 0

      i've come to appreciate a balanced middle way. if something is actually working -- great: don't mess with it, even if the code is horrible. 7 times out of 10, however, i find that the ugliest code has the most bugs; the hardest to find and fix bugs. even if cleaning it makes me ( and my team ) eat a few new near term bugs, it can often save time in the long term.

      if you're really unsure of your changes a great approach can be to write unit tests *then* re-factor. unfortunately sometimes ugly code is amazingly hard to unit test because it's so very un-unit like.

    29. Re:Only 30K lines anyway... by Actually,+I+do+RTFA · · Score: 1

      If you pass something in by reference, you need to know it goes in there by reference, it's not visible in the calling code. If something's not visible - well, that's a bug just waiting to crawl in there. If you pass something by pointer, the calling code shows it clearly and you know that whatever was passed is likely to be changed by the called function. That's the rationale used by Trolltech and it is quite convincing to me.

      I agree that output should always be passed by pointers, as that makes things explicit. I like passing in by reference when the variables are read-only however. It makes things clean, and makes it less likely that input/output will be confused.

      --
      Your ad here. Ask me how!
    30. Re:Only 30K lines anyway... by mwvdlee · · Score: 1

      I'm going to make a bold statement here:

      "There is no such thing as a comprehensive test".

      If you have a program that adds numbers, and you test it with every possible addition between 1 and a billion, you have no guarentee it'll work with billion-and-1. Or zero for that matter.
      The best a test can do is demonstrate a program less or more likely to fail, and only when used within certain limits.

      --
      Slashdot social media options: AIM, ICQ, Yahoo, Jabber and Mobile Text. Why no MySpace?
    31. Re:Only 30K lines anyway... by AceJohnny · · Score: 1

      12. Anything that looks like it was written by drunk lemurs or the French, to be deleted on principle and replaced by something sane.
      Monsieur, je suis outré par votre comparaison de mes glorieux compatriotes avec des nobles lémuriens sous influence!

      Les lémuriens ne boivent pas de vin, c'est evident: la convoyage de bons crus vers Madagascar pour consommation lemurienne n'est pas economiquement viable!

      Sir, I am shocked by your comparison of my glorious compatriots with noble lemurs under the influence!

      Lemurs do not drink wine, obviously: transportation of proper wines to Madagascar for consumption by lemurs isn't economically viable!


      --
      Misleading titles? Inflammatory blurbs? Keep in mind that Slashdot is a tabloid.
  19. For more details, consult... by tiluki · · Score: 1
    1. Re:For more details, consult... by Anonymous Coward · · Score: 0

      Oh come on, mods. That's funny!

  20. BTW-flagging performance by Anonymous Coward · · Score: 0

    "Most of the ones I've known had enough performance anxiety issues of their own without adding any."

    Wow! Lends new meaning to the term, performance review.

  21. refactoring is not a word by Dr.+Tom · · Score: 1

    Nope, it's not. And it's a stupid practice, the way some people try to define it.

    "What are you doing?"

    "I'm refactoring code."

    "Oh, you aren't doing anything."

    1. Re:refactoring is not a word by stratjakt · · Score: 0

      It's just "rewriting" in manager speak, since "rewriting" implies you did it "wrong" in the first place.

      When, in truth, it's not about right or wrong, since anything that works is right (and to many, anything that compiles), but it's about "better", and "better" could mean maintainability, portability, testability, or whatever metric of the month they're using.

      I once created a little code-gen to assist unit testing a particular functionality of a project. Basically, any time a field engineer or tester found a bad instance, they could use a tool I provided to instrument on the running assembly and "snapshot" some input data, and basically hand me a failing unit test, which I could then fix. There were tons and tons of combinations that crashed this thing, it was broke all to hell when I took ownership of it.

      The bad data started flowing in, the code got really strong really fast, and really quickly I had added a couple hundred unit tests to the automated build. And I got bawled out for it, since "number of unit tests written" was apparently the metric management had decided to use to see how good the coders are, and since I was suddenly producing an order of magnitude more than others, I was "cheating". It wasn't like the tests were just trivial permutations of data, every one of these things was an edge case that was borking the code. By comparison, most of the handwritten unit tests I was seeing were really just useless dickstroking permutations of Assert.IsTrue(awesome == me);

      The fact that they could finally ship the product with this particular feature working and tested was lost on them.

      Everyone has PHB stories, because everyone has (or has had) a PHB.

      --
      I don't need no instructions to know how to rock!!!!
  22. Code can be sexy and beautiful by DavidV · · Score: 1

    quicksort [] = [] quicksort (x:xs) = quicksort less ++ [x] ++ quicksort greater where less = [ y | y = x ]

    --
    !sig
    1. Re:Code can be sexy and beautiful by jlarocco · · Score: 2, Insightful

      Elegant as it may be, that version of quicksort is so slow that (IIRC) even the Haskell documentation suggests against using it in "real" code.

      Personally, I think the C++ way is even easier to read, and it has the benefit of being really fast:

      sort(xs.begin(), xs.end());

    2. Re:Code can be sexy and beautiful by Anonymous Coward · · Score: 0

      Good lord, why not just sort(xs)? The way C++ makes its containers try to look like arrays complete with pointer arithmetic is just repulsive.

  23. So, SeeSoft then... by Anonymous Coward · · Score: 0

    I just scanned the article and I see that the big idea is to reimplement SeeSoft but not as well. Awesome.

  24. It's not enough for the code to "just work" by kurisuto · · Score: 4, Insightful
    From TFA:

    "The problem is that warty old code isn't always just warty - it's battle-scarred. It has years of tweaks and bug-fixes in there to deal with all sorts of edge conditions and obscure environments. Throw that out and replace it with pristine new code, and you'll often find that a load of very old issues suddenly come back to haunt you. So, a total rewrite is out. This means working with the old code, and finding ways to wrestle it into shape."


    There's a big difference between having code which just happens to somehow work, and having code which works because the code is clearly written and documented, where the person in charge of maintaining it actually understands what the code is doing.

    Whether you rewrite from scratch or work with the legacy code, it's your job as the programmer to understand and document all of the tweaks, bug fixes, edge conditions, and obscure environments. If there aren't comments in the existing code to explain these things, then it's your job to understand why the code is doing what it is doing, and add the comments as needed. If the code isn't clear, it's your job to make it clear.

    The author correctly points out that when you do a total rewrite, then the undocumented special cases handled by the old code will make themselves felt. As these problems present themselves, it takes time to fix them. However, you also get the opportunity to understand the undocumented special cases and get them clearly coded and properly documented, which reduces maintenence costs over the long term. Your judgment whether to maintain or to rewrite should take both of these factors into consideration.

    1. Re:It's not enough for the code to "just work" by dubl-u · · Score: 1

      it's your job as the programmer to understand and document all of the tweaks, bug fixes, edge conditions, and obscure environments.

      And in my view, it's my job as a programmer to document all of those things in readable automated tests. Only a programmer can check that a comment or a document has been followed. Automated tests mean that the computer can do the dirty work. And I'm all for that.

  25. Form follows function? by martyb · · Score: 4, Interesting
    FTFA:

    ... A better solution would be to print a class per page. At the start of the project, the application had about 150 classes, and the refactoring effort is focussed on about 80 of those. Initially, gigantic classes would be an incomprehensible smudge of grey, but as the refactoring process starts tidying the code and factoring out into other classes, the weekly printout would start to literally come into focus, hopefully ending up with many pages actually containing readable code (which happens roughly when the class is small enough to fit on no more than 3 pages at normal size).

    Brilliant! Absolutely brilliant! "Smell test?" Yah, right. But then I got to thinking, "Why are code formatting standards such a hot topic?" The computer doesn't care if indentation is expressed with 2 spaces, 3 spaces, or a tab. But, I do! Over time, I've learned how to see coding errors just from the slight aberrations in the LOOK of code. Couldn't tell you WHAT it was, at first, it just felt (or smelled) wrong. So call it what you will, but I could now see how "smell test" has some basis behind it. Then, I got to thinking of an age-old question:

    How do you find a needle in a haystack?

    1. Make the haystack smaller, and/or
    2. Make the needle(s) bigger

    The technique in the article accomplishes BOTH of these. I'd suggest running the code through a pretty printer to get consistent layout throughout the whole project. The more the semantics of the project can be represented by syntax, the more visible the troublesome code becomes.

    1. Re:Form follows function? by hcdejong · · Score: 1

      How do you find a needle in a haystack?

            1. Make the haystack smaller, and/or
            2. Make the needle(s) bigger You forgot
            3. Run the haystack through an MRI machine.
    2. Re:Form follows function? by DerekLyons · · Score: 1

      I'd suggest running the code through a pretty printer to get consistent layout throughout the whole project.

      Umm... running it through a pretty printer wipes out the very details that printing out is supposed to bring out. After pretty printing, you are no longer seeing the 'native' code - but rather you are seeing the patterns hard coded into the pretty printer.
    3. Re:Form follows function? by martyb · · Score: 2, Insightful

      I'd suggest running the code through a pretty printer to get consistent layout throughout the whole project.
      Umm... running it through a pretty printer wipes out the very details that printing out is supposed to bring out. After pretty printing, you are no longer seeing the 'native' code - but rather you are seeing the patterns hard coded into the pretty printer.

      I respectfully disagree. Consider a piece of code that has 8 levels of nesting. With a judicious use of short variable names, parentheses, and curly braces, it is possible to make it *look* not so bad in the original code. It might look like there's only a half dozen levels. With a pretty printer, nesting LOOKS exactly as nesting IS. At a *GLANCE* I can see where things are getting hairy.

      That does NOT mean that it must be refactored. Only that it is an area that may be worthy of additional consideration.

      On second thought, nothing says this is a black-or-white choice. If it works for you to print out code as is, great! My experience has been that a pretty printer can be helpful. YMMV.

    4. Re:Form follows function? by tepples · · Score: 1

      You forgot
      3. Run the haystack through an MRI machine. In your analogy, what's the equivalent of an MRI machine for finding places where a program could be improved?
    5. Re:Form follows function? by DerekLyons · · Score: 1

      I agree, it's not a black a white an choice. I was only pointing out that the pretty printer can hide flaws, as you point out that it can reveal them.

    6. Re:Form follows function? by cyborg_zx · · Score: 2, Insightful

      Well the obvious problem with the analogy is that you're not finding needles in a haystack - you're looking for hay in a haystack.

  26. 30k? by Anonymous Coward · · Score: 0

    I know the author means well, but it's really hard to take him seriously when he calls 31 kloc a "fairly large" application. Besides that, too much of the "article" (if a blog entry can be called an article) comes off as ego stroking.

  27. Netscape by BorgDrone · · Score: 1

    I love the way the article uses the complete rewrite of Netscape as an example of why you shouldn't rewrite from scratch. Cause we all know how big a failure Firefox is

    1. Re:Netscape by argent · · Score: 1

      Not to mention that Netscape was already doomed as a browser company well before that rewrite started.

    2. Re:Netscape by balster+neb · · Score: 3, Informative

      I love the way the article uses the complete rewrite of Netscape as an example of why you shouldn't rewrite from scratch. Cause we all know how big a failure Firefox is Have to disagree with you.

      While the Mozilla story did have a happy ending, the rewrite resulted in IE getting a near monopoly of the browser market. The "new" Netscape was massively delayed, and was finally released as a rebranded version of the bloated Mozilla suite. It was in the period between about 1999 and 2004 that IE expanded it's market share. In other words, Netscape lost as a result of throwing away the old code base.

      It was only from around 2004 onwards, with Firefox, was Mozilla able to present a viable alternative to IE.
    3. Re:Netscape by dubl-u · · Score: 2, Insightful

      I love the way the article uses the complete rewrite of Netscape as an example of why you shouldn't rewrite from scratch. Cause we all know how big a failure Firefox is </sarcasm> Firefox is not Netscape.

      The Netscape browser, like the company that made it, indeed ended up a failure. And my pals who were there at the time tell me that the poor code quality was a major factor in the inability to get anywhere. How long did it take between the last decent release of Netscape and the 1.0 release of Firefox? Four years? Six? I guess it depends on what you consider the last decent release. No matter how you count it, though, there were years of thrashing trying to get something based on the old code out the door. Eventually, they just gave up.

      Firefox is a big success now, but Netscape was a giant crater of a failure during a crucial period, leaving Microsoft effectively unopposed in their attempts to take over the browser market.
    4. Re:Netscape by nuzak · · Score: 1

      > In other words, Netscape lost as a result of throwing away the old code base.

      Netscape had already lost. The only takeaway lesson was "it takes a lot of time to rewrite, and possibly even more than starting over from scratch".

      --
      Done with slashdot, done with nerds, getting a life.
  28. When the code is the documentation... by argent · · Score: 1

    There's a big difference between having code which just happens to somehow work, and having code which works because the code is clearly written and documented, where the person in charge of maintaining it actually understands what the code is doing.

    But the point is that when the code is the documentation, which is what you have when you have undocumented code, you're throwing out the documentation with the code if you start from scratch. Refactoring includes documenting the code you're rewriting. In fact I've found that comments added when refactoring do a lot more to explain why the code does what it does than the comments that were already there. I don't mean that the existing comments were wrong, or didn't match the code, but that the comments added by the person who did the refactoring describe the things that were hard to understand in the original code, and often explain why the battle scars were necessary.

    I've found that happening with my code that other people have worked on, with other people's code that I have worked on, with other people's code that other people have worked on, and with my code that I have worked on (because, after all, "you three years ago" might as well be another person... if that's not the case for you, you better ask yourself if you've stopped learning).

    The author correctly points out that when you do a total rewrite, then the undocumented special cases handled by the old code will make themselves felt. As these problems present themselves, it takes time to fix them.

    Sometimes. And sometimes they don't... yet. Sometimes you're reinstalling a time bomb that you thought you'd already defused.

    With either a rewrite or a refactoring, too, you need to understand and document the result. If by the end of a refactoring project you haven't documented the depth charges... then you haven't really finished the job yet.

    Your judgment whether to maintain or to rewrite should take both of these factors into consideration.

    Indeed. I love rewriting code, myself. Start over from scratch. Throw out the old and crufty. But I gotta keep telling myself to watch out for deceptive arguments about why I'm rewriting... and I think this is one of the ones I've tried on myself often enough that I just don't trust it any more.

  29. There are no corner cases in really good gode by Terje+Mathisen · · Score: 4, Interesting

    There are two ideas of thought about corner cases (and the GP pointed out one).
    Thought #1) (GP) There's no such things as a corner. It is a requirement - it may be that fewer people/fewer processes use it; but, it is still a section of the total solution that must be designed to overcome some problematic section. Otherwise, why is the code being written?

    Thought #2) Corner cases only effect a small number of your user-base; therefore, code to satisfy 95%-99% of your customers. The underlying principle here is that the manager will wait for another release. This approach is usually taken when the project manager failed to account for something and says (and I quote), "We'll just re-design it after the first release." I have taken part in a few optimization competitions, and each time #1 has been a crucial part of the solution:

    The usual approach is to optimize the 90-95% case, then bail on the remainder, but this will almost always be beaten by code which manages to turn everything into the "normal" case, with no if/else handling, no testing, no branching.

    When I was beaten by David Stafford in Dr.Dobbs Game of Life challenge, I had lots of specialcase code to handle all the border cases, while David had managed to embed that information into his lookup tables and data structures. (He had also managed to make the working set so much smaller that it would mostly fit in the L1 cache. :-)

    When my Pentomino solver won another challenge, being twice as fast as #2, the crucial idea was to make the solver core as tiny as possible, with very little data movement and the minimum possible number of tests.

    Terje
    --
    "almost all programming can be viewed as an exercise in caching"
  30. And will the result be as delightful? by tringtring · · Score: 1

    Will the result of refactoring code using the PGW method be as funny as his books that the users of the code will laugh till they cry?

  31. Space efficiency? by tepples · · Score: 1

    You can pass char* to the string constructor and you can get back with string::c_str() -- what else do you need? What I need is the extra RAM that the GNU libstdc++ implementation of the string class takes up. I develop for a handheld device, and its 4 MB of RAM is a lot smaller than the 1 GB of RAM that you're probably used to working with.
    1. Re:Space efficiency? by Wrath0fb0b · · Score: 1

      What I need is the extra RAM that the GNU libstdc++ implementation of the string class takes up. I develop for a handheld device, and its 4 MB of RAM is a lot smaller than the 1 GB of RAM that you're probably used to working with. Absolutely. I should have qualified my post by excepting cases like yours where RAM is super-tight (embedded/handheld/etc. . . ) and cases where performance is at a huge premium (tight loops/bottlenecks/mission-critical/real-time). That said, in the vast majority of cases the performance gain from using char* are vastly outweighed by the simplicity, clarity and power of std::string. Heck, automatic destruction when the string goes out of scope is, IMHO, worth the cost of admission alone.
  32. Refactoring vs Sleep/Napping by pg--az · · Score: 1

    >> Code that was written while ... half-asleep has 25 minutes of video which are worth watching just to catch e.g. the beautiful French accent when the researcher Van Cauter remarks that Americans regard going without sleep as a "badge of honour". The idea that you might actually be more effective at linking together all those pieces of information while asleep than awake - if true, it's a paradigm shift from the jolted-caffeine-philosophy.

  33. Re:Refactoring vs Sleep/Napping - URL by pg--az · · Score: 1

    Sorry, the URL for the CBSnews videos "Science of Sleep" is http://www.cbsnews.com/stories/2008/03/14/60minutes/main3939721.shtml?source=mostpop_story

  34. Where are my mod points when I need them? by SpammersAreScum · · Score: 1

    Of course, then I'd be in a quandry, choosing between "Funny" and "Informative"...

  35. Perl 6 as a Cautionary Tale ... by joe_n_bloe · · Score: 4, Interesting
    I'd like to focus on the author's comments about rewriting vs. refactoring. From July 25, 2000:

    Last Monday, nobody knew that anything unusual was about to happen. On Tuesday, the Perl 6 project started. On Wednesday, Larry announced it at his "State of the Onion" address at the Perl conference.

    It's one thing to decide to rewrite rather than refactor a product that is losing market share because it is not performing as well as its competitors. (E.g. Netscape.) It's another thing to decide to rewrite (and redesign) rather than refactor a wildly successful and popular product because its continued development has become difficult. Just shy of eight years later, Perl 5 is still creaking along nicely, and Perl 6 (White Elephant Service Pack) is still under design as much as development.

    Is Perl 5 so hard to refactor that a determined effort couldn't have made progress, or been completed twice over, in 8 years? Along the way, a lot of the cruft and inelegance in the language could have been removed, and more elegant features inserted.

    It happens over and over again - developers, even experienced ones, can't see the impracticality of what they're getting into, and can't see that they're doing work that isn't needed.
    1. Re:Perl 6 as a Cautionary Tale ... by Anonymous Coward · · Score: 0

      Just for the record, for some reason you seem to think that Perl6 is a rewrite Perl5. This is laughable. Perl6 is completely unrelated to Perl5 other than the name and certain superficial syntax features. Perl5 continues to be refactored and updated on a (probable) daily basis.

      Perl6 might be an example of lots of things (such as the danger of design by committee) but it's really not an example of rewriting a successful project.

    2. Re:Perl 6 as a Cautionary Tale ... by joe_n_bloe · · Score: 1

      Just for the record, for some reason you seem to think that Perl6 is a rewrite Perl5. This is laughable. Perl6 is completely unrelated to Perl5 other than the name and certain superficial syntax features. Perl5 continues to be refactored and updated on a (probable) daily basis.

      Perl6 might be an example of lots of things (such as the danger of design by committee) but it's really not an example of rewriting a successful project.


      When the name of the product changes from "Foo X" to "Foo X+1" it's safe to assume that the intent is at least a recognizable evolution. In fact, initially, Perl 6 was supposed to be a recognizable evolution of Perl 5. And as you can see by the quote above, the motivation of the rewrite and redesign was not that Perl was useless, but that it had become difficult to maintain and evolve. The fact that Perl 6 is now only superficially related to Perl 5 is another indication of how far adrift the project has gone from its original moorings. Meanwhile, it's evident that Perl 5 is both maintainable and evolvable; my point is that the resources spent on the Perl 6 class project would have been much better spent rewriting Perl 5 internals - at least from the standpoint of users and Perl 5 developers.

      Dramatic evolutionary rewrites of internals are certainly possible. PostgreSQL is the best example of this that I can come up with. And g++.

  36. What? by Peaker · · Score: 2, Insightful

    Create policies like "maximum function call depth" I would rather have a policy of maximum function size (which may increase function call depth) than this policy.

    Do you want to encourage people to inline their functions manually, and not divide things into small, cute trivial functions?

    Is this a misguided attempt to increase efficiency?
  37. Re:Grok it. re: Gnome HIG by Anonymous Coward · · Score: 0

    So..
    Do you think the Gnome HIG was written by a thought #1) project manager or a thought #2) project manager?

  38. The Wodehouse Method For Refactoring Code? by George+Johnston · · Score: 1

    Wouldn't it be to leave it to Psmith?

    --
    Orignator of the Miserable Failure Googlebomb
  39. Re:Screen vs Paper, Memory Retention by Ox0065 · · Score: 1

    I read somewhere (it may be rubbish) that we remember things differently when read on a screen, than when read in print.
    Things read on a screen tend to be more short term memory
    Things read in print tend to be more long term memory.

    I know I print out drawings for checking, because you just see more... ...butts.

    --
    thx e
  40. Another take at 50.000 ft. code visualization... by mAx7 · · Score: 3, Interesting

    In the Complexity Map, a slightly similar approach, a treemap is used to visualize the code's namespace hierarchy in a 2d-landscape. Results from code metric tools are layed out in the treemap, either for individual metrics (e.g. cyclomatic complexity) or for aggregated metrics (anthing that influences team productivity; e.g. errors that are not logged). Due to the Prefuse-based seamless zooming, combined with drill down functionality, it's really easy to visualize and investigate hotspots in extremely large codebases.

    The website contains some more background and a nice interactive demo. If you have the patience to wait for the applet to load, I'll guarantee you you'll like it.

    Disclaimer: I am the author of this tool. The website mentions commercial interest, but to be honest: there's hardly any. I've found that the concept is just too difficult to sell over the web, so I'll probably open source it soon.

  41. Visualize it. by Anonymous Coward · · Score: 0

    "I wish someone made these mindmapping programs and made them more accessable to programs and programming."

    Free Mind and Compendium Software visualization

  42. Yeah, but look in a mirror. by symbolset · · Score: 1

    You're Terje Mathisen.

    For most of us the brilliant flash of insight that allows us to fully integrate a problem is an epiphany - that rare and fleeting moment where our brilliance truly shines. It's neither frequent nor persistent enough to build a plan on which we must rely.

    For you it's more like the rising and setting of the sun.

    That said, yeah, that's what I meant by "grok it". If I had spelled it out that way though someone could justly accuse me of the hubris of believing that I'm you.

    The usual approach is to optimize the 90-95% case, then bail on the remainder, but this will almost always be beaten by code which manages to turn everything into the "normal" case, with no if/else handling, no testing, no branching.

    Thanks for the reminder. I had fallen into a bad habit.

    --
    Help stamp out iliturcy.
    1. Re:Yeah, but look in a mirror. by Terje+Mathisen · · Score: 1
      Thanks for making my day! :-)

      You're Terje Mathisen.

      For most of us the brilliant flash of insight that allows us to fully integrate a problem is an epiphany - that rare and fleeting moment where our brilliance truly shines. It's neither frequent nor persistent enough to build a plan on which we must rely.

      For you it's more like the rising and setting of the sun. It most certainly isn't anything like that, great code is almost always a result of Edison-like effort:

      "10% inspiration, 90% perspiration."

      Great code, at least for me, depends on being willing to "try & try again", something that's only possible when I have the luxury of not working to a deadline.

      Last fall I wrote the worlds fastest Ogg Vorbis decoder, starting from Sean T Barret's public domain code:

      To get the last 30% of speed (to beat the best existing decoder) I had to throw away at least 75-80% of all my optimization attempts, since they turned out to slow it down instead of speeding it up like my insight told me it should do.

      I.e. even when working on what I really love, I can be wrong a lot more often than I'm right. :-(

      Terje
      --
      "almost all programming can be viewed as an exercise in caching"
  43. single class by Anonymous Coward · · Score: 0

    We have a single class that's 30k lines. =(

  44. Harvard shmarvard by Bloke+down+the+pub · · Score: 1

    How do you aggregate the combined efforts of all the teams into a birds eye perspective and how do you know where the boundaries of the project lie?
    Obviously, you need to nurture a holistic learning culture as an enabler for out of the box thinking. This will become an ipso-facto best practice, and in combination with co-synergistic paradigms a strategec transformational ethos will emerge, driving in a virtuous spiral to add stakeholder value.
    --
    It's true I tell you, feller at work's next door neighbour read it in the paper.