Writing and maintaining code is necessary because you want to make it easily understandable for people. So here are some of my tips on how you can write better code and maintain it.
1. Avoid Global Code
- Global variables and loops are a mess and can prove problematic when your application grows to millions of lines of code (most do!!!).
- Think twice before you pollute the global namespace with variables, functions, loops, etc.
2. Use meaningful structures
- Structuring your application 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 - HTML, CSS Templates/Layouts, JavaScript, PHP Code.
3. Use meaningful Names
- Never use names like
$k
, %n
, and $test
for your variables. - Good code should be meaningful in terma of variable names, function/method names, and class names.
- Some good examples of meaningful names are:
$request
, $dbResult
and $tempFile
(depending on your coding style guidelines these may use underscores, camelcase, or, pascal case).
- Write meaningful, single line comments for vague lines.
- Write full parameter and functionality descriptions 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!
5. 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.
7. Refractor
- Code refactoring is the eight 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 good health!
- If your function or method is more than 20-25 lines, it's more likely that you're including too much logic inside it, and you can probably split it into two or 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.
Need Help
Need help in raising funds to buy a Mechanical Keyboard. This pandemic has affected my family badly so can't ask my DAD for it. Please Help Me.
😎 Thank You For Reading | Happy Coding ⚡ Also published at https://blog.rahulism.co/how-to-maintain-and-write-better-code