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

linux 机器格式化_为什么机器人应该为我们格式化代码相关推荐

  1. 发明导诊机器人团队_导诊机器人的制作方法

    本实用新型涉及导诊机器人. 技术背景 数据显示,目前,世界上至少有48个国家在发展机器人,其中25个国家已涉足服务型机器人开发.在日本.北美和欧洲,迄今已有7种类型计40余款服务型机器人进入实验和半商 ...

  2. 发明导诊机器人团队_导诊机器人品牌

    智能形象:科技馆的形象宣传代言人,当讲解员换成机器人,无需向参馆者推荐介绍,自然而然吸引观众体验,自带宣传效果,提升展馆形象. 专业讲解员:全面掌握馆内展品专业知识,拥有规范化的讲解技能,为参馆者提供 ...

  3. js 格式化 java时间格式化_用JavaScript(js)对时间格式化

    可以说是Web项目中不可或缺的一个Javascript类库,它可以帮助你快速的解决客户端编程的许多问题,下面贴出一个用js格式化时间的方法. Date.prototype.format=functio ...

  4. 查看cpu使用率_腾讯游戏开发工程师:Linux 机器 CPU 毛刺问题排查

    作者:jasonzxpan,腾讯 IEG 运营开发工程师 本文排查一个Linux 机器 CPU 毛刺问题,排查过程中不变更进程状态.也不会影响线上服务,最后还对 CPU 毛刺带来的风险进行了分析和验证 ...

  5. vm ubuntu设置中文_如何在本地Ubuntu Linux机器或VM上设置LAMP服务器

    vm ubuntu设置中文 The purpose of this brief guide is to take you through the process of setting up a LAM ...

  6. 玛酷机器人与艾迪瑞特_加盟玛酷机器人和艾克瑞特机器人哪个好

    现在,很多的人都对机器人感兴趣,其实,机器人只是我们的一个媒介,我们通过机器人这个媒介,提高孩子的学习能力,锻炼孩子的逻辑思维,但是很多的加盟者对机器人领域的了解比较少,很难根据自己的实际情况选择出合 ...

  7. Linux驱动开发_设备文件系统详解

    目录 何为设备管理器? Linux下dev的作用 Devfs sysfs kobject udev proc 何为设备管理器? 设备管理器就是负责管理这台电脑上的外设,当我们通过电脑提供的USB口插入 ...

  8. linux格式化磁盘命令(磁盘分区及格式化)

    Ubuntu下挂载一个新硬盘的基本步骤是: 给硬盘创建分区; 给硬盘创建文件系统; 挂载移动硬盘. 需要用到的命令: lsblk #查看所有硬盘情况 df -lh #查看硬盘占用情况,以及挂载位置 s ...

  9. linux基础知识_压缩—进程管理-网络管理-ftp-nfs-ssh-scp

    linux基础知识_压缩-进程管理-网络管理-ftp-nfs-ssh-scp 1.压缩包管理 gzip .gz格式的压缩包,不打包,分别压缩,原文件消失 bzip2 .bz2格式的压缩包,原文件不会消 ...

最新文章

  1. python实现密码的强度_字符串处理函数(二)python语言实现密码强度校验
  2. Web字体库下载及转换工具
  3. 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google
  4. Windows进程与线程学习笔记(三)—— KPCR
  5. php exec grep 写错误,又遇到了grep的结果毫无意义的错误性显示
  6. webp转换gif_右键转换文件格式
  7. CF1526 D. Kill Anton
  8. DM框架中CString/CStringW 转 double (c++通用)
  9. 算法工程师,『工程』二字怎么破?
  10. 二分法和牛顿迭代实现开根号函数:OC的实现
  11. 基于Kinetis系列微控制器K60芯片的I2C接口函数程序说明1
  12. 27学java能找到工作吗_今年27,想自学Java,转行程序员,请问可行吗?
  13. 【信道估计】LS/MMSE信道估计,CS信道估计的MATLAB仿真
  14. google hacking 记录
  15. fms 连 mysql_FMS+Thinkphp+Mysql 直播源代码,开源可消耗
  16. [汇编] 在屏幕中央显示时钟
  17. Linux:进程控制
  18. matlab生成随机矩阵
  19. BEA提出SOA四大策略
  20. 字节跳动2020春招后端开发工程师笔试复盘

热门文章

  1. asp.net core 系列 6 MVC框架路由(下)
  2. [bzoj 2726] 任务安排 (斜率优化 线性dp)
  3. 51nod1270(dp)
  4. Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀和
  5. [Java]向上/下转型Casting
  6. 【转】C++ Vector用法深入剖析
  7. QuickWAP V1.5利用ASP读取Access记录集一例
  8. 使用工具类实现通用分页处理
  9. DNS Bind9在windows7下
  10. jeesite缓存问题