How NOT to Code

Feb 05, 2010

I've been extremely busy this week, so sorry no coding challenges. I'll be starting up again next week. In the mean time, I would like to take a moment to show how bad coding has a hugh impact on an industry and technology. Case in point are the large number of comments written to Kevin Lynch's post earlier this week about the Flash on the iPhone issue.

You can see a ton of comments from people who describe their negative experiences with Flash based applications. Their frustration is very clear and it could possibly affect many of us if this continues. ColdFusion faced similar problems back in the 90s. Many early applications written in CF were resource hogs and frankly shouldn't have seen the light of day. Partly as a result, the view of the industry to ColdFusion is pretty negative. I always try to point out that there are badly written PHP, ASP, .NET apps as well and it's easy to blame the language when it's clearly bad coding that is at fault.

Flash has had a similar history from the 'skip-intro' era to designers trying to write apps with the Flash timeline. Flex, I think, has done a lot to help, but there are plenty of examples of badly written Flash based applications to this day. This blog series 'How NOT to Code' is designed to help correct this problem.

Bad code exists. Every language/platform suffers because of it, and I know I may be preaching to the choir with this blog post, but I can't explain how important it is for all of us to help prevent these problems. Here  are some basic rules or guidelines I hope you can use.

1. The code is to blame. Bad code simply yields bad results. You can try to blame the issues on the hardware, language, anything. But in 99.9% of the cases I run across, it's simply bad code. Simply put, always accept responsibility if it's your fault.

2. Everyone writes bad code. I do it, you do it. The top gurus do it. We're all human and we all write bad code from time to time. The key is to realize when it happens and to fix it as soon as possible.

3. Test, test, test. Look into unit testing and proper usability testing. Create a procedure to test your applications under load and optimize your code. Remember, fixing a bug after the fact takes far more time and resources and can easily kill your productivity.

4. Never accept subpar performance. This is trickly but it's also vital. You may be doing your best, but all the developers on a team need to be dedicated to the process. Managers also need to understand that sending code out quickly will cost more over time. Take a small amount of additional time to ensure quality. For example, I love watching Gordon Ramsey's Hell's Kitchen and Kitchen Nightmares. You can apply the same principles to any discipline. Quality always comes first, not speed. Once quality is ensured, efficiently will develop in time with experience and under proper management.

5. Know your limits. In many cases, I see the wrong tool being used to addressed a problem or a very first attempt to solve a problem being applied. Let's face it, we all know when we have reached our limits. We copy/paste someone else's code or we struggle to work through the problem. This is the point we can cause the most damage. Make your best first attempt, but don't even think of trying to use it. If you have other team members, have them try to develop their own solutions. Once this first round is done, compare the ways everyone tackled the problem. If you don't have team members, ping a coder list, etc. After the first review, make a second attempt - this should be far better. You may be able to use the second attempt, but I would make a point to comment the code or make a note to revisit the problem in the near future. In many cases, I'll revisit the code 6 weeks later and work up a third solution which will work even better.

6. Always revisit code. Management needs to fully understand that time has to be taken to revisit and refactor code. This is trickly if you spend 80% of your time chasing bugs, but it's vital if you want to reduce the number of problems. When revisiting and refactoring code, it's very important to use unit testing. Fixing a bug or problem that produces more bugs is never  a solution.

7. Never write 'large' applications. What do I mean by this? I think most developers mess up here. They get a request to build a large app and things just go down hill from there. Every large application is simply a collection of very small apps or components. That point of view is very important. You want every component of the app to encapsulate itself and to be as autonomous as possible. For example, a shopping cart may have a component that determines the shipping cost. Think of that as a very small shipping cost app that the cart calls. When you develop in this manner, the code becomes more flexible and easier to maintain.

8. Always avoid esoteric code. If only you and god knows and can understand what you coded, you made a big mistake. In 6 weeks, you'll forget what it did as well. This is easy to catch  and you should understand it when it happens. It's usually very large procedural code, and it runs through a ton of changes or operations to handle a simple problem. This type of code either contains no comments or is heavily commented as the coder tries to explain the logic. Again, break the code down into its parts and rework it. Compare other people's solutions to the problem.

9. Comment your code, but don't over comment. Some comments are completely superfluous and silly. Well written code reads like a novel to me. Variable and method  naming should make sense by itself. It may be slightly more to type but it's well worth the effort.

10. Learn, learn and learn some more. Never become a one language developer. Always try learning new languages each year and how they tackle problems. You'll be surprised how much you get from this. A cool thing about this point is this. You actually learn more from your bad code! When you learn a new language - most of your inital code is bad and you make a ton of mistakes. It's in the process of fixing those mistakes that you learn. That's why as developers become more proficient in a single language their skills may actually drop off over time. They solve the same problems with the same tools and never grow.



Matt Bolt

Matt Bolt wrote on 02/05/1011:12 AM

Hats off John - Great article.

My favorite from your list is:
4. Never accept subpar performance.

This is a huge issues with most flash developers, and takes a large toll on stability and scalability.

Overall, there's nothing you wrote that I don't agree with, and I thank you for taking the time to share your thoughts.
John Sieber

John Sieber wrote on 02/05/105:52 PM

I agree with Matt. Great rules to live by and learn from as well. Thanks!
John Mason

John Mason wrote on 02/05/106:03 PM

Thanks, hope it helps.

Kevin wrote on 02/06/104:08 AM

Good points for a software developer. However what do you do when you are stuck with an industry which has come to accept sub par performance. This problem is with developers who want to improve but are stuck with those that don't. And end up fixing other people's problems.

BlackWasp wrote on 02/06/105:32 AM

Good list of points.
Yann Cébron

Yann Cébron wrote on 02/06/109:58 AM

good list, not much to add if everyone would follow these simple rules software would suck much less
John Mason

John Mason wrote on 02/06/1011:09 AM

@Kevin, there's some level of this in many industries. With my work, I end up fixing a lot of other people's mistakes either as a consultant or within my hosting company. But it sounds like you're in a company that is not focused on these principles and seems unlikely to change, then in that case you may need to look for a new place to work. Heck, I would even take a pay cut if there was an opportunity to learn and work with a great team.
erik van nieuwburg

erik van nieuwburg wrote on 02/06/1011:13 AM

Great points. I've been reading the book 'clean code' lately which also discusses all of these issues. I can really recommend it to everyone who wants to take him/herself seriously as a developer.

Point 9 is one which could really need some extra highlighting in the dev scene. It always surprises me how devs can give variables and methods the most obscuring names and then try to compensate that by a lengthy comments. So fail.....
Denis Willian

Denis Willian wrote on 02/11/106:29 AM

Thanks, very good article. I liked the number 3. It's very important, test, test and test. But a lot Employers think that if you are coding unit test, you are losting you time. I'm trying to convince that It's good.


Write your comment

(it will not be displayed)