Slashdot Mirror


Ask Slashdot: What Makes Some Code Particularly Good?

itwbennett writes: When developers talk about what makes some source code particularly 'good,' a handful of qualities tend to get mentioned frequently (functional, readable, testable). What would you add to this list?

39 of 298 comments (clear)

  1. Obviously by Anonymous Coward · · Score: 5, Funny

    When it's mine.

    1. Re:Obviously by Anonymous Coward · · Score: 2, Insightful

      What makes an article a click whoring waste of time? I don't know, I didn't read past the first for 9 impression generating lazily written dreck, but being posted on slashdot is becoming a good indication.

    2. Re:Obviously by Anonymous+Brave+Guy · · Score: 4, Funny

      Tell that to your six-months-ago self, who wrote the mess you've been debugging all week. :-)

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    3. Re:Obviously by nitehawk214 · · Score: 4, Funny

      I hate it when I have to debug some other asshole's code.

      I hate it more when the asshole is six-months-ago-me.

      --
      I'm a good cook. I'm a fantastic eater. - Steven Brust
    4. Re:Obviously by plopez · · Score: 2

      When it's mine and I am the only one who understands it and so therefore have a cash cow until retirement :)

      --
      putting the 'B' in LGBTQ+
    5. Re:Obviously by stjobe · · Score: 4, Insightful

      I am all over the place, I guess many programmers are.

      Being a programmer is a trade in some parts, and you can get by with good craftsmanship.
      In other parts it's a creative art, and you can't force creativity.

      To write really good code, you need to both have the craftsmanship and the creativity.

      --
      "Total destruction the only solution" - Bob Marley
    6. Re:Obviously by Jane+Q.+Public · · Score: 2

      There *IS* one problem here: efficiency is the last item, while "simple" and "pleasant to look at" are given higher priority on the list.

      Yes, that is a regrettable trend I've seen lately. Yes, code should be readable, and should be easy to maintain. But there is a huge amount -- maybe even majority -- of time when efficiency should be considered before readability or beauty.

    7. Re:Obviously by Jane+Q.+Public · · Score: 2

      I will elaborate just a little:

      I have personally seen code essential to execution time, which was neat and canonical, made literally more than 100 times more efficient in the sense of time and CPU cycles, by using methods that were a little less readable.

      And I mean via testing, not just subjective impression.

      TFA has its priorities skewed.

    8. Re:Obviously by nitehawk214 · · Score: 3, Insightful

      The door wings both ways though. Sometimes six-months-ago-me is a pretty cool guy.

      Like that time I was searching for an answer to a problem on Stack Overflow. At first I thought "Ha, what a coincidence, the top rated answerer has the same name as me." It was not a coincidence... it was me.

      --
      I'm a good cook. I'm a fantastic eater. - Steven Brust
  2. Because it's mine! by DickBreath · · Score: 3, Insightful

    Doesn't every self important developer think that their own code is the best?


    How many managers does it take to reach a decision to begin a study to determine whether a light bulb should be replaced?

    --

    I'll see your senator, and I'll raise you two judges.
    1. Re:Because it's mine! by mykepredko · · Score: 2

      Agreed for "self important developers".

      "Good" developers know that they probably missed things or could have done some parts of it better (and know that unless there is a functionality issue, to not go back and try to fix them).

      myke

  3. must fail by BytePusher · · Score: 3, Insightful

    Code needs to fail in an easy to understand and predictable way. If possible and practical provide descriptive and easy to follow details through logging or return values...

    1. Re:must fail by swilver · · Score: 2

      It is good practice to throw exceptions for even fatal errors. What they've missed however is to add information to the exception about the error (the stuff you propose they should log).

      Somewhere way up the call stack you have a catch all exception handler, which is responsible for logging the exception -- so it is not necesarilly the caller who logs this. If the caller cannot handle the exception, it should just leave it and let it bubble up.

      This way there's no need to duplicate logging code in every small little function that can't handle certain inputs.

  4. When you go back to it several months later by Z80a · · Score: 5, Insightful

    And say "thank you, my past self for making this so easy to understand and versatile!".

  5. Easy to change by Anonymous Coward · · Score: 2

    Code that you can change, and the change does what you expected and did'nt ruin something else in a seemingly unrelated place.

    Doesn't matter if the code is otherwise butt ugly, as long as you can do this, the code is good enough to be workable.

    1. Re:Easy to change by Xolotl · · Score: 2

      I was looking for a comment along these lines .. exactly. Over the years I've found that good code is that which can be opened up months or years after it was written and changed or repurposed easily. Good programmers seem to write this instinctively.

  6. a few heuristics by Sneftel · · Score: 3, Insightful

    Good code feels obvious and self-evident, as though its design springs directly from the problem, rather than from the cleverness of the developer.

    Good code is free of regret; regardless of how much it's been modified and refactored, it feels as though it was written in a single sitting, by a developer who somehow knew the right way to do it already.

    Good code is not just readable, but inviting. It feels as though there is no wrong place to start reading it.

    Good code doesn't have a single goddamn class named "Manager".

    --
    The opinions stated herein do not necessarily represent those of anybody at all. Deal with it.
    1. Re:a few heuristics by avandesande · · Score: 3, Informative

      Good code is boring and bland, lacking excitement and surprises.... and mostly goes unnoticed.

      --
      love is just extroverted narcissism
    2. Re:a few heuristics by bensch128 · · Score: 2

      I hate to burst your bubble, but every example of component based programming that I've seen uses managers to access and control the individual components. You cannot use components otherwise. (At least, not in a memory restricted environment like a video game)

      If you have ever worked on a large video games, then you know exactly what I am talking about.

  7. What would you add to this list? by QuietLagoon · · Score: 3, Insightful

    That it works.

    1. Re:What would you add to this list? by stjobe · · Score: 2

      To paraphrase Torvalds:

      If it compiles, it's good. If it runs, ship it.

      --
      "Total destruction the only solution" - Bob Marley
  8. GOTO by s.petry · · Score: 3, Funny

    Lots and lots of GOTO NNNN and I know it's good code!

    --

    -The wise argue that there are few absolutes, the fool argues that there are no probabilities.

  9. take it from a pro by nimbius · · Score: 4, Funny

    I think i can shed light on this subject, having several years experience in this field. New programmers and seasoned alike often make this mistake, either through carelessness or ignorance. When working to write good code, you must make sure to set good="yes" or good="veryyes." ive written code for 20 years now and this has only ever failed me in PHP. Apparently the language does not support "good" code.

    --
    Good people go to bed earlier.
  10. Rhyming variable names by gurps_npc · · Score: 3, Funny
    Yes, that is the key measure of how good your code is.

    If you have no rhyming variable names, then your code sucks. 1 stars

    If you have one or two, it is barely acceptable. 2 stars

    Half rhyme, then 3 stars.

    If most, but not all, rhyme, 4 stars.

    If every single variable rhymes with at least one other variable, you have a great job, 5 stars.

    To get the coveted 6 stars, all the variable names have to rhyme with each other.

    --
    excitingthingstodo.blogspot.com
  11. diversity! by digsbo · · Score: 3, Funny

    When it's written by a gender and race balanced team, in a positive, multicultural environment, then it's good code!

  12. I like eligant code by Karmashock · · Score: 2

    That is... as brief as it can possibly be to accomplish the stated goal. Very simple.

    I also like it when the code is commented properly so that every operation is broken down and and annotated to explain what it is doing.

    This lets me go through the whole program and see what it is doing and the way it is doing it very quickly.

    The code that makes me crazy is the rube goldberg code that does some thing very simple in the most elaborate way possible, has no annotations so I have no idea what the actual point of the code is, and then to make things worse various parts of the code will randomly switch between doing different things for no reason.

    THAT code I hate. I see that shit and I delete it and write over myself.

    Part of the problem is that people don't know how to do somethings. They understand the language but they haven't memorized the really short code snippets that are the ideal way of getting from point A to point B. And that's just something a programmer should either have memorized or stored somewhere for recall.

    Using those code snippets tends make the code far more elegant and much more readable because you can see beyond the annotations what they're doing because you recognize that snippet.

    If they never use them, that is fine too... just so long as they don't make their code look like that breakfast making machine from Chitty Chitty bang bang. If they do that, I will murder them all. :-D

    --
    I've decided to stop wasting my time responding to AC trolls/sockpuppets... so if you want a response from me... login.
  13. Re:Good code by orasio · · Score: 4, Interesting

    For newly written code, things like readability, testability, and maintainability all can come in to whether it is "good" or not

    For legacy stuff, Good code is code that works. Who cares how easy it is to read or test as long as it works?

    The second one should also include "immutable". If it's hard to understand it will evolve easily to non working, and time spent on improvements can start to creep up very fast.

    I have worked in very clever, solid code, but not easy to read. It was then maintained and extended by average, but competent programmers down the road, and turned into a big mess, only because it was so hard to understand.

    In my experience, good code is easy to read, above all. That will make it easy to extend it coherently, find bugs and stuff. Also, if it doesn't work OK, it's easy to find out why. The single metric that saves time, money, and improves quality down the road is readability. Eveything else should be suject to that.

    And, about the last point in the "article", "efficient", it's nonsense. Premature optimization is the root of all evil. You should _always_ follow the second rule of optimization (see http://c2.com/cgi/wiki?RulesOfOptimization ).

  14. Re:Compactness and Readability by Yunzil · · Score: 5, Insightful

    It communicates clearly what it is trying to do.

    It doesn't, actually. Looking at that as someone who doesn't know what a CRC32 is I have no idea why it's doing what it's doing. Some actual useful comments might be nice.

  15. Time by El_Muerte_TDS · · Score: 3, Insightful

    After 5 years you can still read it and understand what it was supposed to accomplish, and it does so.

  16. chocolate. dark chocolate. by swschrad · · Score: 2

    not to be confused with code that sorta looks like it, but surely doesn't smell like it...

    --
    if this is supposed to be a new economy, how come they still want my old fashioned money?
  17. Simple by Elixon · · Score: 3, Insightful

    I would definitely add "simple". Everybody can write complex code but it takes experience and great knowledge to be able to choose the best fit for the implementation. More experience and knowledge you have more options to choose from. Beginner will usually go with the first hunch that will get complex sooner or later as he will meet challenges he didn't expect...

    So yeah, simple, readable, documented, functional, consistent...

    --
    Well, I've got to get back to work. When I stop rowing, the slave ship just goes in circles.
  18. It's not that hard. by paavo512 · · Score: 3, Interesting

    Good code has documentation for knowing what it should do, and has unit tests to verify that it actually does that. If there are any problems good code can be modified to meet the (possibly changed) requirements better, while unit tests ensure the modifications do not make the code worse. Code which cannot be modified is not good code. It's that simple.

  19. It's good if they don't code like 90s C++ devs by gestalt_n_pepper · · Score: 4, Interesting

    Their whole mindset still makes me want to puke.

    Obfuscated crap. Techno-machismo teens playing games trying to get their code into the least number of characters and the least amount of memory. I've had to fix or test so much of this junk and it's still just plain stupid.

    The *human* part of the system is what *matters* and that includes the code interface. First, I want comments not about *what* is happening. I can read that. I need to know *why* it was done, so I don't undo it, or I can do something different safely. Comments should be one liners, limited to "why" and sometimes "how".

    If adding a variable aids readability, add the fucking variable! Shove all the results into a meaningful, readable variable name and then shove *that* into your function argument, not some long series of nested function. It's not the 90s. You don't have to save memory! Memory is there to make your code readable. Use it!

    Ok, rant over. I'm going back to work now.

    --
    Please do not read this sig. Thank you.
    1. Re:It's good if they don't code like 90s C++ devs by Voyager529 · · Score: 2

      Techno-machismo teens playing games trying to get their code into the least number of characters and the least amount of memory.

      Given how many programs I've used over the past decade that have required unreasonably high amounts of RAM, and/or were subsequent releases of software that solved a particular problem in earlier iterations but required 1/10th the memory to do it, I wouldn't mind a handful of these guys getting their desks back. Adobe is a great place to start.

      I've had to fix or test so much of this junk and it's still just plain stupid.

      I'm by no means a programmer, but from the handful of times I've seen what you're talking about, I'll say this: if it's possible to save 10% of RAM by shifting the ease-of-use burden to the comments instead of the actual code, then as an end user, I'm 100% in favor of commenting the everloving hell out of difficult to read code that saves RAM when users are running it.

      You don't have to save memory! Memory is there to make your code readable. Use it!

      Wrong. Use as much as necessary, but no more. I might have 12GB of RAM in my laptop, but it's not all for you. It's for EVERYTHING I do, and when I start adding VMs or large After Effects projects to what I'm doing, 12GB starts to get pretty cramped. Some RAM usage will be inevitable, but wasteful RAM usage is wasteful.

      I most certainly have plenty of admiration for the Demoscene.

  20. Re:Well commented. by Jamu · · Score: 5, Funny

    What if the comment is // this code sucks.?

    --
    Who ordered that?
  21. Compactness / infimum by TeknoHog · · Score: 2

    A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.

    -- Antoine de Saint-Exupéry

    --
    Escher was the first MC and Giger invented the HR department.
  22. Re:It's not polite to talk with your mouth full by bbn · · Score: 3, Insightful

    FACT: The PC to Server ratio is MILLIONS to 1 alone

    This does not stand to even 5 ms of thinking. I am living in a country with 5 million people. You are claiming we have 5 servers or something like that.

    Or if you are an american: How many cities do you have with more than 5 million people? How many cities with more than 5 servers? ...

    Or ... there are about 7 billion people on the earth. Lets assume that each have a PC (many don't). You are claiming there are only 7000 servers on earth.

  23. Depends on the requirements by TMB · · Score: 2

    The best piece of code I ever wrote in my life was nearly 100x faster than the next best algorithm ever written to do that problem. That took the expected run time down from a few days to an hour.

    It was not a particularly well-commented piece of code. If it were, it would have been even better.

    It was not a particularly obvious algorithm for solving the problem. If it were more obvious why one would choose to do it this way, it would have been even better.

    If the primary concern is runtime (because it normally takes days to run), and you literally make it orders of magnitude faster, that's good code. It could be better if it were also better commented and easier to maintain, but those aren't *always* the primary concern (yes, sometimes they are. That's the point -- criteria differ!)

    [TMB]

  24. Re:Compactness and Readability by hcs_$reboot · · Score: 2

    Some actual useful comments might be nice.

    Well, when the function is named calculate_crc32()...

    --
    Slashdot, fix the reply notifications... You won't get away with it...