为什么 Prettier?
建立和执行一种风格指南
¥Building and enforcing a style guide
到目前为止,采用 Prettier 的最大原因是为了停止所有正在进行的关于风格的争论。人们普遍认为,拥有一个通用的风格指南对项目和团队来说是有价值的 但到达那里是一个非常痛苦且没有回报的过程。人们对编写代码的特定方式非常情绪化,没有人喜欢花时间编写和接收没用的东西。
¥By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process. People get very emotional around particular ways of writing code and nobody likes spending time writing and receiving nits.
那么为什么选择“Prettier 风格指南”而不是任何其他随机风格指南呢?因为 Prettier 是唯一全自动的“风格指南”。即使 Prettier 没有 100% 按照你想要的方式格式化所有代码,考虑到 Prettier 的独特优势,“牺牲”也是值得的,你不觉得吗?
¥So why choose the “Prettier style guide” over any other random style guide? Because Prettier is the only “style guide” that is fully automatic. Even if Prettier does not format all code 100% the way you’d like, it’s worth the “sacrifice” given the unique benefits of Prettier, don’t you think?
“我们希望释放思维线索并结束围绕风格的讨论。虽然有时会取得成果,但这些讨论大部分都是浪费。”
¥“We want to free mental threads and end discussions around style. While sometimes fruitful, these discussions are for the most part wasteful.”
“实际上,工程师花费了巨大的精力来清理我们所有的代码,因为我们对三元样式争论了很长时间并且对此不一致。这很愚蠢,但这是一场奇怪的持续“大辩论”,浪费了很多来回的时间。现在我们大家都更容易达成一致:只要运行 Prettier,并遵循这种风格即可。”
¥“Literally had an engineer go through a huge effort of cleaning up all of our code because we were debating ternary style for the longest time and were inconsistent about it. It was dumb, but it was a weird on-going “great debate” that wasted lots of little back and forth bits. It’s far easier for us all to agree now: just run Prettier, and go with that style.”
“告诉人们如何设计他们的产品代码已经很累了。”
¥“Getting tired telling people how to style their product code.”
“我们的首要原因是停止浪费时间讨论风格问题。”
¥“Our top reason was to stop wasting our time debating style nits.”
“设置 githook 减少了 PR 中样式问题的数量,这些问题会因 ESLint 规则或我稍后必须挑剔或清理的事情而导致构建损坏。”
¥“Having a githook set up has reduced the amount of style issues in PRs that result in broken builds due to ESLint rules or things I have to nit-pick or clean up later.”
“我不希望任何人再挑剔其他人。”
¥“I don’t want anybody to nitpick any other person ever again.”
“这让我想起史蒂夫·乔布斯过去每天都穿同样的衣服,因为他有一百万个决定要做,他不想为挑选衣服这样的琐事而烦恼。我觉得 Prettier 就是这样的。”
¥“It reminds me of how Steve Jobs used to wear the same clothes every day because he has a million decisions to make and he didn’t want to be bothered to make trivial ones like picking out clothes. I think Prettier is like that.”
帮助新人
¥Helping Newcomers
Prettier 通常由对当前代码库和 JavaScript 有经验的人介绍,但从中受益最多的人是代码库的新手。有人可能认为它只对编程经验非常有限的人有用,但我们已经看到它加快了有经验的工程师加入公司的速度,因为他们之前可能使用不同的编码风格,而开发者来自不同的编程语言。
¥Prettier is usually introduced by people with experience in the current codebase and JavaScript but the people that disproportionally benefit from it are newcomers to the codebase. One may think that it’s only useful for people with very limited programming experience, but we've seen it quicken the ramp up time from experienced engineers joining the company, as they likely used a different coding style before, and developers coming from a different programming language.
“我使用 Prettier 的动机是:看起来我知道如何很好地编写 JavaScript。”
¥“My motivations for using Prettier are: appearing that I know how to write JavaScript well.”
“我总是把空格放错地方,现在我不用再担心了。”
¥“I always put spaces in the wrong place, now I don’t have to worry about it anymore.”
“当你是初学者时,你会犯很多由语法引起的错误。感谢 Prettier,你可以减少这些错误并节省大量时间来专注于真正重要的事情。”
¥“When you're a beginner you're making a lot of mistakes caused by the syntax. Thanks to Prettier, you can reduce these mistakes and save a lot of time to focus on what really matters.”
“作为老师,我也会告诉我的学生安装 Prettier,帮助他们学习 JS 语法并拥有可读的文件。”
¥“As a teacher, I will also tell to my students to install Prettier to help them to learn the JS syntax and have readable files.”
编写代码
¥Writing code
人们在使用 Prettier 后通常会发生的事情是,他们意识到他们实际上花费了大量时间和精力来格式化他们的代码。借助 Prettier 编辑器集成,你只需按下魔术键绑定以及按一下,代码就会被格式化。如果有的话,这是一次令人大开眼界的体验。
¥What usually happens once people are using Prettier is that they realize that they actually spend a lot of time and mental energy formatting their code. With Prettier editor integration, you can just press that magic key binding and poof, the code is formatted. This is an eye opening experience if anything else.
“我想写代码。无需花费周期进行格式化。”
¥“I want to write code. Not spend cycles on formatting.”
“它消除了我们日常生活中 5% 的麻烦 - 又名格式化”
¥“It removed 5% that sucks in our daily life - aka formatting”
“现在已经是 2017 年了,当你碰巧添加一个参数使其超过 80 列限制时,将调用分成多行仍然很痛苦:(“
¥“We're in 2017 and it’s still painful to break a call into multiple lines when you happen to add an argument that makes it go over the 80 columns limit :(“
易于采用
¥Easy to adopt
我们非常努力地使用争议最少的编码风格,经历了多轮修复所有边缘案例并完善了入门体验。当你准备好将 Prettier 推入你的代码库时,不仅在技术上对你来说应该毫不费力,而且新格式化的代码库不应该产生重大争议并被你的同事轻松接受。
¥We've worked very hard to use the least controversial coding styles, went through many rounds of fixing all the edge cases and polished the getting started experience. When you're ready to push Prettier into your codebase, not only should it be painless for you to do it technically but the newly formatted codebase should not generate major controversy and be accepted painlessly by your co-workers.
“开销很低。我们无需做太多工作就能将 Prettier 投入到不同类型的存储库中。”
¥“It’s low overhead. We were able to throw Prettier at very different kinds of repos without much work.”
“它基本上没有错误。如果在实现过程中出现重大样式问题,我们会对将其扔到我们的 JS 代码库中保持警惕。我很高兴地说事实并非如此。”
¥“It’s been mostly bug free. Had there been major styling issues during the course of implementation we would have been wary about throwing this at our JS codebase. I’m happy to say that’s not the case.”
“每个人都将它作为预提交脚本的一部分运行,我们中的一些人也在保存扩展中使用编辑器。”
¥“Everyone runs it as part of their pre commit scripts, a couple of us use the editor on save extensions as well.”
“速度很快,针对我们较大的 JS 代码库之一,我们能够在 13 秒内运行 Prettier。”
¥“It’s fast, against one of our larger JS codebases we were able to run Prettier in under 13 seconds.”
“Prettier 对我们来说最大的好处是能够立即格式化整个代码库。”
¥“The biggest benefit for Prettier for us was being able to format the entire code base at once.”
清理现有的代码库
¥Clean up an existing codebase
由于提出一种编码风格并实现它是一项艰巨的任务,它经常会被遗漏,你只能处理不一致的代码库。在这种情况下运行 Prettier 是一个快速的胜利,代码库现在是统一的并且更容易阅读,几乎不需要花费任何时间。
¥Since coming up with a coding style and enforcing it is a big undertaking, it often slips through the cracks and you are left working on inconsistent codebases. Running Prettier in this case is a quick win, the codebase is now uniform and easier to read without spending hardly any time.
“看看代码:)我只需要恢复理智。”
¥“Take a look at the code :) I just need to restore sanity.”
“我们继承了约 2000 个模块的 ES6 代码库,这些代码库是由全球团队的 20 名不同开发者在 18 个月内开发的。感觉就像一场胜利,无需进行太多研究。”
¥“We inherited a ~2000 module ES6 code base, developed by 20 different developers over 18 months, in a global team. Felt like such a win without much research.”
乘坐炒作列车
¥Ride the hype train
项目的纯技术方面并不是人们在选择采用 Prettier 时唯一考虑的因素。谁构建和使用它,以及它在社区中传播的速度有多快,都会产生不小的影响。
¥Purely technical aspects of the projects aren’t the only thing people look into when choosing to adopt Prettier. Who built and uses it and how quickly it spreads through the community has a non-trivial impact.
“对我来说,令人惊奇的是:1)2)3)
¥“The amazing thing, for me, is: 1) Announced 2 months ago. 2) Already adopted by, it seems, every major JS project. 3) 7000 stars, 100,000 npm downloads/mo”
“与 React 和 React Native 是由同一个人构建的。”
¥“Was built by the same people as React & React Native.”
“我喜欢成为热门新事物的一部分。”
¥“I like to be part of the hot new things.”
“因为很快人们就会要求它。”
¥“Because soon enough people are gonna ask for it.”