Code smart, not hard
Code smart, not hard is a simple principle that can be applied to any activity, not just programming, but unfortunately is not followed by everyone. Various reasons attribute to bad code being written, but mainly it is a result of laziness and unwillingness to plan ahead. To put it simply, always think things through, plan for possible future changes and be flexible whenever writing code and not confine yourself to bad structure.
Establish structure before any coding
Probably the most important thing on this list is to get a high level idea of how everything is structured before any implementation. Catching problems earlier in the development cycle saves time at an exponential rate as you proceed from one stage to the next.
Become comfortable with a modeling language such as UML for structured code. For projects that consists of a database be sure to also create a schema or E-R Diagram before any creating any tables to hammer out data relationships. This will not only help you sort out your own ideas, but will also allow others to understand your proposed structure easily before any implementation. A great and free program I like to use for my UML modeling is called yED and for databases MySQL Workbench does a sufficient job for diagramming your data.
Refactor sloppy code
Ever find yourself faced with code that “smells” bad? Don’t just keep adding to the bad code, think of ways to improve the code base. Doing so will save time in the long run in maintenance and when new features need to be added.
An example of a common code smell: the same instructions are being copied and pasted in various places throughout the code with very minor differences. Abstract out any variables used, and then extract the logic into a method that you can call with the variables as arguments. This will ensure that if there is a logic change for your algorithm, changes only need to be made in one place as opposed to many. Otherwise, having multiple copies of the same logic would require searching for each instance and replacing it manually or through find and replace which is dangerous.
Write reusable code
Reusable code is related to common practices of refactoring but also deserves a section to call its own. When coding a class, method or algorithm that could be used for a future project, it is in your best interest to make it as flexible to change as possible. With a library of flexible code, you will have an already time-tested code base to work from.
Many books and websites illustrate the importance of using design patterns. Design patterns are coding structures that are accepted by the development community to make easy to read and extend code. A great resource to dive into design patterns is Head First Design Patterns; the book does a great job explaining each pattern and the benefit of using each one, without the need of large confusing code examples.
