Writing code for a web app or software is the most important part of Development Life Cycle. The coder needs to organize everything from the beginning – especially for enterprise level projets. If it’s not properly organized, the coding processes and code management afterwards may end up not just time consuming, but also a bit frustrating.
Well written code is maintainable, reusable, and testable. Following tips will help you to code like a pro:
1. FOLLOW CODING STANDARDS
It’s easy to unorganized code, but it’s hard to maintain such code. Good code typically follows some standard for naming conventions, formatting, etc. Such standards are nice because they make things deterministic to those who read your code afterwards, including yourself.
You can create your own coding standard, but it’s better to stick to one with wider-acceptance. Publicly maintained standards like Zend Framework Coding Standard or PSR-1 Coding Style Guide, it will be easier for others to adapt.
There are so many plugins for editors like sublime, atom etc. for formatting the code. Like, for sublime, you may use sublime code formatter
2. WRITE USEFUL COMMENTS
Proper comments improve readability of the codes. This is a crucial part of standard coding and helps into maintenance of the code.
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. And don’t forget, always keep your comments up to date!
3. USE MEANINGFUL VARIABLE NAMES
Never use names like $x, $y, and $test for your variables. How do expect to read such code in the future? Good code should be meaningful in terms 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 PascalCase).
4. REFACTOR CODE
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. There is no standard rule for it but practising following rules makes code better and organized:
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.
If you have a lot of nested loops then you may be doing some resource-intensive processing without realizing it. In general, you should rethink the logic if you are nesting more than 2 loops. Three nested loops is just horrible!
Consider if there are any applicable design patterns your code can follow. You shouldn’t use patterns just for the sake of using patterns, but patterns offer tried-and-true ready-thought solutions that could be applicable.
5. USE MEANINGFUL STRUCTURES