Good programming involves more than simply writing software that solves a problem or accomplishes a task. Good programming considers a number of factors, such as the speed of operations, error handling, development time, and maintainability of code. Here are some language-independent tips to keep in mind when writing programs or websites that can save you headaches down the road.
Modularize your code.
Break it up by purpose into different sections, files, and even folders if the project size warrants it. Separating your code makes it much easier to manage. More than just splitting up the lines of code though, it’s important to split up the logic as well. In particular, you don’t want one section of code relying heavily on the fact that another section of code works in a very specific way. In that case, it’s easy to introduce bugs in one area by changing code in another. A side benefit of this practice is that you can in some cases recycle sections of code to use on other projects with very little modification.
Comment your code.
It’s essential to comment on your code sufficiently, especially if you plan to revisit it weeks or months later. Commenting can be especially useful if your IDE supports on-the-fly documentation from comments. It’s so nice to interact with your own classes and see just as much popup documentation as the core modules.
Assume that problems will occur, and plan accordingly.
Especially when accepting user input or interfacing with a separate system, it’s important not to assume that everything will go well. Whether maliciously or accidentally, people will put bad data in your program, and other systems will sometimes fail to act the way you expect. Plan contingencies for these situations that don’t bring your system to a halt. Also, understand that parts of your program or website might occasionally behave unexpectedly and have plenty of error checking.
Use design patterns.
Design patterns are tried-and-true solutions to programming problems. It’s almost always easier and more efficient to use one when it applies rather than trying to reinvent the wheel. While not every problem has an established pattern, it’s beneficial to understand design patterns so you can use them if the need arises.
Strive for elegance.
Good programming is like art, blurring the lines between science and creativity. Elegance is defined as the attribute of being unusually effective and simple. In programming, there is no substitute. Elegance means solving problems in the simplest, most efficient way possible while making your code very intuitive and easy to understand. Elegance makes the code run faster, easier to interface with, and reduces the amount of code written, which cuts down on the number of bugs and also makes it easier to find and fix the bugs that do come up. When your code is easy to understand, it reduces the amount of documentation required for it to be understood.