不止一种方法去做一件事There's more than one way to do itTMTOWTDITIMTOWTDI,发音为“Tim Toady”)是一条Perl俗语。这个语言本身就是用这个“不要教程序员如何编程”的主意设计的。这使得很容易可以写出极度杂乱的程序,但是,根据支持者对这个俗语的理解,这也可以很容易地写出简洁的语句。

例子

编辑

以下3种等价。

print if 1..3 or /match/

或者更传统的:

if(1..3 || /match/) {print}

或者甚至更详细:

use English;
if( $INPUT_LINE_NUMBER >= 1 and $INPUT_LINE_NUMBER <= 3 or $ARG =~ m/match/ )
{ print $ARG; }

发展

编辑

这条俗语在Perl社区里被讨论了很多,而且扩充成了“不止用一种方法去做一件事,但有时统一也不错”(There's more than one way to do it, but sometimes consistency is not a bad thing eitherTIMTOWTDIBSCINABTE,发音“Tim Toady Bicarbonate”[1]

例如,以下3种:

if (not flaky_operation){
    die;
}

flaky_operation or die;

die unless flaky_operation;

比较这3种方式,哪一种更好。从阅读方面说,die unless flaky_operation似乎表明了这个操作很难成功,if块又需要输太多代码,而flaky_operation or die似乎说明要么全有,要么全没有[2]

事实上,更好的方式是一个错误时抛出异常而不是返回假的flaky_operation。但大部分Perl函数都不是这样的。另一种做法是:

flaky_operation or die "$!"; #这样die,能给出更多信息。

[2]

和Python哲学的比较

编辑

Python哲学有一个和TMTOWTDI相反的俗语: “应当只用一种——而且是最好的一种——明确的方式去做一件事。”(“There should be one – and preferably only one – obvious way to do it.”)[3]。这鼓励用最好的方法做事,但在需要的情况下也有次要的替代方法。它的目的是让这个语言易学,尤其是易读,因为当做一件事有很多不同方法时,如果要理解现有的代码,就需要知道所有方法。

参考文献

编辑
  1. ^ Can EPO (or TPF) tame TIMTOWTDI?. dev411.com. 2009-01-24. [永久失效連結]
  2. ^ 2.0 2.1 There Is More Than One Way To Do It. [2012-08-17]. (原始内容存档于2006-08-28). 
  3. ^ PEP 20 - The Zen of Python. Python.org. 2004-08-23 [2012-08-17]. (原始内容存档于2022-02-27). 

外部链接

编辑

📚 Artikel Terkait di Wikipedia

Python之禅

The Hitchhiker’s Guide to Python. 2015-11-18 [2019-02-26]. (原始内容存档于2018-12-26).  Tim Peters. PEP20 -- The Zen of Python. 2015-11-18 [2019-02-26]. (原始内容存档于2018-12-26)

Python語法及語義

Python語法及語義(Python syntax and semantics),Python編程語言之語法是一組Python的運作編程規則,用於定義Python程序如何來編寫與編譯(由運行時系統與程式師來操作)。Python語言與Perl、C,以及Java有許多相似之處,不過在這些語言之間仍存在著一些明確的差異。

NeuroKit

(MatLab) MNE-Python (Python) ^ 截于2022年五月18日, GitHub 承认此软件已拿到 644颗星, 并拥有47位贡献者, 及广用于 101 个不同的开源软件. Makowski, Dominique; Pham, Tam; Lau, Zen J.; Brammer

Asm.js

BananaBread based on Cube 2 Humble Mozilla Bundle中的每个游戏(Super Hexagon、轻率漠视重力、Osmos、Zen Bound 2、Dustforce DX、Voxatron、FTL: Advanced Edition和Democracy 3) EM-DOSBox:一个DOSBox模拟器的移植

Google Play

[2015-12-11]. (原始内容存档于2015-12-22).  Distribution of free and paid apps in the Apple App Store and Google Play as of 1st quarter 2018. statista. (原始内容存档于2018-11-27)

SUGIZO

R-VIII Sig. Esp Eclipse S-I Esp Eclipse S-I Proto Type Esp Eclipse S-I (Python) Esp Eclipse S-I Brilliant -Mixedmedia- Esp Eclipse S-II Esp Eclipse S-II