today on wechat What's the most garbage code you've ever seen? Seeing this interesting article, it originally came from Blog posts in CSDN .
§01 import this
after running the following code:
import this
you will get:
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
§ 02 creating new wheels
if the conventional logical judgment does not compound my requirements, I will create a signal judgment function.
▲ figure 2.1 if the logical judgment does not meet my requirements, create one
§ 03 magic numbers
seeing the code in this picture, I can't help asking: what happens if one of the numbers is changed? There may be nothing good.
▲ when I see the code in Figure 3.1, I can't help asking: what happens if one of the numbers is changed? Nothing good is possible
§ 04 machine C voice
this may be the C language written for computers. If it is a course assignment submitted to the C language teacher, I would like to express my deep sympathy and condolences to the teacher.
maybe this is the result of copying from another text editor (using "\ n" to represent a new line) to the editor that needs "\ r\n" to represent a new line
§ 05 judging odd and even numbers
this should be the most Naive algorithm to judge the parity of a number.
▲ Figure 5.1 this should be the most Naive algorithm to judge the parity of a numberbut this function reminds me of a Chinese joke: a person who has just learned how to write Chinese characters one, two and three considers how to write his name (his surname "Wan") in books.
however, the following code test tells us that the above code is normal except for efficiency.
import sys,os,math,time import matplotlib.pyplot as plt from numpy import * def isEven(n): n = abs(n) rst = True for i in range(n): rst = not rst return rst starttime = time.time() print("isEven(100000000): {}".format(isEven(100000000)), "isEven(100000001): {}".format(isEven(100000001))) elapsetime = time.time()-starttime print("elapsetime: {}".format(elapsetime))
▲ in Figure 5.2, the greatest respect for beginners of programming is to keep thinking after reading the above discussionisEven(100000000): True isEven(100000001): False elapsetime: 4.847123622894287
§ 06 string length
when he needed to get the length of a string, he realized it immediately without hesitation for a second.
▲ Figure 6.1 what is the function of this code? Yes, that's what you saw
§ 07 Emoji variables
look at the following code. Using Emoji symbols as variable names, the programming is very touching. isn't it?
▲ Figure 7.1 if the programming language can use Emoji, the taste of the program will immediately improve to a higher leveltested the use of these additional characters in Python.
○ = 1 print(○) ● = 'ball' print("●: {}".format(●))
operation results:
File "/tmp/ipykernel_131/3180195851.py", line 5 ○ = 1 ^ SyntaxError: invalid character in identifier
§ 08 extra wide screen
now I finally understand why programmers like wide screens. But there is a question: how wide is the screen to finally be enough?
▲ figure 8.1 now I finally understand why programmers like wide screens
§ 09 it's not funny
at first, I thought it was funny to see this function. However, for the micro Python language running on some MCU, it may be the most basic operation, which is really not supported. For example, decimal operations are not supported by default in microphoton. You need to write the corresponding floating-point or fixed-point decimal operation.
■ links to relevant literature:
● links to relevant charts:
- Figure 2.1 if the logical judgment does not meet my requirements, create one
- Figure 3.1 when I see the code in this picture, I can't help asking: what happens if one of the numbers is changed? There may be nothing good
- Figure 4.1 this may be the C language written for the computer. If it is a course assignment submitted to the C language teacher, I would like to express my deep sympathy and condolences to the teacher
- Figure 4.2 this code looks really sour
- Figure 5.1 this should be the most Naive algorithm to judge the parity of a number
- Figure 5.2 the greatest respect for beginners of programming is to keep thinking after reading the above discussion
- Figure 6.1 what is the function of this code? Yes, that's what you saw
- Figure 7.1 if the programming language can use Emoji, the taste of the program will immediately improve to a higher level
- Figure 8.1 now I finally understand why programmers like wide screens
- Figure 9.1 as long as the sum of the parameters of this function call is equal to 7, the result of this function is always correct