Do not make your code unintelligible to others, because you might be one of the others who read the unintelligible code after one week. Always supervise yourself by bearing in mind that another developer may take a long time to process bugs if your code snippets have some faults.
In your everyday programming practices, make deliberate efforts to improve your semantic expressions. This will benefit you a lot. Specifically, how can we achieve good semantic expressions?
Code samples in this article are only my test code and do not belong to any real projects.
With this function name and parameter name alone, who can figure out the actual meaning of this parameter?
Assume the following classes are included in our business code.
However, we find the following when querying the database:
In this case, it is inconvenient to find corresponding tables according to the business code. Others will also feel confused when taking over our project. This inconvenience maybe caused by three iterative development cycles from three different persons, neither of whom has referred the naming rules used previously.
After the preceding description about naming, now let's move on to the comment. Is it even necessary to talk about comments? Are you kidding me?
Those questions may come to your mind. However, do you know how to write a comment on an array member object?
Do you know how to write comments on calls on magic methods of a class?
Comments and descriptions do match correctly. Method names are updated, while the business and feature comments for the methods are not updated. The @author information entry is also copied during the process of copying others' code and the original code author is ends up taking the blame for the code errors.
For more information about comments, refer to the web page http://manual.phpdoc.org/HTMLframesConverter/default/
First, I want to make one thing clear: Code with poor readability does not necessarily mean that the software is not good. It is common to see many bad PHP and MySQL code snippets.
The following is a code snippet of an open-source project. The feature is actually excellent. However, this method has too much content. I have listed some shortcomings of the code in the figure.
Do you still remember this figure?
Optimization solution 1Optimization solution 2
$startId
and $lastId
are repeated parameters.
Minimize parameter references
function bad($input1, $input2, &$input3)
{
//...logic
$input3 = "xxx";
return true;
}
Ensure the parameter type and the type of return values are clearly described and avoid the use of mixed types. Let's take the following official code for example. It is also necessary to be skeptical about authoritative practices. This is only my opinion.
In this example, the addUser
is written in a way that makes it more like a remote API interface rather than a function (method). In the code snippet shown on the right side, we need to make judgement based on is_array
every time we use it. This is an unfriendly semantic expression. Exceptions may occur in advanced languages like PHP and Java. We need to make good use of exceptions.
Good semantic expressions make it easier to implement efficient iteration in a collaborative team and troubleshoot unknown code issues online. This is the end of this blog. Have you learned something new?
3 posts | 1 followers
FollowAlibaba F(x) Team - July 28, 2022
Samuel_Song - December 9, 2020
Alibaba Clouder - May 6, 2020
ApsaraDB - December 30, 2021
Alibaba Clouder - February 1, 2021
Alibaba Clouder - September 1, 2020
Thankful for sharing this data. https://www.pinkgoa.in/ I truly like your blog zone no delicacy on the planet.
Our escorts will know what to expect when they come to you, which is why we always recommend that you meet in advance of the date. https://www.thegoaescort.com/ You can talk with them about what you would like to do during the appointment and have an opportunity to get comfortable with each other before you decide on your next steps.
3 posts | 1 followers
FollowAlibaba Cloud provides products and services to help you properly plan and execute data backup, massive data archiving, and storage-level disaster recovery.
Learn MoreA low-code, high-availability, and secure platform for enterprise file management and application
Learn MoreCustomized infrastructure to ensure high availability, scalability and high-performance
Learn MoreSDDP automatically discovers sensitive data in a large amount of user-authorized data, and detects, records, and analyzes sensitive data consumption activities.
Learn MoreMore Posts by zhoumengkang
5381571195078949 December 16, 2022 at 12:57 pm
idk