什么是格式化代码的正确方法? [英] What is the proper way to format code?

查看:149
本文介绍了什么是格式化代码的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写代码时,我尝试将类似代码的行分组在一起,然后留下一个空行并写入另一个块。



我相信这有助于整洁和可读性的代码。

我不是一个没有任何行间距聚在一起的大风扇。它看起来像地狱,很难阅读,很难跟踪。

我有一个老师,降低了我的任务之一,因为我已经在逻辑上间隔我的代码。他说:当你必须在现实世界中整天阅读代码时,你不会把这个行间隔放在里面,你会感谢我。当然,我从来没有,也从来不会感谢他。 >

现在我已经处于真实的世界了,我看到绝大多数没有行间距的代码文件写得很差,思路也很糟糕。



在VB类型的语言中,这可能比在C类型的语言中更为重要,但是同样的概念也是适用的。




  • 您的代码在哪里留空行

  • 行间距也是多少很多?


解决方案

我遵循


When I write code, I try to group lines of similar code together, then leave a blank line and write another block.

I believe this contributes to the neatness and readability of the code.

I'm not a big fan of bunching stuff together without any line spacing. It looks like hell, it's hard to read and it's difficult to follow.

One of the teachers I had, downgraded one of my assignments because I had spaced my code logically. He said, 'When you have to read code all day in the real world, you won't put this line spacing in and you'll be thanking me." Of course, I never did and never will thank him.

Now that I'm in the real world, most of the code files I see that have absolutely no line spacing are poorly written and poorly thought out.

This is probably more prevelant in VB type languages than in C type languages, but the same concept applies.

Two questions come to mind:

  • Where do you leave a blank line in your code?
  • How much line spacing is too much?

解决方案

I follow Microsoft's Guidelines for C#.

Edit: The standard for C# is Don't fight the IDE. If you hit CTRL K+D, the IDE will automatically put blank lines in between code sections.

To follow that up, if you look at C# sample code on MSDN or anywhere else, there's normally a blank line in between each logically placed group. So There will be a blank line after all your member variables, a blank line after each method, etc.

In response to the comments expressing shock and horror that I use an IDE for C# programming:


REAL PROGRAMMERS

这篇关于什么是格式化代码的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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