Slashdot Mirror


The Best Ways To Simplify Your Code? (dice.com)

Nerval's Lobster writes: Technical debt arises for many reasons—whether moving goal posts, pressure to get code tested and released, high programmer turnover, and lack of documentation. Over time, it can also render code a spaghetti-like mess. But how to deal with it? In a new column on Dice, developer David Bolton offers some suggestions, ranging from refactoring to using compiler inference to increase readability and shorten declarations. While those techniques are straightforward, it's clear that a lot of developers let their code get out of control, and trying to plan beforehand doesn't necessarily prevent the work from getting overcomplicated. It seems like every developer has a go-to technique (or four) for keeping things a little more streamlined. What are yours?

17 of 197 comments (clear)

  1. Dice by ADRA · · Score: 1, Insightful

    Dice dice dice dice dice.. oh and add some dice.

    --
    Bye!
  2. Don't try to do too much in one program by QuietLagoon · · Score: 3, Insightful
    systemd fell into the trap of doing too much.

    .
    And then there is always this old chestnut... The Big Ball of Mud.

  3. Step 1 - Don't by mccalli · · Score: 4, Insightful

    No really, do not do this unless you actually need to make changes in the area involved. Diving into old code just to tidy it up brings its own set of risks, and they may simply outweigh any 'niceness' advantage.

    Instead - black box it. Interface it off. Make your new code structured, shiny and gleaming. Do black box-style testing (inputs vs outputs, almost like futzing) against the old code's API and make sure your interfaced version produces the same. Unit test the hell out of your new code.

    Over time, should you actually find a genuine need to, you can then start to look at the black box. Can it be made into a series of smaller black boxes? Can you interface those off too, independently? Cool - do it, one at a time, and use the same approach you used before. Eventually you'll either be left with nicely restructured code or, more likely, nice code that changes often and gnarly horrible stuff that doesn't change ever, but that's hidden behind an interface/library/<insert abstraction methodology here> and works.

    Should always weigh the operational risk of changing ugly-but-known-quantity against shiny-but-unknown-quantity. The path of what to do will be different for every case.

    1. Re:Step 1 - Don't by Fly+Swatter · · Score: 5, Insightful

      Yes this.

      It seems there is always that programmer that decides "I'm going to refactor this bit of code here to make it smaller, faster and cleaner". The actual result is things break (often not immediately) and it winds up taking up even more of said developers time as well as others that use or rely on that code.

      If someone says "I'm going to refactor this bit of code here to make it smaller, faster and cleaner", and they can not give a very good reason to the question "why?", don't let them do it.

    2. Re:Step 1 - Don't by mark-t · · Score: 3, Insightful

      The programmer that decides I'm going to refactor some working bit of code to make it smaller and faster without first having the working version saved in the main code repository is an idiot.

      There is absolutely nothing wrong with refactoring code that is working to make it better, as long as you approach the task incrementally - don't try to do too much in one go, and test and retest each change to ensure that you haven't broken anything. If you do, revert and try to figure out what you did wrong. The time it will save you in the long run when the code is more readable will pay for the time spent refactoring it by more than an order of magnitude.

    3. Re:Step 1 - Don't by gstoddart · · Score: 4, Insightful

      Let me offer a counter point: abstracting, wrapping, deferring, and hiding those black boxes can often lead you to building abstractions and interfaces which promise more than the underlying code can deliver, or which are outright not compatible with it.

      If you run screaming from your real code so you can add a layer of code which allows you to pretend like you know what code you have, sooner or later you do something monumentally STUPID.

      And the more you add layers of pretty on top of layers of ugly, the sooner it will bite you in the ass.

      I've seen (on more than one occasion) someone taking old and ugly code, building a few layers of shiny on top of it, and then start passing around code which explicitly says "WARNING, THIS IS NOT THREAD SAFE" or "DANGER, DON'T EVER DO THIS", and then pass it into stuff which violates those assumptions right up until the code fails spectacularly.

      And then of course people look as far as their abstraction and say "looks good here, we won't check down where the dragons are because our abstraction is awesome and perfect".

      There are dragons, and if you don't understand it well enough to deal with the dragons, you may not be qualified to build a wrapper around the dragons. Because dragons are sneaky and always looking for a way to bit you on the ass.

      I'm not saying don't clean up old code where you can. But if you think piling layers of bad assumptions on top of something you don't understand is a good solution, you're just waiting to get burned.

      Or bit in the ass by a dragon you though was no longer a threat.

      --
      Lost at C:>. Found at C.
    4. Re:Step 1 - Don't by phantomfive · · Score: 3, Insightful

      Let me offer a counter point: abstracting, wrapping, deferring, and hiding those black boxes can often lead you to building abstractions and interfaces which promise more than the underlying code can deliver, or which are outright not compatible with it. If you run screaming from your real code so you can add a layer of code which allows you to pretend like you know what code you have, sooner or later you do something monumentally STUPID.

      You are right, and the GP is also right. The reality is, people who write bad code will write bad code, no matter what 'strategy' they use (and those people will typically use the wrong strategy, too).

      When programmers aren't asking themselves, "how can I write readable code?" then you can be sure their code won't be readable, even when they follow all the guidelines.

      Also, if the programmers on your team are writing bad code, you either need to teach them to do better or leave the company because it doesn't matter how brilliant you are, you can't clean faster than the other programmers make a mess.

      --
      "First they came for the slanderers and i said nothing."
  4. Fuck dice by Lunix+Nutcase · · Score: 3, Insightful

    The best way is likely by not reading a Dice article.

  5. fuck off dice by Anonymous Coward · · Score: 2, Insightful

    But seriously, the main problem is that many developers don't give a shit about the quality of their work and no one notices or does anything about it. It's not a matter of learning a technique.

    If there's no rigor around what you write or how it gets tested, software will turn out shitty. If the development issues can't be fixed, it's often easier to just lie to the client or use hacks and workarounds to hide problems.

    The fault for this lies with management. You need to police your developers but also treat them well enough that they're invested in writing good stuff. You need to hire good people and provide your mediocre developers with good examples to follow. If your developers think they're going to be fired long before they have to maintain anything, they'll probably turn the shittiness up to 11 out of spite. if they anticipate quitting over compensation or overwork or work-life balance, how good is their code going to be? If they hate your guts because they're on a visa and getting taken advantage of, how good is the code going to be?

  6. I don't. by Anonymous Coward · · Score: 1, Insightful

    It seems like every developer has a go-to technique (or four) for keeping things a little more streamlined. What are yours?

    You get the job done as fast as possible and let the Indians worry about it when they get it.

    See, you'll be on a project with an impossible deadline; all the while upper management has been wined and dined by the Tata/WiPro/CSC sales people and are gonna offshore the project for the production and maintenance phase. And many are just offshoring everything.

    So, get your stuff done on time, learn everything you can and find another job. Because if you stick around, you'll be canned and when you are unemployed in this profession, you are unemployable - if you're any good, you'd have a job.

    And do what you can to get into management before you're 35.

    And if you're really smart, you won't fall for this STEM career hype.

  7. When your question gets closed by SO by phantomfive · · Score: 4, Insightful

    When your question gets closed on Stack Overflow, then try to ask it on Slashdot?

    Frankly, if your code is constantly getting out of control and becoming unreadable, you are not doing professional work, you are doing amateur work (and yes, I know that there are plenty of paid professionals who do amateur work. We've seen stories from Trend Micro programmers here recently on Slashdot).

    It's not a set of rules to follow. If there were a mindless algorithm to follow, then a computer could do it. Instead, when you are about to write code, always ask yourself, "How can I write this in a way that future programmers can understand it?"

    --
    "First they came for the slanderers and i said nothing."
  8. Re:How to simplify your code... by PRMan · · Score: 4, Insightful

    Think more. Write less.

    Writing less code has many benefits:

    • Since bugs are a function of lines of code, you write less bugs
    • Since your code is shorter, other people can read and understand it easier
    • You will end up using more libraries, which have been well-tested by others
    • Your code will fit in the CPU cache, making it even faster

    Ideas that involve writing a ton more code to do X always backfire, the latest being TDD/IOC. That stuff is nearly impossible to read and maintain.

    KISS (keep it simple, stupid)

    --
    Peter predicted that you would "deliberately forget" creation 2000 years ago...
  9. Re:How to deal by phantomfive · · Score: 4, Insightful

    I think the code is a representation of the person. People who think clearly write clear code. Learn to think clearly.

    --
    "First they came for the slanderers and i said nothing."
  10. Re:Dice sucks dick. by ShanghaiBill · · Score: 4, Insightful

    When I take shortcuts because I am in a hurry, I try to leave breadcrumbs. I put comments that start with the string "FIXME" and then write a quick sentence or two explaining how I would do it if I had more time. Then later, when I do have time, I can just grep for "FIXME" and find a list of things that need to be fixed. Those comments save me a lot of time, because I have an explanation from when it was still fresh in my mind, preventing many "WTF moments".

  11. Instead of click-bat here are some real sources... by AlanBDee · · Score: 4, Insightful

    These books provide much better information on the topic and deserve a place on your shelf alongside the GoF's Design Patterns.

  12. Re:Dice sucks dick. by fph+il+quozientatore · · Score: 2, Insightful

    Dice.com sucks big dicks.

    +4 insightful? Is everyone with moderation points 14 year old?

    --
    My first program:

    Hell Segmentation fault

  13. Re:I don't understand. by Anonymous Coward · · Score: 5, Insightful

    I think what everybody is hating on is the undisclosed relationship. Nerval's Lobster is a shill account. This is not ethical business practice.