让段落成为文章的基本结构:每一段对应一个主题。
去掉无用的单词。 .
使用主动语态。
避免一连串松散的句子。
将相关的词语放在一起。
陈述句用主动语态。
平行的概念用平行的结构。
让函数成为代码的基本单元。每个函数做一件事。
去掉无用的代码
使用主动语态
避免一连串松散结构的代码
把相关的代码放在一起。
表达式和陈述语句中使用主动语态。
用并行的代码表达并行的概念。
软件开发的本质就是写作。我们把模块、函数、数据结构组合在一起,就有了一个软件程序。
理解如何编写函数并如何构建它们,是软件开发者的基本技能。
交流型函数:执行I/O的函数
功能型函数:一系列指令的合集
映射型函数:给一些输入,返回相应的输出
给定相同的输入,返回相同的输出
没有副作用
刚健的文字是简练的。一句话应该不包含无用的词语,一段话没有无用的句子,正如作画不应该有多余的线条,一个机器没有多余的零件。这就要求作者尽量用短句子,避免罗列所有细节,在大纲里就列出主题,而不是什么都说。William Strunk,Jr.,《英文写作指南》
function secret (message) {
=inherit !important =inherit !importantreturn=inherit !important =inherit !importantfunction=inherit !important =inherit !important()=inherit !important =inherit !important{
=inherit !important =inherit !importantreturn=inherit !important =inherit !importantmessage=inherit !important;
=inherit !important =inherit !important}
=inherit !important};
const secret = msg => () => msg;
const getFullName = ({firstName, lastName}) => {
=inherit !important =inherit !importantconst=inherit !important =inherit !importantfullName=inherit !important = =inherit !importantfirstName=inherit !important + =inherit !important' '=inherit !important + =inherit !importantlastName=inherit !important;
=inherit !important =inherit !importantreturn=inherit !important =inherit !importantfullName=inherit !important;
=inherit !important};
const getFullName = ({firstName, lastName}) => (
=inherit !important =inherit !importantfirstName=inherit !important + =inherit !important' '=inherit !important + =inherit !importantlastName
=inherit !important);
const add2 = a => b => a + b;
=inherit !important// Now we can define a point-free inc()
=inherit !important// that adds 1 to any number.
=inherit !importantconst=inherit !important =inherit !importantinc=inherit !important = =inherit !importantadd2=inherit !important(=inherit !important1=inherit !important);
=inherit !importantinc=inherit !important(=inherit !important3=inherit !important);=inherit !important =inherit !important// 4
const g = n => n + 1;
=inherit !importantconst=inherit !important =inherit !importantf=inherit !important = =inherit !importantn=inherit !important => =inherit !importantn *=inherit !important =inherit !important2=inherit !important;
=inherit !important// With points:
=inherit !importantconst=inherit !important =inherit !importantincThenDoublePoints=inherit !important = =inherit !importantn=inherit !important => =inherit !important{
=inherit !important =inherit !importantconst=inherit !important =inherit !importantincremented=inherit !important = =inherit !importantg=inherit !important(=inherit !importantn=inherit !important);
=inherit !important =inherit !importantreturn=inherit !important =inherit !importantf=inherit !important(=inherit !importantincremented=inherit !important);
=inherit !important};
=inherit !importantincThenDoublePoints=inherit !important(=inherit !important20=inherit !important);=inherit !important =inherit !important// 42
=inherit !important// compose2 - Take two functions and return their composition
=inherit !importantconst=inherit !important =inherit !importantcompose2=inherit !important = =inherit !important(=inherit !importantf=inherit !important,=inherit !important =inherit !importantg=inherit !important)=inherit !important => =inherit !importantx=inherit !important => =inherit !importantf=inherit !important(=inherit !importantg=inherit !important(=inherit !importantx=inherit !important));
=inherit !important// Point-free:
=inherit !importantconst=inherit !important =inherit !importantincThenDoublePointFree=inherit !important = =inherit !importantcompose2=inherit !important(=inherit !importantf=inherit !important,=inherit !important =inherit !importantg=inherit !important);
=inherit !importantincThenDoublePointFree=inherit !important(=inherit !important20=inherit !important);=inherit !important =inherit !important// 42
const compose2 = (f, g) => x => [x].map(g).map(f).pop();
=inherit !importantconst=inherit !important =inherit !importantincThenDoublePointFree=inherit !important = =inherit !importantcompose2=inherit !important(=inherit !importantf=inherit !important,=inherit !important =inherit !importantg=inherit !important);
=inherit !importantincThenDoublePointFree=inherit !important(=inherit !important20=inherit !important);=inherit !important =inherit !important// 42
import pipe from 'lodash/fp/flow';
=inherit !importantpipe=inherit !important(=inherit !importantg=inherit !important,=inherit !important =inherit !importantf=inherit !important)(=inherit !important20=inherit !important);=inherit !important =inherit !important// 42
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);
=inherit !importantpipe=inherit !important(=inherit !importantg=inherit !important,=inherit !important =inherit !importantf=inherit !important)(=inherit !important20=inherit !important);=inherit !important =inherit !important// 42
软件开发的本质是写作。我们把模块、函数、数据结构组合在一起,就构成了软件程序。
如果能用更少的代码表达相同的意思,且不改变或混淆代码含义,那就应该这样做。
如果可以使用更少变量达到相同目的,也不会改变或混淆原意,那也应该这样做。
主动语态比被动语态更加直接、有力。 — William Strunk,Jr. 《英文写作指南》
isActive(user)优于getActiveStatus(user)。
isFirstRun = false;优于firstRun = false;。
increment()优于plusOne()。
unzip()优于filesFromZip()。
filter(fn, array)优于matchingItemsFromArray(fn,array)。
element.onClick(handleClick)优于element.click(handleClick)。
component.onDragStart(handleDragStart)优于component.startDrag(handleDragStart)。
componentWillBeUpdated(doSomething)
componentWillUpdate(doSomething)
componentWillUpdate(doSomething)
component.beforeUpdate(doSomething)优于component.beforeComponentUpdate(doSomething)
const drawUserProfile = ({ userId }) => {
=inherit !important =inherit !importantconst=inherit !important =inherit !importantuserData=inherit !important = =inherit !importantloadUserData=inherit !important(=inherit !importantuserId=inherit !important);
=inherit !important =inherit !importantconst=inherit !important =inherit !importantdataToDisplay=inherit !important = =inherit !importantcalculateDisplayData=inherit !important(=inherit !importantuserData=inherit !important);
=inherit !important =inherit !importantrenderProfileData=inherit !important(=inherit !importantdataToDisplay=inherit !important);
=inherit !important};
├── components
│=inherit !important ├──=inherit !important =inherit !importanttodos
│=inherit !important └──=inherit !important =inherit !importantuser
├──=inherit !important =inherit !importantreducers
│=inherit !important ├──=inherit !important =inherit !importanttodos
│=inherit !important └──=inherit !important =inherit !importantuser
└──=inherit !important =inherit !importanttests
=inherit !important ├──=inherit !important =inherit !importanttodos
=inherit !important └──=inherit !important =inherit !importantuser
├── todos
│=inherit !important ├──=inherit !important =inherit !importantcompo
=inherit !importantnent
│=inherit !important ├──=inherit !important =inherit !importantreducer
│=inherit !important └──=inherit !important =inherit !importanttest
└──=inherit !important =inherit !importantuser
=inherit !important ├──=inherit !important =inherit !importantcomponent
=inherit !important ├──=inherit !important =inherit !importantreducer
=inherit !important └──=inherit !important =inherit !importanttest
“做出明确的断言。避免无聊、不出彩、犹豫、不可置否的语气。使用“not”时应该表达否定或者对立面的意思,而不要用来作为逃避的手段。”William Strunk,Jr., 《英文写作指南》。
if (err) return reject(err);
=inherit !important// do something...
if (!err) {
=inherit !important =inherit !important// ... do something
=inherit !important}=inherit !important =inherit !importantelse=inherit !important =inherit !important{
=inherit !important =inherit !importantreturn=inherit !important =inherit !importantreject=inherit !important(=inherit !importanterr=inherit !important);
=inherit !important}
=inherit !important{
=inherit !important =inherit !important=inherit !important: =inherit !importantiterator=inherit !important =inherit !important?=inherit !important =inherit !importantiterator=inherit !important : =inherit !importantdefaultIterator
=inherit !important}
=inherit !important{
=inherit !important =inherit !important=inherit !important: =inherit !important(=inherit !important!=inherit !importantiterator=inherit !important)=inherit !important =inherit !important?=inherit !important =inherit !importantdefaultIterator=inherit !important : =inherit !importantiterator
=inherit !important}
const createEvent = ({
=inherit !important =inherit !importanttitle=inherit !important = =inherit !important'Untitled'=inherit !important,
=inherit !important =inherit !importantdescription=inherit !important = =inherit !important''=inherit !important,
=inherit !important =inherit !importanttimeStamp=inherit !important = =inherit !importantDate=inherit !important.=inherit !importantnow=inherit !important()
=inherit !important})=inherit !important => =inherit !important// ...
=inherit !important// later...
=inherit !importantconst=inherit !important =inherit !importantbirthdayParty=inherit !important = =inherit !importantcreateEvent=inherit !important({
=inherit !important =inherit !importanttitle=inherit !important = =inherit !important'Birthday Party'=inherit !important,
=inherit !important =inherit !importanttimeStamp=inherit !important = =inherit !importantbirthDay
=inherit !important});
const createEvent(
=inherit !important =inherit !importanttitle=inherit !important = =inherit !important'Untitled'=inherit !important,
=inherit !important =inherit !importantdescription=inherit !important = =inherit !important''=inherit !important,
=inherit !important =inherit !importanttimeStamp=inherit !important = =inherit !importantDate=inherit !important.=inherit !importantnow=inherit !important()
=inherit !important);
=inherit !important// later...
=inherit !importantconst=inherit !important =inherit !importantbirthdayParty=inherit !important = =inherit !importantcreateEvent=inherit !important(
=inherit !important =inherit !important'Birthday Party'=inherit !important,
=inherit !important =inherit !importantundefined=inherit !important,=inherit !important =inherit !important// This was avoidable
=inherit !important =inherit !importantbirthDay
=inherit !important);
平行结构需要尽可能相似的结构表达语义。格式上的形似使得读者能够理解不同语句的意义也是相似的。- William Strunk,Jr., 《英文写作指南》
刚健的文字是简练的。一句话应该不包含无用的词语,一段话没有无用的句子,正如作画不应该有多余的线条,一个机器没有多余的零件。这就要求作者尽量用短句子,避免罗列所有细节,在大纲里就列出主题,而不是什么都说。-William Strunk,Jr.,《英文写作指南》
英文:Eric Elliott 译文:众成翻译
地址:http://www.zcfy.cc/article/ele ... .html
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。