Daily Productive Sharing 534 - Do Stupid Things

Daily Productive Sharing 534 - Do Stupid Things
Photo by Brett Jordan / Unsplash

One helpful tip per day:)

Thorsten Ball suggests that we'd better write more stupid code -- code that solves problems without being fancy, as Occam's Razor suggests.

For example, list comprehension in Python is a way to write a for loop, and in many cases the two are interchangeable, and even a two-level nested for loop or a conditional for loop can be rewritten as list comprehension. But the latter two cases of list comprehension are not very easy to understand and easy to introduce bugs. If you are not familiar with it, it is better to follow the "stupid way" to write for loop.

k = [[1, 3], [4, 5]]
r = [i * 6 for j in k for i in j if i > 2]
r = []
for j in k:
	for i in j:
		if i > 2:
		r.append(i * 6)	

If you enjoy today's sharing, why not subscribe?

Need a superb CV, please try our CV Consultation


#work

Thorsten Ball 建议我们多写笨代码 -- 那些能解决问题,不花里胡哨的代码,正如奥卡姆剃刀原理所言。

举个例子,Python 里的 list comprehension 是 for loop 的一种写法,在不少情况下两者可以互换,甚至两层嵌套的 for 循环或者有条件判断的 for 循环也可以改写成 list comprehension 。但后面这两种情况的 list comprehension 并不是很容易懂,也容易写错。如果不熟悉的话,不如还是按照“笨办法”写成 for loop。

k = [[1, 3], [4, 5]]
r = [i * 6 for j in k for i in j if i > 2]
r = []
for j in k:
	for i in j:
		if i > 2:
		r.append(i * 6)	

如果我们的内容对你有价值,不如付费支持我们 :)

需要更棒的简历,不妨试试我们的 CV Consultation

如果你也想成为更高效的人,欢迎加入我们的 TG group