Monday, April 5, 2010

The Brute Force Programmer Commandments

If the past two years have taught me anything about working with 80 contractors from the school of Brute Force Programming, it’s the following principles. I have taken the liberty of formulating them into a handy Ten Commandments format for your viewing ease and pleasure:

  1. I say unto you, this is the first and greatest of the commandments: figure out what to do, and do it until it’s done. Don’t mess around with frameworks, or methods, or events, or hooks, or closures, or “levels of abstraction”, or other such frivolity. If 3000 lines are required to read that file, translate its values, put them in your ArrayList, print the ArrayList, open the database and store its size there and then return its 19th element...well then 3000 lines your monolithic method shall be.
  2. Verily, art thou not sure something thou needest to use is set up? Set it up again. You know, just to be safe. We might have a valid user, or we might not, so set it up again and you’ll know for sure! You could go examine the other guy’s code to find out what he intends for the program state at this point, but he’s probably as clueless as you and therefore must not be trusted! For your purposes, unknown == uninitialized!
  3. Is thy variable of a nullable type? Always check it for null before you do anything with it, regardless of where it got its value (even if it was assigned two lines above). Strange and mysterious things go on inside computers; who are you to say that the computer hasn’t suddenly decided to collect your object?
  4. If thou be coding along down a tunnel, keep thou focused on the light at the end. Don’t get distracted! If something seems like it could go wrong with code you call, be sure to catch any pesky errors that might arise and suffocate them in a catch block before they can derail you.
    • If you’re following Commandment #3 you’ll be checking everything for null, but what do you do if something actually is null? At this point it’s important not to interrupt the flow that is bringing you to that happy place. Return something sensible. For instance if your method is getFinalPrice, and your Product is somehow mysteriously null, go ahead and return zero.
  5. Behold, local variables are bad, because what if you want to use the value you calculated in them in some other method? Make instance fields! Just remember to stay vigilant in your observance of Commandment #2 and keep those fields constantly initialized.
    • Instance fields are useless should some other class suddenly want to use them if you just make them private. Every one needs a setter and getter!
  6. Spelling? Duh. Compilers can’t read English. Whitespace between operators? A waste of keystrokes. Youcanreadthisjustfinecantyou? The same applies to whitespace between blocks. A Bad Programmer makes thousands of lines of code by repetition, not by cheap whitespace.
  7. If thou be compelled to use local variables, always declare them at the top of your function, even if you don’t use them until the bottom. It’s a matter of principle! Then if you decide you need to use your variable higher up, you’ve saved precious seconds...
  8. Store everything collection of things thou mayest need in an ArrayList. Then when you need one of the items, iterate through the list until you find it. Don’t use HashSet because that can’t be iterated with an old-style for loop. Don’t use List because, what is that anyway? Use ArrayList.
  9. Do everything you can to avoid allocating every object. Seriously, you’ll be able to shave that execution time! It’s just that important. Don’t pass around empty collections, pass around null and check for it everywhere. You’ll save milliseconds!
  10. Thou shalt not covet thy neighbor’s constant. Make your own constant in your own constant file! Your neighbor’s public static final String THIRTY_SIX = “36” is his own and who knows if it will change. You’d better make one for yourself.
Be righteous in your observance of these words, and you will be blessed for all your programming days with particularly brutish and forceful code. Forever and ever, amen.

No comments:

Post a Comment