当前位置:网站首页>In a bad mood, I just write code like this

In a bad mood, I just write code like this

2022-06-26 16:21:00 Embedded Linux,

stay GitHub There's a project on , It describes the 「 The best garbage code 」 19 key principles of . From variable naming to annotation writing , These guidelines will guide you to write the most brilliant bad code .

In order to keep up with the original GitHub Project consistent style , There is no conversion below . Readers can understand all points of view in the opposite way , This is the perfect way to avoid writing junk code .

Project address :
https://github.com/trekhleb/state-of-the-art-shitcode

Of course , The following 19 garbage code writing guidelines are not comprehensive , If readers find that there are some unbearable bad code habits , You can also express your views .

Article 1 with a : The less you type, the better

If we type less , So the more time you have to think about code logic . As shown below ,「Good」 Indicates an example of following this rule ,Bad An example of not following the rule .

be8abe5f5271a919fcfa17bfd49009e4.png

Second : Variable / Function mix naming style

We need to mix naming methods and variables , In this way, we can reflect the diversity of naming .

154d371af29710cc2e38da3657b77738.png

Article 3 the : Don't write notes

Anyway, the code can understand , Why write notes ? Or say , No one's looking at my code anyway , Why write notes ?

02b8f298a2e3078dfffe501d2cbc02be.png

Article 4. : Write notes in your native language

If you break rule three , At least you need to write notes in your native language or other languages . If your native language is English , Then you're violating the rule . Since most programming languages are in English , So why don't you use other languages to comment on ?

7839914f1ea8661c08e229066eda0646.png

Article 5. : Mix different formats as much as possible

Again , For the diversity of the code , We need to mix as many different formats as possible , For example, single or double quotation marks . If they have the same semantics , Then we should mix .

dae0bd1e33c87a73824f556023e9efb5.png

Article 6. : Write the code as one line as much as possible

If a series of parameters and methods are implemented together , So the code has to be written together .

be5a0139297ec38a1aec199c67874cc6.png

Article 7. : Keep quiet when you find mistakes

When you find something wrong , Others don't need to understand it , So there's no need to print out logs or Traceback.

7129ca681422d8f8fabda604a85e5772.png

Article 8. : Global variables are widely used

Use global variables , It's for 「 Globalization 」 An indispensable part .

f62439d538a86a666e8984df409feb69.png

Article 9. : Build standby variables

In case of a one thousand , We need to create some spare variables , Call them whenever you need them .

f4d73575864863a72598b4cc97fdb47f.png

Article 10. :Type Use with caution

Generally, do not specify variable type or do type checking frequently , No type is the best type .

62b0210aab6763cb59c869105544fe29.png

Article 11 : Get ready 「Plan B」

You need to prepare some code that you can't run (unreachable code), They can be your 「Plan B」.

ce3d6cc1d1593d1e1e1df7ad18a374d2.png

Twelfth article : Nested trigonometry

If the code has some nested structure , Or the structure of indenting empty lines , The triangle rule is the most beautiful .

81c4a44554b957c2f2ea919cd74ed8f1.png

Article 13 : Mixed indent

We need to avoid indenting , Because indenting makes complex code take up more space in the editor . If you have to indent , Then use a mixed indentation strategy . Of course , This strategy Python It doesn't work , Because it determines the structure of the code by shrinking in .

523c66e2b8f30cecea4cd4b47a2578f0.png

Article 14 : Don't lock dependencies

Every time you install a new library , Update existing dependencies . Why maintain the previous version , We need to keep up-to-date with third-party code bases .

1c945af129b7c53fdce9e0bdfe0aab93.png

Article 15 : Long function is better than short function

Don't divide the whole logic of the program into code blocks , If IDE Suddenly not , It can't find the necessary files or functions . So write the code in a body function , And no longer maintain additional function imports or code files , So this method is the most stable .

There's no problem with 10000 lines of code in a single file , A thousand lines of code for a single function is no problem .

Article 16 : The code doesn't need to be tested specifically

These tests are often repetitive and meaningless work .

Article 17. : Try to avoid duplicate code

Write the code as you want , Especially in small teams , It is, after all, 「 free 」 Rules .

Article 18 : Building a new project doesn't require README file

In the early stages of the project , We can keep it for a while .

Article 19 : Save unnecessary code

In the process of writing code , A lot of test code is often generated . These codes are also very important information , So you can't delete , At most, it can only be commented out .

END

edit :Jack-Cui

source : Almost Human


The copyright belongs to the original author , If there is any infringement , Please contact to delete .

8a28344237fa846dd65f318bd743e340.gif

原网站

版权声明
本文为[Embedded Linux,]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261554581848.html