Slashdot Mirror


Any "Pretty" Code Out There?

andhow writes "Practically any time I hear a large software system discussed I hear "X is a #%@!in mess," or "Y is unmanageable and really should be rewritten." Some of this I know is just fresh programmers seeing their first big hunk o' code and having the natural reaction. In other cases I've heard it from main developers, so I'll take their word for it. Over time, it paints a bleak picture, and I'd be really like to know of a counterexample. Getting to know a piece of software well enough to ascertain its quality takes a long time, so I submit to the experience of the readership: what projects have you worked on which you felt had admirable code, both high-level architecture and in-the-trenches implementation? In particular I am interested in large user applications using modern C++ libraries and techniques like exception handling and RAII."

29 of 658 comments (clear)

  1. New Law? by VGPowerlord · · Score: 5, Insightful

    The cruftiness of source code is directly proportional to the amount of time spent working on it times the number of people working on it.

    Has someone created such a law before?

    --
    GLaDOS for President 2016! "Well here we are again. It's always such a pleasure." -- GLaDOS, 2011
    1. Re:New Law? by hobo+sapiens · · Score: 5, Insightful

      No, submit it to foldoc.org. Also, it needs a corollary, which we'll call The Hobo Tangent: Enhancements are the root of all evil.

      Let's cue the scene...

      An application's code is written by a competent developer and is nice and clean and pretty. He releases the application, and its a success. He gets moved to the next high profile project, and then the application's code gets handed to the maintenance droid, you know, the new guy on the team who shops *exclusively* at Whole Foods, listens to Nickelback, and has stacks of People magazine in his cubicle. He took a semester of VB while at Party U pursuing a liberal arts degree, and so he is a programmer too, you know!

      Then, some PM or business manager who probably gets paid more than the original competent developer gets some bright ideas to make the application do things it never was intended to do. Let's say its a scheduling tool. Well, hey, they say, let's make it ALSO have an RSS feed! And a document repository! And a calculator! Can can you create some little project management software to work with it? A requirements document gets written, and a timeline is assigned.

      Everyone does their little piece, totally oblivious to the steaming pile of dung spaghetti they are turning the application's code into.

      Two years later, the original developer gets called to fix a problem that is simply beyond the dimwits holding the code. Poor guy doesn't even recognize his own application, and for him, it's like getting called to the morgue to ID the remains of a family member.

      And that, friends, is why people complain about bad code.

      Does this post make me sound cynical?

      --
      blah blah blah
  2. Next up on slashdot... by Anonymous Coward · · Score: 2, Insightful

    Is an artist ever happy with a painting?

    Trends and tastes change. If any coder is 100% happy with a project, they're in the wrong field.

    1. Re:Next up on slashdot... by estevon07 · · Score: 5, Insightful

      I couldn't agree more. As I grow older, I've learned there really is a time when something is "good enough" to satisfy known requirements. I find that many applications are over engineered to some pie in the sky version of what's right and good - usually at the expense of simplicity and stability. I've often heard Java folks talk about re-factoring code and that's fine if no one is using your app, but in the event that folks and money are dependent on it, then re-factoring really just increases risk to all involved. The best possible outcome is that no one will notice the changes.

      It's definitely hard for more passionate developers to realize when time to value ratio has diminished to the point that your time is better spent on other projects. There's always one more thing to spruce up or optimize. Having been both a musician and developer, I like to think of my work as a reflection of me. Playing an instrument is similar in that there is always more to learn and practice on any given song, but sometimes you need to put it down and move along to other pieces. Even the best musicians play a variety of songs. I'm sure Eddie Van Halen could have perfected Eruption for the next 20 years after he recorded it, but he decided to spend his time on other works.

  3. Maturity = Mess by loony · · Score: 2, Insightful

    Well, you'll always find people that say a code base is a mess when the better word would be different. I have a if statement with out { and } to close it... Is it needed in C++ for a single line? Nope - does it make it clearer and easier to read? Yes to me - and my friend Chris will tell you exactly the opposite...

    And on a related note - why rewrite? Can't people ever just go for cleaning something up? No cause then you are just doing mindless reformatting - while if you rewrite, you can claim you make it better, faster, whatever... So of course people will say its better to rewrite...

    Finally, all mature code is a mess. If I rewrite it, I concentrate on the core piece of functionality... That is going to be small, lean, pretty and fast... Then the code matures - it gains more features. It gains portability. It is being worked on by many... and suddenly your re-write is no better than the code you set out to replace.

    Peter.

    1. Re:Maturity = Mess by PianoComp81 · · Score: 4, Insightful

      And on a related note - why rewrite? Can't people ever just go for cleaning something up? No cause then you are just doing mindless reformatting - while if you rewrite, you can claim you make it better, faster, whatever... So of course people will say its better to rewrite...
      On one project I'm currently working on, we DO just clean up the code. It's necessary because over time developers (myself included) haven't been following the coding standard for the project. To make the code more readable to future developers, we actually will try to clean up a file or pieces of a file when we touch it (comments, style, magic numbers, etc.). There are a lot files, and it hasn't always been a success (it's a continual battle), but it's better than rewriting it.

      Now, I *have* rewritten a lot of the code on the project, but not because it was "ugly". We had quite a lot of "prototype" code still in the project. Since it was prototype code, it didn't check for or handle error conditions very well (not to mention the endless bugs that have been found due to the prototype code). We've had to rewrite a lot of the code because it was easier to do that than fix the bugs in the code. This usually allows for easier debugging in the future AND gets rid of any of the bugs that were found (the bugs were usually caused by a bad or even completely wrong approach to the implementation).

      The difference is knowing when to clean up the code and when to rewrite it. If a developer just can't understand the code (because it needs cleanup or it's just very complicated), then it should be cleaned up and commented properly. Sure it's tedious, but everyone on the project loves you afterwards because they can suddenly understand the code! If there are bugs and it's obvious the implementation should have been done a different way (for speed, usability, modularity, whatever), then a rewrite might be in order.

      (and of course, as you mention, as time goes on the code starts looking "bad" or "old" again - time for hopefully another cleanup rather than a rewrite)
  4. Why code always gets messier and messier by Anonymous Coward · · Score: 1, Insightful

    1) Cleaning up code is a job to do after the code is done.
    2) The code is never done. Refer to rule one.

  5. Inconsistency by Anrego · · Score: 5, Insightful

    I find the thing that really makes code unreadible is inconsistency. This is particularly true of languages like C++ where there is no well defined one true coding convention. If all your code is in house, this is not such a problem, because you can define your own coding convention and stick to it. If however you are relying on other libraries, chances are your going to end up with one library that names its function like_this, and one likeThis, and another fnct_LikeThis ...

    Worse is when you don't even define a coding convention for the code you throw into the mix. Now you have libraries with inconsistent naming, and multiple developers all using their own favorite notation.

    Additionally, their is inconsistency in the functioning of libraries. Some use function pointers, some work by inheritance, some (like glade) read the export list..

    I'm not a huge Java fan, but I think they have maintainability down pat. Very consistent language, well defined coding convention, and a mature set of defacto tools (JUnit, javadoc, log4j, struts, spring, hibernate, etc..) make it a lot easier to jump into older code because everything feels familiar. In most other languages you have to spend quite a bit of time just decrypting the existing code, and then more time learning the particular API's they've chosen.

  6. python by codepunk · · Score: 3, Insightful

    Just about all code I have seen written in python is great looking stuff..mainly because of
    the imposed indentation and clear language characteristics.

    --


    Got Code?
  7. postfix by hey · · Score: 2, Insightful

    postfix (the mail program) looks pretty nice to me.

  8. Are you kidding? by Anonymous Coward · · Score: 5, Insightful

    Boost is what I call "template madness". It uses template metaprogramming to the max, which (in the real world) means three things:

          (1) It's impossible to debug. You can't read the code. The debugger can't unravel the templated variables and stuff in any meaningful way for you. You can't even step through code, that's doing a supposedly simple operation like memory allocation!
          (2) Some compilers will choke on the code, or compile it wrong in subtle ways due to differing interpretations of some obscure section of the enormous C++ language spec.
          (3) The error messages from the compiler are useless. You have to run them through a filter to even figure out what they mean.
          (3) Bugs in the library are very difficult to fix. Template metaprograms are essentially programs written in a functional language, except one that has horrible syntax. This is not the stuff that normal C++ programs are made of.

    Your mileage may vary. My day job is working on a game engine for an upcoming Xbox360 game. Engines are hard enough without impractical crap like template metaprogramming in them. Give me straight-line C/C++ code any day.

    1. Re:Are you kidding? by JNighthawk · · Score: 5, Insightful

      Mod up. AC knows what he's talking about.

      Even beyond that, Boost fills up the symbol table quick as hell, because of how templated it is. I worked on a project that used LuaBind, which requires Boost, extesnively. We eventually had to swap over from Visual Studio 2003 to 2005, because of Boost's templating filling up the symbol table. Visual Studio would just fail to compile. Lua's hard enough to debug as it is, but tossing Boost on top of it made it impossible.

      --
      Wheel in the sky keeps on turnin'.
    2. Re:Are you kidding? by Bryan+Ischo · · Score: 1, Insightful

      Thank you for that, your post is right on the money. C++ template metaprogramming is just a horrible, horrible idea, and because Boost is so fundamentally geared towards template metaprogramming, I have to refuse to ever use Boost wherever I get to make the decision. It's unfortunate because the Boost library is very closely tied to the "future" of C++ (I believe that large parts of it are going to be incorporated in the C++ standard soon, and I suspect that C++ and Boost will become more and more intertwined as time goes on). This sucks because it means that as time goes on C++ is going to get less and less useable. Why can't they just leave a good thing alone?

    3. Re:Are you kidding? by JNighthawk · · Score: 2, Insightful

      It being ugly and hard to debug is a problem of both, but regardless, the problem exists and it causes many of us not to adopt Boost.

      --
      Wheel in the sky keeps on turnin'.
  9. Any app that doesn't need to be rewritten... by 3vi1 · · Score: 2, Insightful

    "Any app that doesn't need to be rewritten hasn't grown sufficiently beyond its original intent." - Jesse Litton, 1990

  10. Re:Understanding != Writing Code by nonsequitor · · Score: 2, Insightful

    "I've seen plently of good programmers declare code 'ugly' because it had a few warts but in reality they just couldn't understand it."

    If you can't understand the code it's the author's fault. That is if you have mastered the syntax of the language of course.

    I'm currently working with an embedded code base that was written for job security. Everything is somewhat elegantly organized, but nothing is commented and functions are usually 200 - 500 ELOC. There aren't any notes explaining module interactions with module descriptions or example usages. Considering the project size is around 25,000 lines that makes the code very dense. Sure in 6 months I can document it and have it usable for a team again, but if I don't have time for fixing bugs or adding a feature, contractors will need to be able to come in and work on it with a minimum of rampup time. 6 months is not acceptable.

    Not everyone needs to understand the derivation of every algorithm used, but they shouldn't have to perform the work a second time to verify its accuracy. Unsustainable code is ugly. If the average programmer can not read the source with documentation and understand its purpose, then the original programmer is at fault for not expressing themselves in a way that others can understand. This is why senior programmers make coding standards and do design work.

  11. Pretty code can be found here by Okian+Warrior · · Score: 5, Insightful

    Phoenix Technologies used to make both BIOS and printer software. The printer software department split off and became a different company, and then I lost track of them...

    They made printer software that went into virtually every printer not made by HP at the time. Canon, Ricoh, Lexmark, or whoever would come out with new hardware and license the software from Phoenix. Yep, some of my code is in every Lexmark printer right now.

    They had a couple hundred thousand lines of code that did PCL, GL, and Postscript for the consumer market, and it was the most readable and well developed code I have seen. Comments were explanatory, variables were well named, and execution paths were well defined and easy to follow.

    They really had their act together for testing as well, with an elaborate and comprehensive regression suite that checked *every* aspect of all of the [printer] languages, and a team of QA people who would go over the results nightly. I'm not making this up - you would come in to work in the morning and there would be maybe 5 E-mails from QA outlining bugs which were either in your code or assigned to you for reasonable reasons.

    We did the software for the first Lexmark printer. The first internal release gathered 900 bug reports from QA. When we went to market there were 7 remaining, all of which were deemed inconsequential.

    When you are in the commercial market making fixed-program computers (dishwashers, printers, cell phones, VCRs) you don't have security updates and new versions, and a recall is usually out of the question. It's much cheaper to do all of your QA up front and ship a quality product.

    In my opinion we've grown sloppy in the programming business. I've been a contractor for the past 30 years and I haven't seen anyone else who comes close to true quality procedures. Even FAA safety certified stuff is usually hokey and obscure. Thank god we've still got human pilots.

    Having seen the procedures firsthand I have an appreciation of how easy and valuable it all is. No one else seems to understand that, and so everyone keeps running around putting out fires and slipping deadlines.

  12. Re:Code prettyness only at module level by billcopc · · Score: 2, Insightful

    If that's truly how your code works, you need to fire your boss!

    Every project should budget resources to future-proof the actual code by cleaning it up and writing adequate documentation, and I don't mean a cookie-cutter paragraph for every single function. I mean do what needs to be done to ensure that the next time you have to work on that code, you won't waste twice as much time re-learning what it does.

    Managers who ship code the minute it compiles are either assuming the project will never be revisited, or (most likely) they are complete imbeciles.

    --
    -Billco, Fnarg.com
  13. Re:im hesitant.. by Cassini2 · · Score: 4, Insightful

    My take on C++ is that the best programs only use a fraction of the features. The language is so big it is dangerous. Just because a feature exists in the language, does not mean it is good for every application. I am very wary of operator overloading and templates too. You need to make your code sufficiently clear that you can be sure it works. if you cannot quickly understand your code, then chances are you made a mistake.

  14. Comments lie. Code never lies. by rs79 · · Score: 2, Insightful

    "And don't forget that postfix is well-commented,"

    In all fairness, nobody has ever cashed in on Bernstein's security guarentee. There have been some oopsies with postfix.

    I think Bernstein's code is as nice as it gets. Course, Dan is polite to me too; so maybe I live in an alternative universe.

    --
    Need Mercedes parts ?
    1. Re:Comments lie. Code never lies. by rs79 · · Score: 2, Insightful

      It's not clear to me an actual case has been made that warrents the $500.

      Moreso, the "bugs" describes above can be "features" to somebody else. I appreciate the brevity of Dan's code; C++ LongVariableNames make me throw up a little in my mouth.

      What's wrong with replacing the Standard C Library? Now you know what you're dealing with. Exactly.

      If your C program exits and doesn't deallocate memoru your O/S is broken.

      Given the number of sites that run djbdns and qmail and the number of CERT advisories against them (zero) I really can't fault it too much.

      The BIND code has lots of descriptive stuff in it. And lots of CERT advisories.

      You might find when you've written a very large amount of C code that it has to make sense to you; in the context of the code itself the names do mean something, just not to the casual observer.

      --
      Need Mercedes parts ?
  15. Re:good source by jerryasher · · Score: 2, Insightful

    I came in here to say exactly that. The design and source to Tcl and the source to AOLServer are actually clean, clear, and elegant. I think much of that is thanks to the Tcl/Tk Engineering Guide.

  16. Re:Nice three things ya got there. by Bryan+Ischo · · Score: 2, Insightful

    By the way, your response is ridiculous in many ways. I don't feel like I should have to point these obvious things out to you but I guess I do:

    1. The GP sounds like he understands template metaprogramming very well. Nothing about his post suggests that he either a) says that he is an "expert" (as you are suggesting he does), or b) *isn't* enough of an expert to have formed a valid opinion on the subject.

    2. Clearly the GP used the word "impossible" to mean "very difficult". You're being pedantic by suggesting that he literally meant it is impossible to debug.

    3. It's only because of lots and lots of dedicated effort by Boost developers that Boost's very-complex template metaprogramming techniques work on a wide variety of popular compilers. I would be willing to bet quite a bit that they had to jump through many hoops to find least-common-denominator solutions that worked on all compilers. I believe that this makes the GP's point perfectly. This kind of programming relies too much on subtle complexities of the language definition and while the Boost authors were willing to put lots of work into making these things work on various C++ compilers, I wouldn't want to do that and wouldn't choose to use a library that may force me to do the same (which might happen if I decide to extend Boost's implementation).

    4. Once again by "useless" the GP clearly meant "requiring much more work to decipher than other more conventional error messages". Once again you dismiss his point by interpreting it completely literally.

    My rule of thumb as a developer: whatever you do, make sure that the next guy who wants to work with it doesn't have to get a Ph.D. in the subject in order to continue your work. In other words, keep your implementation simple and as mainstream as possible, so that it can easily be picked up by anyone. Template metaprogramming fails this utterly.

  17. Re:Firefox by vrmlguy · · Score: 2, Insightful

    I've been coding for 35 years. The only code that I've written was written by (surprise, surprise) me. The code that I've read has been written by hundreds, if not thousands, of people, who out-number me considerably. But wait, you say, you were talking about reading about writing code. However, much of the code that I read is at on-line sites like Perlmonks and O'Reily and even MSDN, where each bit of code is accompanied by detailed explanations of why it was written the way that it was. If that's not reading about writing code, then I don't know what is.

    --
    Nothing for 6-digit uids?
  18. ...and Knuth should know by toby · · Score: 3, Insightful

    As the inventor of "literate programming"[1], early practitioner of open source, and author not just of The Art of Computer Programming and its included programs, but some extraordinarily elegant and widely used software systems himself (including TeX and METAFONT). How many people's programs are worth printing as hardcover books?

    [1] mention also to Kernighan & Plauger's Software Tools.

    --
    you had me at #!
  19. Young Programmers... by Organic+Brain+Damage · · Score: 3, Insightful

    ...almost always want to re-write old code from scratch.

    Almost always without taking the time to understand what that old code does. Why? Because writing code is much easier than reading code. Reading code takes perseverance and ability to focus on large numbers of nit-picky details. Something our TV-age brains cannot easily do.

    The result of throwing out the old code without understanding what it is accomplishing is not always positive from a business perspective.

    Sure, sometimes crufty code is crap.

    But sometimes, like on a terminal emulator project I worked on in the mid-90's, the cruft was a bunch of code, accumulated from 1985 to 1995, that actually emulated the bugs in the firmware of 10 different manufacturer's dumb terminals. The programmers who wrote the applications that ran on these dumb terminals relied upon these bugs in the firmware and when the bugs disappeared, the applications broke.

    The company that tried to sell the "correct", "new", "elegant" terminal emulator hit a big solid brick wall called "market acceptance." The company that kept the cruft made roughly $4 million per year in profits and supported 25 employees' and their families for a decade while they developed new products.

    So, before you look at code in a shipping product and say to yourself "this is crufty crap and should be re-written from scratch" ask yourself this question: "Do I really understand what this crufty crap is doing?"

  20. The Tandy CoCo by Fantastic+Lad · · Score: 5, Insightful
    Limitation is the Mother of Pretty Software.

    I remember when that cute little home computer came out, and all the programs were just so. . , plinky.

    Memory was a huge barrier, because you only had a small quantity of the stuff, and nobody understood the architecture of the system well enough to produce efficient programs.

    But back then, there were no video card upgrades. No faster processors and mother boards being produces every three months. If you wanted higher speed and cooler graphics, you had to write your code in more ingenious ways.

    And so that's what happened.

    By the twilight years of the Color Computer, the games people were writing on that thing were unreal. I remember looking at a few and thinking to myself, "This is the same computer? Wow! Humans rock!"

    When you reach the raw power limitations of your muscles but you still want to improve yourself in your combat skills, you take up Kung Fu. That's how it was in the old home computer days. Nowadays, though, (dang kids; I hadda walk fifty miles to school!) it seems that the bulk of improvement comes with the purchasing of increasingly large muscles.

    This is not to say that there is no software innovation. Heck, id Software did some pretty amazing things with software ingenuity. But I do remember thinking during the first few years of the big PC revolution, after the 486 was reaching its twilight, "You know, all this hardware innovation is great and all. . , (big muscles are cool), but part of me wishes it would stop cold for six solid years just what would happen when the programmers were really pushed. --You know, to see what one of these machines is actually capable of doing.


    -FL

  21. Re: Any Pretty Code Out There? by Xernon · · Score: 2, Insightful

    As you note, the answer is mostly no. In my experience, it is the experienced people who are more likely to complain about code quality, as they can better recognize it and value it more. I just wrote an article about the need to make code more 'pretty': Why You Should Polish Your Code. One code base that has consistently impressed me is the Spring Application Framework, which is open source and written in Java (I'm primarily a Java developer). I've looked at other popular open source Java libraries and generally have not been impressed by their code quality, so I don't think that open source is any guarantee of prettier code.

  22. Re:Firefox by Gorshkov · · Score: 2, Insightful

    Reading more about writing code than actually writing code would imply very little coding experience, or a vast desire for redundancy, or a career in software engineering research.
    And spending more time reading code can go a long way into impressing on somebody the importance of writing it clearly in a manner that makes it maintainable and easily understood.

    So many programmers in the world need to have their keyboards substituted for a pair of reading glasses so they can learn some basic competence.