make code easier to read

1. Choose specific, avoid generality, find more expressive words 
fetch and download are better than general get,
deliver, dispatch, route, distribute are more explicit than send

2. Use i, j, k in the loop structure; if more You can use userI or user_i to be precise (either camel case or underscore)

3. Add more information to the variable name to make it easier to read.
hexId // hexadecimal id
startMs // start time, in milliseconds elapsedSecs // Elapsed time , in
seconds Data 5. Short names can be used in small scopes such as: TaelService $m; 6. Throw away useless words: convertToString() can be replaced by toString() 7. Use name format to pass meaning class name big camel case format : ClassName variable Small camel case: fooName constant: CONST_NAME 8. Avoid ambiguity such as getting data from the database:




















db->filter('year<=2017'); We don't know whether the filter here is to pick out the data that conforms to the rules, or to subtract the data that conforms to the rules.

The following is relatively more semantically clear
db->where('year <=2017')->get();

9.min/max (inclusive) means limit

10.first/last means inclusive range

11.begin/end means inclusive/excluded range

12. Name the bool value
readPassword is required to read Take the password, or the password has been read, it is better to just needPassword or userIsAuthenticated
to add words such as is, has, can or should to the bool value, which can make the semantics more clear

13. Avoid using the antonym
$disableSsl = false;
you should use
$ useSsl = true;

14. Match the user's expectations.
As the user always expects the get and size methods to not need to do too much calculation. Most people understand that count requires more operations than size.

15. Temporary variables should be used after processing the original variables, otherwise do not use temporary variables.

15. Principles of source code layout consistency:
A. Use a consistent layout
B. Similar code should look
the same C. Related code Row grouping, forming code blocks

16. Add extra methods to tidy up irregularities

17. Use column alignment when needed
$rule = [
'name' => 'required|between:2,50',
'content' => 'required|between:10,500',
'limitnum' => 'required|integer|between:10,1000',
'level' => 'required|integer|between:1,500',
'img' => 'required|mimes:jpeg,bmp,png,gif|image|max:1024k'
];

18. Choose an order that makes sense, and always use it.
For example, forms in html should be arranged in order from important to unimportant, followed by alphabetical order. PHP should also receive form values
​​$name = Input::get('name');
$age = Input::get ('age');

19. If the value on the left should be a variable, and the value on the right should be a constant

20. Unless the logic is simple (such as only checking the condition and returning a value), don't use the ternary operator

21. In the function Return early in the loop, use break or continue early in the loop; reduce nesting by returning early.

22. When multiple ifs,Correct/simple/interesting or suspicious cases should be handled first
$age = intval($rawAge);
if ($age <= 0) {
return 'wrong age';
}
if ($age > 100) {
return 'age error';
}

if ($age > 0 && $age < 20) {
return 'youth';
}

process correct first
if ($flag === true) {
.. ...
}

23. Split super long expressions, such as:
if (trim(explode($rawStr, ',')[0]) === 'test') {
...
}

can be split into:
$users = trim(explode($rawStr, ',')[0]);

24. The summary variable
if ($request->uid === $user->id) {
...
}
can be written as:
$userOwnsDocument = ($request->uid === $user->id);

25. De Morgan's theorem (respectively negate, transform and/or):
if (! $a && ! $b && ! $c) {

}
to More readable than the code below
if (! ($a || $b || $c)) {

}

26. Don't abuse short circuit logic,But the following logic is reasonable
if($object && $object->userName) {
....
}

27. Change the complex logic to simple and elegant:

return ($begin >= $other->begin && $begin < $other->end) || ($end >= $other->begin && $end <= $other->end);

can be split into:
if ($other->end <= $begin) {
return false;
}

if ($other->begin >= $end) {
return false;
}

return true;

28. Split huge statements:

29. Reduce variables, for variables that only appear once, do not have to declare. Reduce variables for control flow.

30. Reduce variable scope; such as

class Object
{
private $str;

public function method1()
{
$this->str = '...';
$this->method2();
}

public function method2()
{
$this->str = '...';
}
}

str is only used in method1 and method2, so it should be narrowed down.
class Object
{
public function method1()
{
$str = '...';
$this->method2($str);
}

public function method2( $str)
{
....
}
}

31. Use static methods if you can use static methods.

32. Use constants more; variables that are written only once are better. Return early to avoid intermediate variables.

33. Actively discover and extract irrelevant 34.

Code does one thing at a time

35. Less codebase is better

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325309253&siteId=291194637