英語(yǔ)學(xué)習(xí)之《Clean Code》英文交流術(shù)語(yǔ)整理(二)

如果有一天剑辫,有人告訴你干旧,你將來(lái)需要和一幫只會(huì)說(shuō)英語(yǔ)和泰語(yǔ)的家伙工作在一起,而你的工作是要告訴他們?nèi)绾螌?xiě)整潔的代碼妹蔽,如何做重構(gòu)椎眯,什么是TDD,什么是面向?qū)ο蟾炱瘢阍撛趺崔k编整?

你問(wèn)我,我只能告訴你乳丰,我需要重新讀一次《整潔代碼》掌测,《重構(gòu)》以及《
敏捷軟件開(kāi)發(fā):原則、實(shí)踐和模式》的英文版成艘,然后整理出里面可以為我使用的術(shù)語(yǔ)和表達(dá)赏半。

第二章:Functions(函數(shù)方法)

Different levels of abstraction(不同層次的抽象)

Do you understand the function after three minutes of study? Probably not. There’s too much going on in there at too many different levels of abstraction. There are strange strings and odd function calls mixed in with doubly nested if statements controlled by flags.

Intuit(Understand or work out by instinct贺归,意會(huì))

How can we make a function communicate its intent? What attributes can we give our functions that will allow a casual reader to intuit the kind of program they live inside?

Small(小而美的函數(shù))

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. This is not an assertion that I can justify. I can’t provide any references to research that shows that very small functions are better. What I can tell you is that for nearly four decades I have written functions of all different sizes. I’ve written several nasty 3,000-line abominations. I’ve written scads of functions in the 100 to 300 line range. And I’ve written functions that were 20 to 30 lines long. What this experience has taught me, through long trial and error, is that functions should be very small.

Do one thing(只做一件事情)

Functions should do one thing. They should do it well. They should do it only. The problem with this statement is that it is hard to know what “one thing” is.

One level of abstraction below(低一級(jí)的抽象)

Is the function doing one thing or three things? Notice that the three steps of the function are one level of abstraction below the stated name of the function.

If a function does only those steps that are one level below the stated name of the function, then the function is doing one thing. After all, the reason we write functions is to decompose a larger concept (in other words, the name of the function) into a set of steps at the next level of abstraction.

One level of abstraction per function(每個(gè)函數(shù)具有相同的抽象層次)

In order to make sure our functions are doing “one thing,” we need to make sure that the statements within our function are all at the same level of abstraction.

Mixing levels of abstraction within a function is always confusing. Readers may not be able to tell whether a particular expression is an essential concept or a detail.

Don’t be afraid to make a name long(不要擔(dān)心名字太長(zhǎng))

Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment. Use a naming convention that allows multiple words to be easily read in the function names, and then make use of those multiple words to give the function a name that says what it does.

Refine that code, splitting out functions, changing names, eliminating duplication(打磨代碼淆两,拆分函數(shù),修改命名拂酣,消除重復(fù))

Writing software is like any other kind of writing. When you write a paper or an article, you get your thoughts down first, then you massage it until it reads well. The first draft might be clumsy and disorganized, so you wordsmith it and restructure it and refine it until it reads the way you want it to read.

When I write functions, they come out long and complicated. They have lots of indenting and nested loops. They have long argument lists. The names are arbitrary, and there is duplicated code. But I also have a suite of unit tests that cover every one of those clumsy lines of code.

So then I massage and refine that code, splitting out functions, changing names, eliminating duplication. I shrink the methods and reorder them. Sometimes I break out whole classes, all the while keeping the tests passing.

To be continued(未完待續(xù))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末秋冰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子婶熬,更是在濱河造成了極大的恐慌剑勾,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赵颅,死亡現(xiàn)場(chǎng)離奇詭異虽另,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)饺谬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)捂刺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人募寨,你說(shuō)我怎么就攤上這事族展。” “怎么了拔鹰?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵仪缸,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我列肢,道長(zhǎng)恰画,這世上最難降的妖魔是什么宾茂? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮拴还,結(jié)果婚禮上刻炒,老公的妹妹穿的比我還像新娘。我一直安慰自己自沧,他們只是感情好坟奥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著拇厢,像睡著了一般爱谁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上孝偎,一...
    開(kāi)封第一講書(shū)人閱讀 51,610評(píng)論 1 305
  • 那天访敌,我揣著相機(jī)與錄音,去河邊找鬼衣盾。 笑死寺旺,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的势决。 我是一名探鬼主播阻塑,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼果复!你這毒婦竟也來(lái)了陈莽?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤虽抄,失蹤者是張志新(化名)和其女友劉穎走搁,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體迈窟,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡私植,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了车酣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片曲稼。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖骇径,靈堂內(nèi)的尸體忽然破棺而出躯肌,到底是詐尸還是另有隱情,我是刑警寧澤破衔,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布清女,位于F島的核電站,受9級(jí)特大地震影響晰筛,放射性物質(zhì)發(fā)生泄漏嫡丙。R本人自食惡果不足惜拴袭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望曙博。 院中可真熱鬧拥刻,春花似錦、人聲如沸父泳。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)惠窄。三九已至蒸眠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間杆融,已是汗流浹背楞卡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留脾歇,地道東北人蒋腮。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像藕各,于是被迫代替她去往敵國(guó)和親池摧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355