Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming IT Technology

Beautiful Code Interview 286

An anonymous reader writes "Safari Books Online has just posted an interview with Andy Oram and Greg Wilson, the two editors who put together the recent O'Reilly book, Beautiful Code. "Beautiful Code" features 33 different case studies about challenging coding scenarios from some of today's most high-profile developers and OS project leaders. There's also a new Beautiful Code web site based on the book where many of the authors are blogging about their work and coding practices."
This discussion has been archived. No new comments can be posted.

Beautiful Code Interview

Comments Filter:
  • by jimmyhat3939 ( 931746 ) on Wednesday August 08, 2007 @07:06PM (#20163605) Homepage
    But I find goto is often as beautiful as it gets:

    for (loop 1) {
          for (loop 2) {
                if (something happens that makes me want to bail on both loops) {
                      goto loop_done;
                }
                do_inner_loop_work;
          }
    }

    loop_done:

  • by jimmyhat3939 ( 931746 ) on Wednesday August 08, 2007 @07:23PM (#20163771) Homepage
    Personally, I think that is uglier than goto. Also, I think it's more confusing, because you have this condition repeated twice, and you'd have to have a "break;" in the right spot in the inner loop, as well as some sort of a condition check right after you get out of the inner while loop to ensure the remainder of the outer while loop does not execute once the condition is met. Yes, you can do this a lot of different ways, but you have to torture the while/for syntax to avoid goto.
  • by RootsLINUX ( 854452 ) <rootslinux@NoSPaM.gmail.com> on Wednesday August 08, 2007 @07:28PM (#20163819) Homepage
    How about this as an alternative:

    bool continue_loop = true;
    for (loop1 && continue_loop) {
          for (loop2) {
                if (termination condition == true) {
                        continue_loop = false;
                        break;
                }
                do_inner_loop_work();
          }
    }


  • Re:Have to say... (Score:5, Insightful)

    by Reality Master 101 ( 179095 ) <RealityMaster101 ... m ['gma' in gap]> on Wednesday August 08, 2007 @07:28PM (#20163827) Homepage Journal

    Now that I've been in the real world, I have to say that I don't agree.

    I have to respectfully say that if you believe this, you haven't written 1) enough code, and 2) complex enough code, to have filled up your brain sufficiently to where you can't remember what the hell you were thinking at that time. When you've reached that level of programmer maturity, THEN you will understand the importance of comments. :)

    Never mind trying to blaze the trail for programmers that come after you. I also predict that you haven't tried to unravel another programmer's crappy code.

    The problem with comments is that you now have two things to maintain, the code and the comments

    Yes. People who change code but don't update the comments should be flayed appropriately.

    I find that (for me at least) I have the greatest success with short, composed methods that do one thing and one thing well all backed up by unit tests that test behavior and requirements, not simply that foo() returns 15.

    Testing and commenting are two different subjects. Comments are not to tell you that "foo() returns 15", comments are to tell you the *context* of code, how it fits in with the overall goal of the subroutine.

  • by intx13 ( 808988 ) on Wednesday August 08, 2007 @07:33PM (#20163861) Homepage
    Most code is beautiful at one point in time - namely, when it's first written. A decent programmer can produce some decent code that performs the task at hand elegantly. With a little work this can become beautiful. Most applications I write start out very elegant, beautiful, commented, clever, etc. - It's only after the project grows and I'm working on a file named "main4.3.a.iii.bak2.worksithink.c" that the comments turn into "// why is this here?" and the variables go from "nDBEntryCount" to "temp" and the code becomes an ugly mess.

    The real trick is DESIGNING the application in such a way that it can grow gracefully, and STAY beautiful. And that's really tough - knowing what sorts of features and requirements the future will hold is difficult. A big part of this is the language itself - I love assembly languages, and I could write some really clever and beautiful assembly code. But when the requirements change and the code needs a new feature? There goes all the carefully timed loops and cycle counts!

    Beautiful code is as much beautiful, expandable, future-proof design as it is beautiful implementation.
  • I've become jaded (Score:5, Insightful)

    by syousef ( 465911 ) on Wednesday August 08, 2007 @07:48PM (#20163989) Journal
    Is anyone else jaded by these books that go on and on about why a particular techique or code snippet or methodology is "right" or "beautiful" or "the way forward"?

    I look at some of the code mentioned and yes it's neat. Some of the code snippets from these books (not just this one specifically) is either really obvious or makes me want to blow chunks because it's an over-complication or over-simplification just to demonstrate a technique which you know will be over-applied and end up in some set of corporate standards that sees it being misused. ...then there's some of the frameworks and methodologies out there that are generally worshiped as God's own code, but which when you try to use them turn out to be cumbersome, horrible, unintuitive messes. Years later this is suddenly "discovered" (EJBs I'm thinking of you!!!) and a whole new set of horrible frameworks goes through several iterations (Hibernate 1 vs 2 vs Spring persistence, Struts vs Spring MVC) where nothing is allowed to mature for long enough to have the major bugs ironed out.

    Perhaps I'm just getting old but I'm really getting tired of all this. You want to know what makes code beautiful?

    1) It does the job 100% correctly as intended.
    2) It does it as simply as possible - not so simple it doesn't work, and no more complex than it absolutely needs to be...building everything in but the kitchen sink just in case is a fool's game.
    3) It's readable and well documented enough that anyone who knows the language (or better yet a programmer familiar with a similar framework but not this one) understands it.
    4) Its easy and quick to make changes as requirements change - that means GUI tools for GUI development (What ever happened to RAD tools being the norm in the industry!? It can take a week to make significant changes to a web page in Struts or Spring MVC, where it use to take about a day to do it for the clients developed with the RAD tools of the late 90s!)
    5) It fits in well with the rest of the system. A module that works beautifully in isolation but doesn't fit in with the system can ruin the system.

    All the rest is just a bunch of consultants trying to bilk you for cash.

    Yes patterns can help, but they can also hurt.
    Yes externalizing code into config files can make a system more flexible (but you'll pay for it in readability and tracability/debugability).
    Yes aspects of the agile methodology - continual integration and test driven coding - can help but they're not the only way and there's a cost associated.
    Yes Object oriented code offers things that procedural does not, but again there's a cost and your developers better understand the language constructs.

    You need to look at each of the above as tools in your arsenal, not religious doctrine.

    Note that my recent experience is with Java/J2EE so that's where my examples come from but I've worked on dozens of languages and frameworks.
  • by RetroGeek ( 206522 ) on Wednesday August 08, 2007 @08:08PM (#20164145) Homepage
    You forgot "working within the langauge".

    I have seen examples where a person knows langiage A but is working with language B. And by God he WILL try to make language B work like language A, come hell or high water.

    This is where you get "clever code". Which is un-maintainable.
  • Stupid frameworks. (Score:5, Insightful)

    by mattgreen ( 701203 ) on Wednesday August 08, 2007 @08:48PM (#20164481)
    Ugh, Java frameworks.

    Somebody needs to drag the people who make these things in a room, erase their memories, and make them use what they have created. Perhaps then they can start to feel how asinine they can be sometimes. It is as if they get off on how many design patterns, random XML config files, and other "best practices" they can cram into a single framework. "We're switching to using a BuilderFactoryGatewayStrategyFacade." Thanks for the heads-up guys, we were all dying to know exactly how you implemented it! (Don't forget to scatter pattern names all over your code. People have to know you're using them!) All I want to do is integrate such and such framework in with my program. But, no, I have to read the documentation that describes the problem and how exactly to use the framework. Inevitably, they begin spouting off about how "elegant" it is that you can configure exactly which IntFactory to use by hard-coding the classname in a mandatory configuration file that is prone to getting lost at deployment time. (Remember, making objects with just the new operator is a classic beginner's mistake, don't fall prey!)

    The end result is you end up with what should be a fairly simple task (like OO-relational mapping) have 400 page manuals because it ends up doing every little thing that people want to do. In the time it takes you to choose the right framework, download and install the binaries, wade through the required config files, sift through the quickstart, and actually get familiar with how it is done, you could have just written and tested the tedious JDBC code to load and unload an object from the database.

    But, why do that? There's no hype around that! You're not REALLY an enterprise architect until you have twenty different config files that need to be present just to run your product! If it is an enterprise product, it shouldn't be simple to configure!

    All of these products do serve legitimate needs. But the obsessive over-engineering that surrounds them and the religious fervor by which they are declared Good (despite violating the principle of least surprise at every turn) point to fear. A fear that the code you're writing just isn't good enough somehow. The fear that your code is too simple, too straightforward. A worry that that requirement you're meeting is mission-critical, and, mishandled, could threaten the stability of the entire system. This isn't usually the case. It would seem that Java's simplicity sort of drives its hardcore users mad after awhile. What it lacks in expressiveness, people try to make up for by inane configuration and extensibility instead of just sitting down and Getting The Damn Thing Done. Sure, the code is boring. The best code is anything but glamorous.
  • by Anonymous Coward on Wednesday August 08, 2007 @09:03PM (#20164615)
    prints "just another perl hacker"
  • by Crispy Critters ( 226798 ) on Wednesday August 08, 2007 @09:08PM (#20164639)
    So what if they compile to the same code? Source code is for humans to read, and machine code is for machines to read. Comments don't show up in the compiled code at all. Does this mean that they serve no purpose?

    Break's and goto's are very different, and I am surprised to see so many people say that they are essentially the same. When I am reading code and I see a break statement, I know where the flow goes. When I see a goto statement, I have no idea where the flow goes unless the label is withing a few lines. That is the difference.

  • by Breakfast Pants ( 323698 ) on Wednesday August 08, 2007 @09:24PM (#20164739) Journal
    That isn't true at all. What happens in the second case when x == 0.0 ?
  • by compro01 ( 777531 ) on Wednesday August 08, 2007 @09:27PM (#20164757)
    simple code is always beautiful, but beautiful code isn't always simple.
  • by gfody ( 514448 ) on Wednesday August 08, 2007 @10:54PM (#20165321)

    0x05 dollars says that "break (loop-label)" and "goto (post-loop-label)" compile to almost identical opcodes.


    Indeed. As does "for", "while", "repeat" or whatever else your HLL gives you.
  • Re:Have to say... (Score:4, Insightful)

    by Nevyn ( 5505 ) * on Thursday August 09, 2007 @01:43AM (#20166241) Homepage Journal

    The problem with NO comments is that debugging can not determine wither code is correct - it can only find whether two representations of a solution are equivalent.
    ...
    So a good programmer writes TWO versions of his program - in representations as different as possible. (Preferably one optimized for automated translation, one for human readability.) That way he's thinking in different mindsets, greatly reducing the likelihood of making identical errors in both representations

    Who cares about identical errors, you're screwed either way then. The big problem is when the "documentation" and the "code" don't match ... you have no idea which one is wrong.

    As the old saying goes: "The man who has a watch knows what time it is, the man who has two is never sure." ... of course the man who has one watch and a big pile of unit tests which prove it's keeping the right time is doing the best of all :).

    I've been at this for a pretty long time now, and I've found very little use for "comments explaining what the code does" ... but a lot of use for "comments explaining why". And personally, I've gone back to code I've written over 5 years ago and could see what it was doing instantly ... and on the bad side I've read code I wrote a year or so ago and not understood why it was doing something (to be fair, after thinking about it a bit it became "obvious" ... but then I wrote a comment explaining it anyway :).

    Yes, I've read others peoples code that (in theory) would have been easier to understand if it had been heavily commented ... but it would have been even easier to read if they'd just been any good at what they were doing and written the code well.

  • by Anonymous Coward on Thursday August 09, 2007 @08:06AM (#20167963)

    What happens in the second case when x == 0.0 ?

    The program doesn't crash.

    If you want different behavior when x == 0, the appropriate way to do that is with a special case, not with sneaky notation. Trying to do otherwise will just make the code harder to read and introduce other unexpected behaviors. What happens when x is very large or inf or -inf or NaN? Are those behaviors intended or not?

  • by FuzzyDaddy ( 584528 ) on Thursday August 09, 2007 @09:21AM (#20168671) Journal
    No - the lesson is that it's worth not taking your grade so seriously that you muzzle yourself. As it often is in real life, sometimes you pay a price for being a smartass, but it's often worth it anyway.

To the systems programmer, users and applications serve only to provide a test load.

Working...