preface

Python has a lot of syntactic sugar that allows you to do things in short code.

I used to feel geeky and Pythonista by obsessing over using all kinds of syntactic sugar and trying to do it in the shortest code possible (the least amount of code), but now I think that’s a mistake.

Brevity on the surface

The following code can do exactly the same thing. Which would you prefer?

# notation 1: Use list generators
result = [(x, y) for x in range(5) for y in range(10) if x * y > 20]

# 2: Simple multi-level loop

# Correct example
result = []
for x in range(5):
  for y in range(10):
     if x * y > 20:
       result.append((x, y))
Copy the code

In the past, I had no hesitation in writing method 1, which I thought was geeky and could highlight my proficiency in Python. However, the code in Method 1 was not friendly in terms of readability, and the brevity was superficial.

True brevity should be based on semantically clear programming, so that the person reading your code can understand at a glance what you are trying to say.

On the basis of clear semantics, to simplify the code, remove redundancy, the logic of the roundabout deletion, and strive to express their ideas in the simplest way.

True brevity is a refinement of the mind, not a shortening of code with fancy syntactic sugar. Blog Wang Yin gives a very graphic description of the phenomenon: “It’s like organizing a wad of wires, not just crumpling them up and stuffing them into a box. This will only create more problems for your future work, as well as safety risks.”

In addition to being hard to understand, it’s easy for the writer to write bugs without knowing it, and it’s also hard to debug, which takes more time to locate the cause of the bug.

Remember one rule: “Reader experience > programmer experience > machine experience”

In the process of developing a project, the time spent writing code is much less than the time spent reading code and debugging. Therefore, the most important thing is to write code with clear semantics, followed by optimization based on this.

Most of the time using a lot of syntactic sugar is just to show off. This is probably the “programming naivete” that every young programmer goes through. Of course, there are some sophisticates who deliberately make code hard to understand in order to increase their “irreplaceability,” which is another story.

Meaningful variable names

Meaningful variable names are an important prerequisite for semantically clear programs.

A hundred lines of code filled with variables a, B, C, and D would be annoying, while a hundred lines of code with variable names that are all meaningful would look easy. The variable names themselves carry a certain amount of information, which helps us understand the code better and faster.

Use Python dark magic in moderation

The nature of Python’s dynamic language makes it easy to write in Python in ways that might be difficult to write in a language like Java. This dark magic is a double-edged sword that should be used sparingly (and there are plenty of Java ways to write in moderation).

What is moderate?

Reading multiple Python open source libraries, you can see that many of the advanced uses in Python are only used for the underlying architectural implementation (such as metaclases, decorators with complex logic), by encapsulating the underlying logic so that the hard-to-understand code is not exposed directly to the application layer interface.

This keeps the underlying code clean and avoids confusing the application layer code

As an ordinary programmer, he is most often exposed to the development of various business logic at the application level. At this time, he should avoid using Python’s advanced syntax and strive to make the code semantics clear and easy to understand at a glance.

The underlying framework is designed and implemented in a similar way, and can be understood even with Python’s advanced syntax. However, the business logic is different, and the corresponding code of different business varies greatly. If a large amount of syntax sugar is used, it will be difficult to understand, cause problems and debug. In this case, it is best to complete the corresponding business logic in the simplest way to ensure semantic clarity.

At the end

Recently, I was reading the code of Python project of my company, and I got inspired.

I hope you can get out of the days of grammar candy and find other ways to prove your value and that you can’t be easily replaced.

If you think it is helpful and valuable to you, please click on “Watching” and support two two.

Common design mistakes in programming languages (1) – The pursuit of smallness