We all know that writing code can sometimes be the most difficult part of any kind of development process. 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.
Good code is maintainable, reusable, and testable. So, today I am going to address you some tips that helps you to keep everything as need as possible.
Avoid Global Code
- Global Variables and Loops are a mess and can prove problematic when your application grows to millions of lines of code.
- Think twice before you pollute the global namespace with variables, functions, loops etc.
Write Useful Comments
- Write meaningful, single line comments for vague lines.
- Write full parameter and functionality description for functions and methods; for tricky logic blocks, describe the logic in words before it if necessary
- Don't forget, always keep your comments up to date
Use a Code Standard
- It's easy to write bad, unorganized code, but it's hard to maintain such code.
- Good code typically follows some standards for naming conventions, formatting etc.
Use Meaningful Structures
- Structuring your applications is very important, don't use complicated structures, always stick to simplicity.
- Always split the four parts of any typical application apart from each other -CSS, HTML Templated/Layouts, JavaScript, PHP Code
Use Automated Build Tools
Refactor
- Code refactoring is the habit of highly effective developers.
- Believe it or not, you should be refactoring your code on a daily basis or your code is not in a good health.
If your function or method is more than 20-25 lines, it's more likely that you are including too much logic inside it, and you can probably split it into two or more smaller functions/methods.
If your method/function name is more than 20 characters, you should either rethink the name or rethink the whole function/method by reviewing the first rule.
Use Meaningful Names
- Never use name like $k, $m and $test for your variables.
- Good code should be meaningful in terms of variables names, function/method names, and class names.
- Some good example of meaningful name are: $request, $dbResult and $tempfile or depending on your coding guidelines these may use underscores, camelCase or PascalCase.