我们什么时候应该在源代码中插入空白行? [英] When should we insert blank line(s) in source code?

查看:47
本文介绍了我们什么时候应该在源代码中插入空白行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定应该在源代码中的哪里添加新的空白行.我想写出既易于阅读又易于理解的漂亮代码.

I'm not sure where I should add new blank line(s) in my source code. I would like to write beautiful code that is both easy to read and understand.

是仅基于对象还是基于概念?举例说明对我来说最有帮助.

Is it object-based only, or concept-based? Answers with examples would be most helpful to me.

推荐答案

我个人不使用太多垂直空格-其他人会推荐比我在下面描述的更多的空格.只要分开的东西之间有某种视觉上的分隔,我就想看代码,而不是空白行.我倾向于:

Personally I don't use much vertical whitespace - other people will recommend more than I describe below. As long as there's some kind of visual separation between separate things, I want to look at code, not blank lines. I tend to put:

  • 非成员函数之间(以及在C ++中,在类外部定义的成员函数之间)的空白行
  • 类之间的单个空白行.
  • 与后面的多个函数/类相关的注释前后的空白行,即在代码中定义了节":用于XML处理的辅助函数",
  • 类中成员函数之间没有空白行
  • 函数内部通常没有空行.如果一个函数处于多个阶段,则可以在它们之间插入空白行,或者仅在它们之间添加注释以解释这些阶段.很长的函数很可能以这种方式与空格分开,而不是我认为很长的函数特别好.如果某个成员函数确实具有内部空白行,那么我通常也将其与该类中其他成员函数的空白行分开.

两个连续的空白行在我看来有些可疑,而我自己从来没有这样做过.三只浪费了我显示器上的完美空间-谁想要将IMO分开的东西放到另一个文件中.

Two consecutive blank lines look a bit suspect to me, and I hardly ever do that myself. Three is just wasting perfectly good space on my monitor - anyone who wants things that separate should IMO put them in different files.

用于自动文档编制的注释块(Javadoc,Doxygen,Pydoc等)还提供了视觉上的分隔-比几行空白更多,并且比任何数量的空白更有生产力.

Comment blocks for auto-documentation (Javadoc, Doxygen, Pydoc and the like) also provide visual separation - more so than a few lines of whitespace, and more productively than any amount of whitespace.

在团队中工作时,请复制您修改的任何文件的样式.不要重新格式化它以符合您自己的喜好,不要以您喜欢的格式添加新代码.要么同意一种家庭风格,要么就可以与之共存.

When you're working in a team, copy the style of any file you modify. Don't reformat it to match your own preferences, don't add new code with your preferred formatting. Either agree a house style or just live with it.

这篇关于我们什么时候应该在源代码中插入空白行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆