美文网首页
英语学习之《Clean Code》英文交流术语整理(二)

英语学习之《Clean Code》英文交流术语整理(二)

作者: 朱本威 | 来源:发表于2019-02-18 22:51 被阅读0次

    如果有一天,有人告诉你,你将来需要和一帮只会说英语和泰语的家伙工作在一起,而你的工作是要告诉他们如何写整洁的代码,如何做重构,什么是TDD,什么是面向对象,你该怎么办?

    你问我,我只能告诉你,我需要重新读一次《整洁代码》,《重构》以及《
    敏捷软件开发:原则、实践和模式》的英文版,然后整理出里面可以为我使用的术语和表达。

    第二章:Functions(函数方法)

    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,意会)

    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(小而美的函数)

    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(低一级的抽象)

    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(每个函数具有相同的抽象层次)

    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(不要担心名字太长)

    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(打磨代码,拆分函数,修改命名,消除重复)

    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(未完待续)

    相关文章

      网友评论

          本文标题:英语学习之《Clean Code》英文交流术语整理(二)

          本文链接:https://www.haomeiwen.com/subject/xflneqtx.html