named

Let’s start with a couple of questions. Okay? When reading someone else’s code, what information do you use to understand what the code is doing? What are variables, functions, and classes?

When we read other people’s code, we usually understand program intent through variables, functions, and classes, so variables, functions, and classes are essentially interfaces between the programmer and program logic.

Therefore, when you name variables, classes, and functions, if you use unclear, nondescribable names, you are really confusing the program logic of any programmer who reads the code.

Such as:

dxy

If you just look at the name, no one can guess what it means, you have to read more code and infer from the context, right

To:

distanceBetweenXY

You can immediately understand that the name stands for “direct distance from x and y.”

Good code is self-explanatory

“Clear and expressive code with few comments is far superior to cluttered and complex code with lots of comments.” — Robert C. Martin “Clear, expressive code with few comments is far better than messy and complex code with lots of comments.” — Robert C. Martin

Before optimization:

Check that a worker has full benefits

if ((employee.flags ! = HOURLY_FLAG) && (employee.age > 65))

After the optimization:

if (employee.isEligibleForFullBenefits())

A function or class does only one thing (single responsibility)

If you’ve ever seen a function with hundreds or even thousands of lines, you know how painful it is to read, understand, and modify. Code comments only play a minor role.

“Programming is breaking one big impossible task into several small possible tasks.” — Jazzwant “Programming is about taking a big impossible task and breaking it down into smaller possible tasks.”

Good code is broken down into blocks of atoms. The goal of each function should be to do one thing, and each class should aim to represent a specific concept.

PutElephantIntoRefrigerator () process oriented split:

openRefrigerator()

pushElephantIntoRefrigerator()

closeRefrigerator()

Object-oriented split:

class Elephant {} class Refrigerator { private isDoorOpen; privateisDoorOpen; private isDoorOpen; privatevolume = [];

public function open(){
    $this->isDoorOpen = true;
}

public function close(){
    $this->isDoorOpen = false;
}

public function save($object){
    $volume[] = $object;
}
Copy the code

}

elephant=newElephant(); elephant = new Elephant(); elephant=newElephant(); refrigerator = new Refrigerator(); Refrigerator – > open (); refrigerator->open(); Refrigerator – > open (); refrigerator->save( elephant); elephant); elephant); refrigerator->close();