In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the necessary coding skills for senior PHP engineers". In daily operation, I believe many people have doubts about the necessary coding skills of senior PHP engineers. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the necessary coding skills of senior PHP engineers?" Next, please follow the editor to study!
What is refactoring and why do we need it?
Refactoring refers to the methods and steps that help us write concise code. This is important for other developers who may read, extend, and do not need to edit to reuse our code.
The following will show you some examples of refactoring logic code to make it better.
Do not ReFactor the code in the production environment without unit testing
My first piece of advice is to never start refactoring logical code without full unit testing. My reason is that you will end up with a damaged function that is difficult to repair, because it is also difficult for you to point out where the damage is. So, if you want to ReFactor it, start with testing. Make sure that the part you are going to refactoring is covered by the test. PHPUnit code coverage analysis.
Start refactoring from the bottom of your code
Take a look at the picture below. This is a real hotel management system project I found from Github. This is an open source project, but a closed source project would be bad.
Example: refactoring from the bottom
If you look at this code, there are three levels in red. The bottom layer should be the declaration of being surrounded by if/else under the first if condition. In general, the lowest level is concentrated on a single logical processing, which is easier to refactoring.
Make your methods shorter and break them down into smaller methods or configuration files / DB tables
Maybe here, we can refine it to a private method as follows:
Make your method shorter
The next deep point will be the upload parameters and load view. Now, let's take a look at the add () method after refactoring the rest. It becomes more concise, easy to read, and easy to test.
Example: first refactoring the lowest level
If stated that he insisted on using curly braces.
Most programming languages support single-line if declarations, because this is relatively simple, so some developers just use it, but it is not easy to read and can cause problems, because a blank line can break the condition and cause a crash. Take a look at the difference between the following two examples:
Example: using curly braces
Don't use magic numbers or magic strings:
In the next example, you notice that an error message is returned if the room exceeds 250. Here, 250 is considered a magic number. If you're not the developer who wrote this, it's hard to point out what this number means.
Example: magic number
To ReFactor this method, we can point out that 250 represents the maximum number of rooms. To replace the hard code, we can extract it to a variable $maxAvailableRooms. It is now easier for other developers to understand.
Example: fix magic number
Don't use else statements if you don't really need:
In the same availablerooms () function, you notice the if declaration, where we can easily get rid of the else part and keep the logic consistent.
Example: ignore else declaration
Use names that represent your methods, variables, and tests
In the following example, you will find that the hotel management system has two methods: "index ()" and "room_m ()". For me, I don't know what their purpose is. I think it should be easy to understand if their names can describe themselves.
Example: bad method naming
Make full use of the functions of your programming language
Many developers do not take advantage of the full functionality of the programming language they use. Many features can save you time and make your code more robust. Take a look at the following example and notice how it is easier to achieve the same result with less code, by using type hints.
Finally, I'd like to provide some quick tips for better coding:
Replace the old array () with the new array form [].
Unless it is important not to check the data type, use the = operator instead of =.
It is always a good idea to give a short descriptive name to a public method. Private methods can use longer names because their scope of application is limited.
Use the common name such as add () only for methods that implement the interface, and use descriptive names such as addUser () or addDocument () for individual class methods.
Removes unused methods from the class.
Use the is/has prefix for functions that return boolean and so on: isAdmin ($user), hasPermission ($user).
Always use access modifiers in class methods and properties.
Pay attention to interface contamination: use only methods that the user can use publicly.
Organize class methods at the top of the public method.
Always apply the concept of a single responsibility to the class.
At this point, the study of "what are the necessary coding skills for senior PHP engineers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.