如何避免浪费屏幕空间编写稀疏的 C# 代码?
普遍接受的格式化 C# 代码的方式似乎如下:
namespace SomeNamespace
{
namespace SomeSubNamespace
{
class SomeClass
{
void SomeFunction()
{
using (var someFile = new StreamWriter(somePath))
{
try
{
lock(someCriticalSection)
{
using (var someDisposableThing1 = new DisposableThing())
{
DoSomething();
using (var someDisposableThing2 = new DisposableThing())
{
lock(someOtherCriticalSection)
{
DoSomethingMore();
}
}
}
}
}
catch(Exception e)
{
Log(e);
}
}
}
}
}
这浪费了大量的水平和垂直屏幕空间。我肯定不是第一个注意到的人。我的问题是:您是否接受它,或者您是否开发了不同的格式样式以避免过多的空白?
PS:请注意,我什至还没有使用单个 if 语句!
The commonly accepted way to format C# code seems to be as follows:
namespace SomeNamespace
{
namespace SomeSubNamespace
{
class SomeClass
{
void SomeFunction()
{
using (var someFile = new StreamWriter(somePath))
{
try
{
lock(someCriticalSection)
{
using (var someDisposableThing1 = new DisposableThing())
{
DoSomething();
using (var someDisposableThing2 = new DisposableThing())
{
lock(someOtherCriticalSection)
{
DoSomethingMore();
}
}
}
}
}
catch(Exception e)
{
Log(e);
}
}
}
}
}
This wastes a large amount of screen space, both horizontally and vertically. I'm surely not the first person who notices. My question is: do you live with it, or have you developed a different formatting style to avoid an excess of white space?
PS: Note that I didn't even use a single if statement yet!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我倾向于编写小类、短方法和小级别的嵌套。您不仅可以看到屏幕上的所有内容,而且代码也更好!
I tend to write small classes, short methods with small level of nesting. Not only you can see everything on your screen, also the code is better!
我有一个 24 英寸宽屏显示器。比起回收空间,我更喜欢“漂亮的打印”。压缩的、挤在一起的代码很难阅读,而且会伤害我的大脑。
I have a 24" widescreen monitor. I greatly prefer "pretty-printing" over reclaiming space. Condensed, crammed-together code is hard to read and hurts my brain.
如果我要尝试/捕捉某些东西,我会与任何相邻的 using 块结合起来。同样,我会聚合命名空间,所以更接近:
If I'm going to try/catch something, I would combine with any adjacent using blocks. Similarly, I would aggregate namespaces, so something closer to:
也不认为它看起来那么糟糕;)
然而,制作更短的函数确实是一个重点。 的操作,那就太好了
特别是,如果您可以执行诸如替换之类
Don't think it looks so bad either ;)
Making shorter functions however really is a point. In particular, it's nice if you can do stuff like replacing
by
using 语句非常方便,但如果代码变得难以阅读,您可以不使用它:
The using statement is very handy, but if code becomes harder to read you can do without it:
我在代码块的开头行看到了开括号,如下所示:
更少的空间,但我认为它很难看。
I've seen open brackets on the beginning line of a code block as in:
Less real estate, but I think its ugly.
我同意其他答案,我基本上接受它——对于我的团队来说,我们要么有高分辨率显示器,要么有双显示器(或两者都有),所以我们不像在 800x600 上开发。
但要记住的一件事是,如果使用实际间距,制表符会更好,因为有些开发人员使用 2 个空格作为制表符,有些是 4 个,有些是 6 个。因此,虽然格式化代码在某个开发人员的 IDE 上可能看起来不错,但穿在别人身上可能不太好看。 (我已经看到了一些可怕的结果。)
VS2010 有一个来自扩展管理器的很好的插件,称为生产力工具,它可以检测文件中何时混合有制表符和空格,并允许您将所有内容转换为制表符或空格(“修复混合选项卡”)。它确实有助于清理您的文件(即使它看起来没有任何作用!)。如果您在团队之间共享代码,那就太好了。
I agree with the other answers that I basically live with it -- for my team we either have high-res monitors or dual monitors (or both), so it's not like we're developing on 800x600.
But one thing to keep in mind is that your tabbing would be better off if you used actual spacing, as some developers use 2 spaces for a tab, some 4, some 6. So while formatted code may look fine on one developer's IDE, it may not look so good on another's. (I've seen some horrible outcomes from that.)
VS2010 has a nice plug-in from the Extensions Manager called the Productivity Tools that detects when you have a mix of tabs and spaces in your file and allows you to convert everything to tabs or spaces ("Fix Mixed Tabs"). It really helps to clean up your file (even if it doesn't look like it does anything!). It's great if you're sharing code across teams.
使用 CodeRush ,
它会让你的代码导航变得更好
你会得到这样的东西
use CodeRush ,
it'll make navigation through out your code much better
you'll get something like this
除了其他人在这里所说的之外,我为减轻影响所做的一件事是使用比例间距字体。文本更易读,但宽度不够。它还迫使你使用制表符而不是空格来对齐(当然,这就是上帝首先给我们制表符的原因)
我个人使用 Comics Sans MS,但这确实让同事们抓狂......
In addition to what others have said here, the one thing I do to lessen the effect, is to use a proportional-spaced font. The text is more readable, but not nearly as wide. It also forces you to use tabs over spaces for alignment (which is of course, why the good Lord gave us tabs in the first place)
I personally use Comics Sans MS, but that really drives coworkers crazy....
让我这样解释一下。如果您确实碰巧遇到了与上面显示的一样深的嵌套代码构造,那么问题可能不是格式而是您的代码结构。
您应该考虑看看这个:Martin Fowler 的重构
提取方法您改进的不仅仅是“浪费”的屏幕空间,但也显着提高了可读性;)
另一种解决方案始终是:在 Visual Studio 中 Alt + Shift + Enter 进入全屏模式
Let me explain it like this. If you really happen to encounter a nested code construct as deep as the one you show above, then the problem is probably not the formatting but your code structure.
You should consider taking a look at this: Refactoring by Martin Fowler
By extracting methods you improve not only the "wasted" screen space, but you dramatically increase readability too ;)
Another solution is always: Alt + Shift + Enter in Visual Studio to go into Full Screen mode
好吧,我买了一台高清显示器;)
但是除了使用更少的缩进之外,处理它的唯一方法是将一些代码移动到他们自己的方法中。
Well I bought an HD monitor ;)
But the only way to deal with it apart from using less indenting would be to move some code into their own methods.
工具>选项>文本编辑器>所有语言>标签>缩进尺寸> 1
这不是一个很好的解决方案...:P
Tools > Options > Text Editor > All Languages > Tabs > Indent size > 1
Not a great solution... :P
您应该阅读 Bob Martin 的简洁代码。它有助于解决这个问题。在本例中,
SomeFunction()
执行不止一件事。将它所做的每件事分解为它自己的方法。另外,不要捕获
System.Exception
;你永远不知道什么会失败,并且你不应该尝试捕获你无法处理的异常。让它们冒泡然后惨死。You should read Bob Martin's Clean Code. It helps with this problem. In this instance,
SomeFunction()
does more than one thing. Split out each thing it does in its own method.Also, don't catch a
System.Exception
; you never know what's going to fail, and you shouldn't try to catch exceptions you can't handle. Let them bubble up and die horribly.