Something that saves time, money and frustration when building a website is writing clean code. Working on simple code is a lot easier than working on complex code, and the more readable code is always simpler. Simpler code also makes it easier for you to communicate your intent, and to be able to see what a single line of code does and why it’s doing what it does. Here are some tips on writing clean code:
- Always know why you re catching an exception. If you catch an exception you need to have a good reason. Reporting an expected error condition to the user in a more friendly way is a good reason. Handling an expected, but unusual, condition is another.
- Never declare implementation types. Parameters, variable, or even return types are used as implementation types. This causes problems in the coding and appearance.
- Use descriptive variable names. This is a rule that can be hard to follow, but in general variables should be named so that they make it clear what a variable contains. Sometimes this is really obvious, and in these cases short names are okay. But if you use a longer name, try to make it meaningful.
- Cut –and-paste code. It is often suggested that the paste function be disabled on every developer machine. Why? Because if you have a snippet of 9 lines of code that do one thing, and you want to do it again to a different set of variables, don’t copy and paste the code. Instead make a function (or a method, if you’re stuck with Java)
- Omit needless code. It’s quite common to find code that’s commented out, but still hanging around. This is ad because it bloats the code unnecessarily. Even if you want to have the possibility to bring the code back, either because you are writing an alternative you are not sure about, or because you don’t dare delete it. However in nearly all cases there is no real reason to keep such code around. You should be using version control, and that means you could always find any deleted code again.