Table of Contents
概述
多人在同一个项目协作的时候保持统一的编码风格很重要。这说起来容易做起来难,本文我推荐一些方法、工具有助于团队保持同样的编码风格。
选择编程语言
如果可以,选择go语言作为编程语言。
go语言自带代码格式化命令gofmt,要求所有代码都有一致的代码风格,甚至不允许未使用的import和未使用的变量定义在程序里,否则编译不通过。
go的关键字仅25个,是我见过最少的。相比而言,c语言有32个,c++ 49个,python 33个,JavaScript 63个,java 53个。关键字精简大家写出来的代码就更趋向于一致。
sonarqube是专门做代码静态分析的工具,它内置了一套自己的规则来检查代码是否符合规范。就内置的编码规范来看,go只有46个,Java可能出现的问题或者code smell最多,达到533个,一个团队要投入多少精力(学习成本、review成本、修改成本)去避免这种编码规范的问题?长期下来是一个无法忽视的投入。
| 语言 | sonarqube编码规范 |
|---|---|
| Java | 533 |
| Python | 440 |
| C# | 373 |
| JavaScript | 219 |
| PHP | 185 |
| VB.NET | 144 |
| TypeScript | 129 |
| Flex | 79 |
| HTML | 62 |
| Kotlin | 49 |
| Ruby | 48 |
| Scala | 47 |
| Go | 46 |
| CSS | 30 |
| XML | 16 |
| JSP | 6 |
使用lint插件
lint是一类工具的统称,即在ide编码时实时帮你检查编码规范的软件。我推荐的有Sonarlint和Alibaba java coding guidelines(java only)这两款插件。在intellij系列的ide上用起来都不错。lint软件比ide自带的代码风格检查更严格,而且会给问题分严重等级。
lint插件会highlight问题代码,鼠标悬浮可以查看问题概述。

点show inspection description可以查看详情

自动格式化
在intellij下手动格式化的操作是 Code -> Reformat Code

在一个项目中,如果有的人格式化代码,有的人不格式化的后果就是git提交后,平白无故会多出很多difference,其实都是格式上的微调,但是会给代码review带来不必要的负担

所以最好大家代码都是格式化的。可以在commit的时候勾选先格式化再提交

也可以设置在保存文件时自动触发格式化操作
intellij preferences -> save actions -> 开启保存触发动作 -> 选择格式化动作

遵守业界规范
坚持高的标准,统一使用业界标杆企业的编码规范,如
Google Style Guides
Alibaba java开发手册



Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
At his momentt I amm goingg away too doo my breakfast,
oncde hasving mmy breeakfast coming oover agaain tto reawd more news.
my blkog – iporn.win (Joseph)
Aw, ths wass ann extremely nicxe post. Takiing thhe timee andd actul effort tto genedrate a great
article… butt wha can I say… I procrastinate a lott aand nefer seeem tto geet
nnearly anyyhing done.
Allso visiot myy homepage – roloxxx.com
We’re a grohp oof voluntsers andd opning a neww schesme iin oour community.
Yoour weeb site provided us wkth vaqluable innfo too
wprk on. Yoou have one an impresive joob aand our
wuole community will bee hankful tto you.
Allso vusit my wweb blog :: pornoworld.info