linux 机器格式化_为什么机器人应该为我们格式化代码
linux 機(jī)器格式化
by Artem Sapegin
通過(guò)Artem Sapegin
為什么機(jī)器人應(yīng)該為我們格式化代碼 (Why robots should format our code for us)
I used to think that a personal code style is a good thing for a programmer. It shows you are a mature developer who knows what good code should look like.
我曾經(jīng)認(rèn)為個(gè)人代碼樣式對(duì)程序員來(lái)說(shuō)是一件好事。 它表明您是一個(gè)成熟的開(kāi)發(fā)人員,他知道好的代碼應(yīng)該是什么樣。
My college professors told me that they knew when some of my classmates used my code in their work because of the different code style. Now I think it was because my code was at least somehow formatted and everyone else’s was a mess.
我的大學(xué)教授告訴我,由于某些代碼風(fēng)格不同,他們知道我的一些同學(xué)何時(shí)在工作中使用我的代碼。 現(xiàn)在,我認(rèn)為這是因?yàn)槲业拇a至少已以某種方式進(jìn)行了格式化,而其他所有人都陷入了混亂。
Since then I’ve spent a lot of time arguing code style and configuring tools to enforce it. It’s time for a change.
從那時(shí)起,我花了很多時(shí)間爭(zhēng)論代碼風(fēng)格并配置工具來(lái)實(shí)施它。 現(xiàn)在該進(jìn)行更改了。
一些例子 (A few examples)
After reading the The Programmers’ Stone I put braces like this for a long time:
看完《程序員的石頭》之后,我放了很長(zhǎng)時(shí)間這樣的括號(hào):
if (food === 'pizza'){ alert('Pizza ;-)'); }else{ alert('Not pizza ;-(');}But then I realized that I may be the only one who did it that way in the front-end community. Everybody else uses this style:
但是后來(lái)我意識(shí)到,我可能是前端社區(qū)中唯一這樣做的人。 其他人都使用這種風(fēng)格:
if (food === 'pizza') { alert('Pizza ;-)'); } else { alert('Not pizza ;-(');}Or this:
或這個(gè):
if (food === 'pizza') { alert('Pizza ;-)'); }else { alert('Not pizza ;-(');}So I’ve changed my style to the last one.
因此,我已將樣式更改為上一個(gè)樣式。
I like this style for chaining very much:
我非常喜歡這種鏈接樣式:
function foo(items) { return items .filter(item => item.checked) .map(item => item.value) ;}I see the same refactoring benefits as for trailing commas:
我看到了與尾隨逗號(hào)相同的重構(gòu)優(yōu)勢(shì):
const food = [ 'pizza', 'burger', 'pasta',]But I’m probably even more lonely with this style than I was with braces. Nobody would ever send me code for review with this style, no linter can enforce it. So I have to stop using it too to be closer to the real world.
但是我可能比使用牙套更孤單。 沒(méi)有人會(huì)以這種樣式向我發(fā)送代碼以供審查,沒(méi)有短毛絨犬可以強(qiáng)制執(zhí)行它。 因此,我也必須停止使用它以更接近真實(shí)世界。
There’s another thing that nobody else does except me . I always put two spaces before end-of-the-line comment:
除了我以外,沒(méi)有人能做的另一件事。 我總是在行尾注釋前放置兩個(gè)空格:
const volume = 200; // mlI thought it improves readability. But it actually makes the codebase inconsistent because other developers only put one space.
我認(rèn)為它可以提高可讀性。 但這實(shí)際上使代碼庫(kù)不一致,因?yàn)槠渌_(kāi)發(fā)人員只放置了一個(gè)空格。
JavaScript開(kāi)發(fā)人員的工作 (What JavaScript developers do)
Unfortunately JavaScript has no official code style. There are a few popular code styles like Airbnb or Standard. You could use them to make your code look familiar to other developers.
不幸的是,JavaScript沒(méi)有官方代碼風(fēng)格。 有一些流行的代碼樣式,例如Airbnb或Standard 。 您可以使用它們使您的代碼看起來(lái)對(duì)其他開(kāi)發(fā)人員熟悉。
You could use ESLint to enforce code style and even autoformat code in some cases. But it won’t make your code base 100% consistent. ESLint with Airbnb config would normalize only my first example and allow inconsistency in the other two examples.
在某些情況下,您可以使用ESLint強(qiáng)制執(zhí)行代碼樣式,甚至自動(dòng)格式化代碼。 但這不會(huì)使您的代碼庫(kù)100%一致。 帶有Airbnb配置的ESLint僅會(huì)規(guī)范我的第一個(gè)示例,而在其他兩個(gè)示例中允許不一致。
JavaScript開(kāi)發(fā)人員應(yīng)該做什么 (What JavaScript developers should do)
Some languages have strict code styles and tools to format code. So developers don’t waste time arguing code style. Look at Refmt for Reason and Rustfmt for Rust.
一些語(yǔ)言具有嚴(yán)格的代碼樣式和格式化代碼的工具。 因此,開(kāi)發(fā)人員不會(huì)浪費(fèi)時(shí)間爭(zhēng)論代碼風(fēng)格。 在Refmt中查找原因,在Rustfmt中查找Rust。
It looks like JavaScript finally has a solution to this problem. A new tool called Prettier will reformat your code using its own rules. It completely ignores how the code was written in the first place.
看來(lái)JavaScript終于可以解決此問(wèn)題。 一個(gè)名為Prettier的新工具將使用其自己的規(guī)則重新格式化您的代碼。 首先,它完全忽略了代碼是如何編寫的。
Let’s try Prettier on my examples:
讓我們?cè)谑纠袊L試更漂亮 :
if (food === 'pizza') { alert('Pizza ;-)');} else { alert('Not pizza ;-(');}function foo(items) { return items.filter(item => item.checked).map(item => item.value);}const volume = 200; // mlYou can disagree with this style. For example I don’t like the else placement and writing function chains in one line is questionable. But I see huge benefits in adopting Prettier:
您可以不同意這種風(fēng)格。 例如,我不喜歡else放置和在一行中編寫函數(shù)鏈?zhǔn)怯袉?wèn)題的。 但是我發(fā)現(xiàn)采用Prettier具有巨大的好處:
- Almost no decisions to make — Prettier has few options. 幾乎沒(méi)有任何決定-Prettier幾乎沒(méi)有選擇。
- No arguing about particular rules if you’re working in a team. 如果您在團(tuán)隊(duì)中工作,則無(wú)需爭(zhēng)論特定的規(guī)則。
- No need to learn your project’s code style for contributors. 無(wú)需為貢獻(xiàn)者學(xué)習(xí)項(xiàng)目的代碼風(fēng)格。
- No need to fix style issues reported by ESLint. 無(wú)需修復(fù)ESLint報(bào)告的樣式問(wèn)題。
- Possible to set up autoformat on file save. 可以在文件保存時(shí)設(shè)置自動(dòng)格式化。
結(jié)論 (Conclusion)
Prettier has been already adopted by some popular projects like React and Babel. And I’m starting to convert all my projects from my custom code style to Prettier. I will recommend it instead of the Airbnb code style.
Prettier已被React和Babel等一些受歡迎的項(xiàng)目采用。 我開(kāi)始將所有項(xiàng)目從我的自定義代碼樣式轉(zhuǎn)換為Prettier。 我會(huì)推薦它而不是Airbnb代碼樣式。
At first I had a lot of “Ugh, that’s ugly” moments with Prettier. But when I think that I’d have to, for example, manually reformat JSX code from a single-line to multi-line when I add another prop and it doesn’t fit on one line — I realize that it’s totally worth it.
起初,我和Prettier在一起經(jīng)歷了很多“丑陋,丑陋”的時(shí)刻。 但是,當(dāng)我認(rèn)為我必須(例如)在添加另一項(xiàng)道具時(shí)將JSX代碼從單行手動(dòng)重新格式化為多行時(shí),又不適合一行,我意識(shí)到這是完全值得的。
Read how to set up Prettier in your project.
閱讀如何在項(xiàng)目中設(shè)置Prettier 。
P. S. Have a look at my new tool that will simplify adding ESLint, Prettier, and other tools to your project, as well as keeping their configs in sync.
PS 看看我的新工具 ,它將簡(jiǎn)化向您的項(xiàng)目中添加ESLint,Prettier和其他工具的過(guò)程,并使它們的配置保持同步。
Subscribe to my newsletter: https://tinyletter.com/sapegin
訂閱我的新聞通訊: https : //tinyletter.com/sapegin
翻譯自: https://www.freecodecamp.org/news/why-robots-should-format-our-code-159fd06d17f7/
linux 機(jī)器格式化
總結(jié)
以上是生活随笔為你收集整理的linux 机器格式化_为什么机器人应该为我们格式化代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 面试官面试前端_如何面试面试官
- 下一篇: 零基础学习ruby_学习Ruby:从零到