Writing a good code is a art. if a programmer follows some good programming habits then he can be a good programmer. Actually any code you write is likely to spend most its time in maintenance; application maintenance is a large expense. Establishing good coding habits will enhance design factors like modularity, and your code will be easier to understand and, thus, easier and cheaper to maintain. bad coding habits create defects in code and can cause code to be difficult to modify without introducing new defects.
Here in this post we will discuss few good programming habits which will help you avoid defects in your code.
1- Write Structured code
2- Write Consistent code
Good PHP code should be consistent. always try to write consistent code by setting rules for the names of variables and functions, adopting standard approaches to recurring tasks like database access and error handling , or simply making sure all of your code is indented the same way, consistency makes your code easier for others to read.
3- Write Portable code
Good PHP code should be portable. you can use php’s existing features such as magic quotes and short tags. Always try to understand your requirement then write your code by adapting PHP features to make code portable and plateform independent.
4- Write Secure code
Good PHP code should be secure. As PHP5 offers excellent performance and flexibility. it leaves important issues like security entirely in the hands of the developer. A deep understanding of major security holes like Cross-Site Scripting (XSS), Cross-Site Request Forgeries (CSRF), code injection vulnerabilities, and character encoding loopholes is essential for a professional PHP developer. By using PHP’s special features and function such as mysql_real_escape_string etc, you can write secure code
5- Documented functions and classes
Code commenting is important part of your code. The comments tell why the functions are doing what they’re doing, which will be much more helpful in the future when the code is being maintained.
6- Avoid Short Tag
Always replaced “short” opening tag with full PHP tag
7- Use single quotes instead of double quotes
Always use single quotes instead of double quotes to avoid the performance hit of PHP searching for variables to interpolate within the strings.
8- Escape string output
Always try to use ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (‘) are also escaped. as it is not necessary to escape but it’s a good habit to get into.
9- Use commas to separate strings ouput
Rather than using the string concatenation operator (.) to pass a single string to the echo statement, the strings to be output by echo are separated by commas for a tiny performance boost.
10- Check post values before output
Always check post values of $_GET['query'] before ouput. isset or empty functions can be used to check variables for null values.