Following the same rules simplifies the work of programmers. Making PHP code one standard helps in teamwork and raises the level of development.
Eight general rules
There are rules that are suitable for writing code in any programming language. Only following them will already improve the quality of your code.
- Make up understandable and readable names. Avoid Russian words in Latin transcription. Only English words denoting the essence.
- Indent each level and separate the logical blocks with an empty string.
- Reduce nesting and remove duplication.
- Control the length. We recommend for functions 20 lines or less, method’s less than 50 lines, classes less than 300 lines, files are less than 1000 lines. Limit the length of one line to the visible value on the screen. Soft restriction is 120 characters.
- Comment and document the code. This will fix all the necessary information.
- Use refactoring. Follow the principle of “refactoring, before and refactoring, more often.” We also recommend reading the book “Refactoring. Improving the draft existing code “by Martin Fowler.
- Work in the version control system. For example, Git as it’s free and convenient. Learning to work in it can be done in 11 sessions on the video course “Git. Fast start”.
- Learn the Open Source code. You can see how the leading developers write and take advantage of the best practices in programming.
At the end of 2017, PHP programming standards apply: PSR-2 and PSR-1. They set the rules for syntax, naming and design. All code must be written uniformly. This concerns spaces, indents, parentheses and lines.
In order do not remember all the requirements of the standards you can work in a modern development environment using PhpStorm, NetBeans and the like. They allow you to automatically format the text in accordance with the rules.
Properly designed PHP code assumes its simple visual perception. It is achieved by indenting and spaces. For indentation, use spaces instead of a tab character. Start each line with four spaces. The code should go to the ladder: open to the right, then reassemble.
Remember: one indent = four spaces.
We select indents of the body of the construction, the body of the method, import blocks, arguments and the like.
Do not put:
We do not stand for separate lines.
Do not put spaces inside: after (and before).
Put spaces before brackets and after.
Inside, the enumeration is separated by spaces.
Are issued in two ways: in one line, separated by commas or in a column. Arguments on one line are written separated by commas inside the parentheses. Space is placed only after the comma.
Comments in the code
Clean code should be properly commented out. Unfortunately, there are two extremes: a detailed comment on each line and a complete absence of comments. Both that, and another hinders in work. Excessive commenting reduces the perception of code, distracts from an understanding of its essence. Write obvious things – spend your own and someone else’s time. Sometimes due to too detailed comments the amount of code is increased several times. Having finished with the code, look critically. Obvious and trivial comments delete.
The reverse side of the coin is the absence of explanations. Codes with complex architecture, scripts, with a lot of nesting require notes. In this case, comments will help quickly navigate the code to other team members. Often they help the code developer himself. The main thing is to stick to the golden mean and comment only on the important points.
The code is not a personal littered box with a pile of papers. The correct code is a file in the Lenin Library. It all structured, documented important information, there are links to other directories and libraries. Net code can be parsed by both the pros and the beginner programmer. It is a pleasure to work with it and also describes the level of skill of the developer.
Try to follow the accepted rules, recommendations and standards. Think about those who will work with him after you.