iOS Developer程序员代码规范 编码习惯

用 Clang-Format 统一团队代码规范(with spa

2017-06-01  本文已影响838人  zvving
Tabs vs Spaces [来源](https://warosu.org/g/thread/55912264)

团队开发中,期望 Code Review 来约束的是代码结构和潜在逻辑问题,这时代码规范这种『细节』就给 review 过程带来不小的噪音。
特别是团队目前的情况:历史负担较重,大量经几手的旧代码风格不一致,团队成员新组建,对代码规范、好的范式还没达成默契。新写代码的规范尚在磨合中,遑论手动改写大量的旧代码。

这是重要不紧急的问题,最近项目略有空闲,是对这类问题下手的好时机;)

目标

破窗效应
『如果那些窗不被修理好,可能将会有破坏者破坏更多的窗户。...又或想像一条人行道有些许纸屑,如果无人清理,不久后就会有更多垃圾,最终人们会视为理所当然地将垃圾顺手丢弃在地上。』
——维基百科

一些调研

本以为不会太复杂,实际执行起来发现问题也不少:

看了一圈,最终决定选择 square 家的方案:https://github.com/square/spacecommander

spacecommander = clang-format + ObjC 定制规范 + git 工作流

spacecommander 在 clang-format 基础上加入几条定制规范,解决了 block 缩进问题,对 ObjC 更友好。内置脚本,很方便的添加 git pre-commit hook,format 代码也算方便,不影响原有工作流。源代码很少,一些 shell 和 python,很容易看懂。
这些跟团队需要很契合,项目有些老,有些小问题,缝缝补补能用就好。

如何执行

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree commit -q -F /var/folders/7w/5_0xvl794n960s_wsr3h68340000gn/T/SourceTreeTemp.GSJLdQ 
🚸 Format and stage individual files:
"/Users/zengming/ming-dev/gg-projects/spacecommander"/format-objc-file.sh 'ios-common/GGLib/GGBase/GGModels/GGHotelAPIModel.m' && git add 'ios-common/GGLib/GGBase/GGModels/GGHotelAPIModel.m';

🚀  Format and stage all affected files:
     "/Users/zengming/ming-dev/gg-projects/spacecommander"/format-objc-files.sh -s

🔴  There were formatting issues with this commit, run the👆 above👆 command to fix.
💔  Commit anyway and skip this check by running git commit --no-verify
Completed with errors, see above

spacecommander 的原理是定制好 .clang-format,执行 commit 前运行 format-objc-file-dry-run 检查当前要提交的文件,如果不符合规范,则提示修改。(默认只检查已修改并 Stage 的文件)
如果要格式化项目所有文件,可以尝试在项目中运行 /path/to/spacecommander/format-objc-files-in-repo

一些细节问题

Block 格式化不符合预期?

参见这里:https://bugs.llvm.org//show_bug.cgi?id=23585

clang-format: Support nested block formatting with ColumnLimit=0.

改到可用,这点还不大符合我的预期。谁有更好的方法吗?

// 格式化后的效果
[UIAlertView bk_showAlertViewWithTitle:@"温馨提示"
                               message:push.alert
                     cancelButtonTitle:@"忽略"
                     otherButtonTitles:@[ @"查看" ]
                               handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                                   if (buttonIndex == 0) {
                                       return;
                                   }
                                   ...
                               }];

// 我期望的效果
[UIAlertView bk_showAlertViewWithTitle:@"温馨提示"
                               message:push.alert
                     cancelButtonTitle:@"忽略"
                     otherButtonTitles:@[ @"查看" ]
                               handler:^(UIAlertView *alertView, NSInteger buttonIndex)
{
    if (buttonIndex == 0) {
        return;
    }
    ...

}];

直接使用 .clang-format ?

最开始我也是直接配 .clang-format,总有些配置不合预期,改起来调试也不是那么方便。spacecommander 的基础配置是一个能看的配置,推荐在它的基础上改,何况 git hook 都写好了何必自己折腾。

.clang-format 全部配置细节参见这里:https://clang.llvm.org/docs/ClangFormatStyleOptions.html

spacecommander 的一些小问题

总结

上一篇 下一篇

猜你喜欢

热点阅读