linux 機器格式化_為什么機器人應該為我們格式化代碼

linux 機器格式化

by Artem Sapegin

通過Artem Sapegin

為什么機器人應該為我們格式化代碼 (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.

我曾經認為個人代碼樣式對程序員來說是一件好事。 它表明您是一個成熟的開發人員,他知道好的代碼應該是什么樣。

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.

我的大學教授告訴我,由于某些代碼風格不同,他們知道我的一些同學何時在工作中使用我的代碼。 現在,我認為這是因為我的代碼至少已以某種方式進行了格式化,而其他所有人都陷入了混亂。

Since then I’ve spent a lot of time arguing code style and configuring tools to enforce it. It’s time for a change.

從那時起,我花了很多時間爭論代碼風格并配置工具來實施它。 現在該進行更改了。

一些例子 (A few examples)

After reading the The Programmers’ Stone I put braces like this for a long time:

看完《程序員的石頭》之后,我放了很長時間這樣的括號:

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:

但是后來我意識到,我可能是前端社區中唯一這樣做的人。 其他人都使用這種風格:

if (food === 'pizza') {    alert('Pizza ;-)');  } else {    alert('Not pizza ;-(');}

Or this:

或這個:

if (food === 'pizza') {    alert('Pizza ;-)');  }else {      alert('Not pizza ;-(');}

So I’ve changed my style to the last one.

因此,我已將樣式更改為上一個樣式。

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:

我看到了與尾隨逗號相同的重構優勢:

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.

但是我可能比使用牙套更孤單。 沒有人會以這種樣式向我發送代碼以供審查,沒有短毛絨犬可以強制執行它。 因此,我也必須停止使用它以更接近真實世界。

There’s another thing that nobody else does except me . I always put two spaces before end-of-the-line comment:

除了我以外,沒有人能做的另一件事。 我總是在行尾注釋前放置兩個空格:

const volume = 200;  // ml

I thought it improves readability. But it actually makes the codebase inconsistent because other developers only put one space.

我認為它可以提高可讀性。 但這實際上使代碼庫不一致,因為其他開發人員只放置了一個空格。

JavaScript開發人員的工作 (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沒有官方代碼風格。 有一些流行的代碼樣式,例如Airbnb或Standard 。 您可以使用它們使您的代碼看起來對其他開發人員熟悉。

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強制執行代碼樣式,甚至自動格式化代碼。 但這不會使您的代碼庫100%一致。 帶有Airbnb配置的ESLint僅會規范我的第一個示例,而在其他兩個示例中允許不一致。

JavaScript開發人員應該做什么 (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.

一些語言具有嚴格的代碼樣式和格式化代碼的工具。 因此,開發人員不會浪費時間爭論代碼風格。 在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.

看來JavaScript終于可以解決此問題。 一個名為Prettier的新工具將使用其自己的規則重新格式化您的代碼。 首先,它完全忽略了代碼是如何編寫的。

Let’s try Prettier on my examples:

讓我們在示例中嘗試更漂亮 :

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; // ml

You 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:

您可以不同意這種風格。 例如,我不喜歡else放置和在一行中編寫函數鏈是有問題的。 但是我發現采用Prettier具有巨大的好處:

  • Almost no decisions to make — Prettier has few options.

    幾乎沒有任何決定-Prettier幾乎沒有選擇。
  • No arguing about particular rules if you’re working in a team.

    如果您在團隊中工作,則無需爭論特定的規則。
  • No need to learn your project’s code style for contributors.

    無需為貢獻者學習項目的代碼風格。
  • No need to fix style issues reported by ESLint.

    無需修復ESLint報告的樣式問題。
  • Possible to set up autoformat on file save.

    可以在文件保存時設置自動格式化。

結論 (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等一些受歡迎的項目采用。 我開始將所有項目從我的自定義代碼樣式轉換為Prettier。 我會推薦它而不是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在一起經歷了很多“丑陋,丑陋”的時刻。 但是,當我認為我必須(例如)在添加另一項道具時將JSX代碼從單行手動重新格式化為多行時,又不適合一行,我意識到這是完全值得的。

Read how to set up Prettier in your project.

閱讀如何在項目中設置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 看看我的新工具 ,它將簡化向您的項目中添加ESLint,Prettier和其他工具的過程,并使它們的配置保持同步。

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 機器格式化

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395245.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395245.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395245.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Pytest高級進階之Fixture

From: https://www.jianshu.com/p/54b0f4016300 一. fixture介紹 fixture是pytest的一個閃光點,pytest要精通怎么能不學習fixture呢?跟著我一起深入學習fixture吧。其實unittest和nose都支持fixture,但是pytest做得更炫。 fixture是pytest特有…

mysql 慢日志報警_一則MySQL慢日志監控誤報的問題分析

之前因為各種原因,有些報警沒有引起重視,最近放假馬上排除了一些潛在的人為原因,發現數據庫的慢日志報警有些奇怪,主要表現是慢日志報警不屬實,收到報警的即時通信提醒后,隔一會去數據庫里面去排查&#xf…

用css實現自定義虛線邊框

開發產品功能的時候ui往往會給出虛線邊框的效果圖,于是乎,我們往往第一時間想到的是用css里的border,可是border里一般就提供兩種效果,dashed或者dotted,ui這時就不滿意了,說虛線太密了。廢話不多說&#x…

無限復活服務器,絕地求生無限復活模式怎么玩 無限復活新手教程

相信不少的絕地求生玩家們最近都聽說了其無限復活模式吧?因此肯定想要知道這種模式究竟該怎么玩,所以下面就來為各位帶來此玩法的攻略相關,希望各位在看了如下的內容之后恩呢狗狗了解到新手教程攻略一覽。“War”模式的設定以及玩法規則如下&#xff1a…

lua math.random()

math.random([n [,m]]) 用法:1.無參調用,產生[0, 1)之間的浮點隨機數。 2.一個參數n,產生[1, n]之間的整數。 3.兩個參數,產生[n, m]之間的整數。 math.randomseed(n) 用法:接收一個整數n作為隨即序列的種子。 例&…

零基礎學習ruby_學習Ruby:從零到英雄

零基礎學習ruby“Ruby is simple in appearance, but is very complex inside, just like our human body.” — Matz, creator of the Ruby programming language“ Ruby的外觀很簡單,但是內部卻非常復雜,就像我們的人體一樣。” — Matz ,R…

windows同時啟動多個微信

1、創建mychat.bat文件(文件名任意),輸入以下代碼,其中"C:\Program Files (x86)\Tencent\WeChat\"為微信的安裝路徑。以下示例為同時啟動兩個微信 start/d "C:\Program Files (x86)\Tencent\WeChat\" Wechat.exe start/d "C:\P…

mysql date time year_YEAR、DATE、TIME、DATETIME和TIMESTAMP詳細介紹[MySQL數據類型]

為了方便在數據庫中存儲日期和時間,MySQL提供了表示日期和時間的數據類型,分別是YEAR、DATE、TIME、DATETIME和TIMESTAMP。下面列舉了這些MSL中日期和時間數據類型所對應的字節數、取值范圍、日期格式以及零值。從上圖中可以看出,每種日期和時…

九度oj 題目1380:lucky number

題目描述:每個人有自己的lucky number,小A也一樣。不過他的lucky number定義不一樣。他認為一個序列中某些數出現的次數為n的話,都是他的lucky number。但是,現在這個序列很大,他無法快速找到所有lucky number。既然這…

安裝Tengine

1.安裝VMware2.安裝CentOS6.53.配置網絡a.修改 /etc/sysconfig/network-scripts/ifcfg-eth0配置文件,添加如下內容DEVICEeth0HWADDR00:0C:29:96:01:6BTYPEEthernetUUID41cbd943-024b-4341-ac7a-e4d2142b4938ONBOOTyesNM_CONTROLLEDyesBOOTPROTOnoneIPADDRxxx.xxx.x.xxx#例如:IP…

node seneca_使用Node.js和Seneca編寫國際象棋微服務,第2部分

node seneca處理新需求而無需重構 (Handling new requirements without refactoring) Part 1 of this series talked about defining and calling microservices using Seneca. A handful of services were created to return all legal moves of a lone chess piece on a ches…

【OCR技術系列之八】端到端不定長文本識別CRNN代碼實現

CRNN是OCR領域非常經典且被廣泛使用的識別算法,其理論基礎可以參考我上一篇文章,本文將著重講解CRNN代碼實現過程以及識別效果。 數據處理 利用圖像處理技術我們手工大批量生成文字圖像,一共360萬張圖像樣本,效果如下:…

mysql 修改字段類型死鎖_mysql數據庫死鎖的產生原因及解決辦法

數據庫和操作系統一樣,是一個多用戶使用的共享資源。當多個用戶并發地存取數據 時,在數據庫中就會產生多個事務同時存取同一數據的情況。若對并發操作不加控制就可能會讀取和存儲不正確的數據,破壞數據庫的一致性。加鎖是實現數據庫并 發控制…

openwrt無盤服務器,搭建基于 OpenWrt/gPXE/iSCSI 的 Windows 無盤工作站

本文要介紹的是如何在 OpenWrt 平臺上面搭建無盤工作站服務器以及 Windows 的 iSCSI 部署。當然,由于 OpenWrt 也可以算得上一種 Linux 發行版了,所以本文所介紹的一些方法,在其它 Linux 發行版上面仍有一定的參考價值。整個過程大概分為以下…

Ralink5350開發環境搭建

一、安裝虛擬機(Oracle VM VirtualBox 或 VMware Workstation) 二、在虛擬機中安裝linux操作系統(當前使用的是Ubuntu1204桌面版) 三、配置linux相關服務 安裝、配置、啟動ftp服務apt-get install vsftpd 改動 vsftpd 的配置文件 /etc/vsftpd.conf,將以…

figma下載_Figma重新構想的團隊庫

figma下載一個新的,功能更強大的界面,用于在整個組織中共享組件 (A new, more powerful interface for sharing Components across your organization) The Team Library in Figma is a set of shared Components across all files in a Team. Component…

boost python導出c++ map_使用Boost生成的Python模塊:與C++簽名不匹配

我正在使用名為Mitsuba的軟件。它附帶了一個用Boost包裝的Python實現。 Python中的這一行:使用Boost生成的Python模塊:與C簽名不匹配scene SceneHandler.loadScene(fileResolver.resolve("model.xml"), paramMap)產生一個錯誤。根據文檔&…

CSU-1982 小M的移動硬盤

CSU-1982 小M的移動硬盤 Description 最近小M買了一個移動硬盤來儲存自己電腦里不常用的文件。但是他把這些文件一股腦丟進移動硬盤后,覺得這些文件似乎沒有被很好地歸類,這樣以后找起來豈不是會非常麻煩? 小M最終決定要把這些文件好好歸類&a…

杜比服務器系統安裝教程,win10杜比音效如何安裝?win10安裝杜比音效的詳細教程...

杜比音效想必大家都不陌生,聽歌或者看電影開啟杜比音效可以給人一種身臨其境的感覺。不少朋友都升級了win10系統卻不知道如何安裝杜比音效?如何為自己的系統安裝杜比音效呢?感興趣的小伙伴請看下面的操作步驟。win10安裝杜比音效的方法&#…

劍指Offer_52_正則表達式匹配

題目描述 請實現一個函數用來匹配包括.和的正則表達式。模式中的字符.表示任意一個字符,而表示它前面的字符可以出現任意次(包含0次)。 在本題中,匹配是指字符串的所有字符匹配整個模式。例如,字符串"aaa"與…