Good code is maintainable, reusable, and testable. If you don't organize everything from the start, especially for big projects. The coding processes and code management afterwards may end up not just time consuming, but also a big headache. This article introduce you to some best practices that will help you write better code and help make you and your team happy and efficient.
1. Use a Coding Standard - It's easy to write bad, unorganized code, but it's hard to maintain such code. Good code typically follows some standard for naming conventions, formatting, etc. You can create your own coding standard, but it's better to stick to one with wider acceptance.
3. Write Comments in Code - Comments are important. You won't appreciate them until you leave your thousand lines script for a couple of days and return to and try and make sense of it. Useful comments always helpful and make life easier for yourself and those after you who have to maintain codes. Write meaningful, single line comments with full parameter and functionality descriptions for functions and methods, also describe the logic in words before it if necessary.
4. Use Meaningful Structures - Structuring your application is very important; don't use complicated structures, always stick to simplicity. When naming directories and files, use a naming convention with your coding standard. Always split coding in libraries and Theme/Layout. It's also a good to keep your directory hierarchy as shallow as possible so it’s easier to navigate and find the code you’re looking for.
5. Avoid Global Code - Global variables and loops are a mess and can prove problematic when your application grows to millions of lines of code. They may influence code elsewhere that is difficult to discern, or cause noisy naming clashes. You should have no blocks defined globally. All switch statements, try catch, foreach, while loops, etc. should be written inside a method or a function. Methods should be written inside class definitions, and class and function definitions should be within namespaces.
6. Use Version Controlling Software - Nowadays, there have a variety of solutions available for controlling your software version. Managing changes and revisions should be easy but effective, so pick whatever version control software that will work best with the workflow of your development team. Most popular version control tool like Git or Mercurial, both are free software/open source and very powerful.
7. Use Automated Build Tools - Try to use tools like Ant or Phing to get your source prepared, compressed, and deployed. Building your whole application with a single command is a marvelous way to prevent errors and omissions that are inherent when performing repetitive tasks, and is a generally core pre-requisite for automated testing strategies.
8. Refactor - Code refactoring is the eighth habit of highly effective developers. You should be refactoring your code on a daily bases or your code is not in good health! Refactoring keeps your code healthy. You should be refactoring everything, from your architecture to your methods and functions, variables names, the number of arguments a method receives, etc.
9. Use Code Documenters - For large applications spanning several classes and namespaces, developer should have automatically generated API documentation. This is very useful and keeps the development team aware. And if you work on several projects at the same time, you will find such documentation a blessing since you may forget about structures switching back and forth between projects.
10. Use a Testing Framework - Testing is crucial to every piece of your million dollar application. There are a plenty of tools that is very useful for testing, but by far the ones most appreciated are the frameworks that help automate the testing process.