Programming Mantras
Okay, so I did the life ones, and now I'm remembering all my
programming rules of thumb. Here ya go:
- If you type something twice, you're wrong.
- (Credit to Scheme jock Eric Hilsdale for this wording.) Any time the same code appears twice, that's bad. If that code is a subroutine that's been cut and pasted, you need to refactor. If it's a common idiom (ex. try/catch/finally), then it should be a macro. Doing repetitive tasks are what computers were invented for. Free up human processing time for solving new problems.
- Data duplication is the root of all evil.
- Similar to the first rule, but more focused on data representation. If you have a bit of data expressed in two places, you have to spend time ensuring they stay in sync. The duplication will be a source of bugs, and a drain on your time, and your program will be more complicated because of it. Pre-calculate, cache, or agregate data only after careful consideration, and only for performance reasons; it will never make your program easier to maintain.
- Name your variables well.
- Someone is going to have to read your code later on. That someone is usually you.
- Never return null.
- The contract of most methods implicitly states that the method
will return something useful.
nullis almost never useful. If you're method may return null, consider checking for this and throwing an exception instead. - Fail fast.
- When something's wrong (empty result set from the database, no network connection, unparseable user input) don't think, don't hesitate, just throw an exception. This makes debugging easier, and leads quickly to more robust code.
- Fix bad code as you find it.
- A lot of project managers out there will tell you that you shouldn't be spending your time re-writing code. Your job is to make money for the company by writing new code. This is true. But it's also true that bad code costs money in wasted developer time. Use your best judgement.
- Don't be careful. Be bulletproof.
- Whenever I hear anyone say, "We'll just have to be careful to...", I know that somewhere down the line, someone will forget to "be careful", and I might have to come in at 2am to fix the problem. Anything you have to "be careful" about should be automated.