免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 42530 | 回复: 74
打印 上一主题 下一主题

缩进用tab还是空格 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-18 15:38 |只看该作者 |倒序浏览
本帖最后由 wwwsq 于 2011-07-20 09:09 编辑

我的观点是只用tab缩进,理由是这样写代码的时候跳转比较快。

使用空格缩进的理由是tab的宽度有些地方是4有些地方是8。这个理由不成立。
1,vim、eclipse、vs、ue之类的编辑器都可以设置tab的宽度,都设置成4应该没问题。
2,用空格做缩进,很容易导致代码对齐错误,比如多缩进一个空格,少缩进一个空格之类的。
3,用空格做缩进,当你修改一个函数的时候,你需要按四下箭头才能从行首移动到代码的第一个字母。
4,用空格做缩进,最后很容易变成缩进一个或者两个空格。
5,那些喜欢连续四个空格的人,我不知道他们已经多久没有亲手写过代码了。

以上。

论坛徽章:
0
2 [报告]
发表于 2011-07-18 15:42 |只看该作者
没听明白,我按一下tab默认生成4个' ',而不是一个'\t',你说的tab是什么

论坛徽章:
0
3 [报告]
发表于 2011-07-18 15:45 |只看该作者
没听明白,我按一下tab默认生成4个' ',而不是一个'\t',你说的tab是什么
bs 发表于 2011-07-18 15:42


'\t'才是tab。
按一下tab生成四个空格,那还是四个空格,只是用tab按键来做快捷键。

论坛徽章:
0
4 [报告]
发表于 2011-07-18 15:46 |只看该作者
以下Linux Kernel Coding Style

                 Chapter 1: Indentation

Tabs are 8 characters, and thus indentations are also 8 characters.
There are heretic movements that try to make indentations 4 (or even 2!)
characters deep, and that is akin to trying to define the value of PI to
be 3.

Rationale: The whole idea behind indentation is to clearly define where
a block of control starts and ends.  Especially when you've been looking
at your screen for 20 straight hours, you'll find it a lot easier to see
how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen.  The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.

In short, 8-char indents make things easier to read, and have the added
benefit of warning you when you're nesting your functions too deep.
Heed that warning.

论坛徽章:
0
5 [报告]
发表于 2011-07-18 15:55 |只看该作者
以下Linux Kernel Coding Style

                 Chapter 1: Indentation

Tabs are 8 characters, and thus indent ...
狗气球 发表于 2011-07-18 15:46



写内核的人和写应用系统的人,面临的是不一样的场景。

他们是用C和汇编写那些很少变化的逻辑,写个东西动不动几个月。

而写应用的那些人,需要用C++, Java, Python, PHP, C#这些不同的语言去写三天两头变化的逻辑,而且常常要三天就写完代码自测完成。

所以,linux kernel的代码风格,有些可以借鉴,有些却需要区别对待。

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
6 [报告]
发表于 2011-07-18 15:57 |只看该作者
我用Tab,4个空

论坛徽章:
0
7 [报告]
发表于 2011-07-18 16:07 |只看该作者
本帖最后由 greensnow 于 2011-07-18 16:10 编辑

同样用tab, 自动expand成4个空格,
看代码, 第一件事是indent过滤一遍...
用vim的, 删除4个空格可以用ctrl-d,不需要连续4次BS

论坛徽章:
0
8 [报告]
发表于 2011-07-18 16:40 |只看该作者
回复 5# wwwsq


    我用的编辑器是自动expand到4个空格的。输入的时候一样是tab键。删除的话自然也有快捷键。不需要一个一个backspace。

论坛徽章:
0
9 [报告]
发表于 2011-07-18 17:10 |只看该作者
只在python中用四个空格, 其它都是硬TAB

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
10 [报告]
发表于 2011-07-18 17:19 |只看该作者
萝卜,青菜各有所爱。
况且萝卜、青菜可以相互替代。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP