如何避免浪费屏幕空间编写稀疏的 C# 代码?

发布于 2024-09-16 09:41:14 字数 1296 浏览 7 评论 0原文

普遍接受的格式化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

桜花祭 2024-09-23 09:41:15

我倾向于编写小类、短方法和小级别的嵌套。您不仅可以看到屏幕上的所有内容,而且代码也更好!

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!

决绝 2024-09-23 09:41:15

我有一个 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.

蓝梦月影 2024-09-23 09:41:15

如果我要尝试/捕捉某些东西,我会与任何相邻的 using 块结合起来。同样,我会聚合命名空间,所以更接近:

namespace SomeNamespace.SomeSubNamespace
{
    class SomeClass
    {
        void SomeFunction()
        {
            StreamWriter someFile;
            try
            {
                someFile = new StreamWriter(somePath);

                lock(someCriticalSection)
                {
                    using (var someDisposableThing1 = new DisposableThing())
                    {
                        DoSomething();                            
                        using (var someDisposableThing2 = new DisposableThing())
                        {
                            lock(someOtherCriticalSection)
                            {
                                DoSomethingMore();
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                Log(e);
            }
            finally
            {
                if( someFile != null )
                {
                     someFile.Dispose();
                }
            }
        }
    }
}

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:

namespace SomeNamespace.SomeSubNamespace
{
    class SomeClass
    {
        void SomeFunction()
        {
            StreamWriter someFile;
            try
            {
                someFile = new StreamWriter(somePath);

                lock(someCriticalSection)
                {
                    using (var someDisposableThing1 = new DisposableThing())
                    {
                        DoSomething();                            
                        using (var someDisposableThing2 = new DisposableThing())
                        {
                            lock(someOtherCriticalSection)
                            {
                                DoSomethingMore();
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                Log(e);
            }
            finally
            {
                if( someFile != null )
                {
                     someFile.Dispose();
                }
            }
        }
    }
}
灼疼热情 2024-09-23 09:41:15

也不认为它看起来那么糟糕;)
然而,制作更短的函数确实是一个重点。 的操作,那就太好了

if (condition)
{
   // bla
}
else
{
  // blub
}

特别是,如果您可以执行诸如替换之类

void someFunction()
{
  if (condition)
  {
    // bla
    return
  }
  // blub
}

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

if (condition)
{
   // bla
}
else
{
  // blub
}

by

void someFunction()
{
  if (condition)
  {
    // bla
    return
  }
  // blub
}
第几種人 2024-09-23 09:41:15

using 语句非常方便,但如果代码变得难以阅读,您可以不使用它:

namespace SomeNamespace.SomeSubNamespace
{
    class SomeClass
    {
        void SomeFunction()
        {
            StreamWriter someFile = new StreamWriter(somePath);
            DisposableThing someDisposableThing1 = null;
            DisposableThing someDisposableThing2 = null;

            try
            {
                lock (someCriticalSection)
                {
                    someDisposableThing1 = new DisposableThing();
                    DoSomething();
                    someDisposableThing2 = new DisposableThing();
                    lock (someOtherCriticalSection)
                    {
                        DoSomethingMore();
                    }
                }
            }
            catch (Exception e)
            {
                Log(e);
            }
            finally
            {
                // requires SafeDispose extension method
                someFile.SafeDispose();
                someDisposableThing1.SafeDispose();
                someDisposableThing2.SafeDispose();
            }
        }
    }
}

The using statement is very handy, but if code becomes harder to read you can do without it:

namespace SomeNamespace.SomeSubNamespace
{
    class SomeClass
    {
        void SomeFunction()
        {
            StreamWriter someFile = new StreamWriter(somePath);
            DisposableThing someDisposableThing1 = null;
            DisposableThing someDisposableThing2 = null;

            try
            {
                lock (someCriticalSection)
                {
                    someDisposableThing1 = new DisposableThing();
                    DoSomething();
                    someDisposableThing2 = new DisposableThing();
                    lock (someOtherCriticalSection)
                    {
                        DoSomethingMore();
                    }
                }
            }
            catch (Exception e)
            {
                Log(e);
            }
            finally
            {
                // requires SafeDispose extension method
                someFile.SafeDispose();
                someDisposableThing1.SafeDispose();
                someDisposableThing2.SafeDispose();
            }
        }
    }
}
拍不死你 2024-09-23 09:41:15

我在代码块的开头行看到了开括号,如下所示:

namespace SomeNamespace { 
    class SomeClass {
        void SomeFunction() {
            using (var someFile = new StreamWriter(somePath)) {
                try {
                    lock(someCriticalSection) {
                        using (var someDisposableThing1 = new DisposableThing()) {
                            DoSomething();                             
                        }
                    }
                } catch(Exception e) { 
                    Log(e); 
                } 
            }
        } 
    } 
} 

更少的空间,但我认为它很难看。

I've seen open brackets on the beginning line of a code block as in:

namespace SomeNamespace { 
    class SomeClass {
        void SomeFunction() {
            using (var someFile = new StreamWriter(somePath)) {
                try {
                    lock(someCriticalSection) {
                        using (var someDisposableThing1 = new DisposableThing()) {
                            DoSomething();                             
                        }
                    }
                } catch(Exception e) { 
                    Log(e); 
                } 
            }
        } 
    } 
} 

Less real estate, but I think its ugly.

残龙傲雪 2024-09-23 09:41:15

我同意其他答案,我基本上接受它——对于我的团队来说,我们要么有高分辨率显示器,要么有双显示器(或两者都有),所以我们不像在 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.

爱的故事 2024-09-23 09:41:15

使用 CodeRush
它会让你的代码导航变得更好

你会得到这样的东西
替代文字

use CodeRush ,
it'll make navigation through out your code much better

you'll get something like this
alt text

天涯沦落人 2024-09-23 09:41:15

除了其他人在这里所说的之外,我为减轻影响所做的一件事是使用比例间距字体。文本更易读,但宽度不够。它还迫使你使用制表符而不是空格来对齐(当然,这就是上帝首先给我们制表符的原因)

我个人使用 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....

残龙傲雪 2024-09-23 09:41:14
  1. 我不经常看到以这种方式嵌套的命名空间 - 也许它会发生,而不是我工作的地方
  2. 和我生活的地方。并且大多数情况下不要编写如此深层嵌套的代码。拥有更短的方法或更少嵌套的方法肯定会减少水平空白问题,并且将方法分解成更小的部分意味着您可以在给定的垂直空间中获得更重要的信息。
  1. I don't often see namespaces nested in that way - maybe it happens, not where I work
  2. I live with it. And mostly don't write such deeply-nested code. Having shorter methods, or methods with less nesting certainly reduces the horizontal whitespace problem, and having methods broken up into smaller pieces means you can get more important information in a given amount of vertical space.
若无相欠,怎会相见 2024-09-23 09:41:14

让我这样解释一下。如果您确实碰巧遇到了与上面显示的一样深的嵌套代码构造,那么问题可能不是格式而是您的代码结构。

您应该考虑看看这个: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

素衣风尘叹 2024-09-23 09:41:14

好吧,我买了一台高清显示器;)

但是除了使用更少的缩进之外,处理它的唯一方法是将一些代码移动到他们自己的方法中。

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.

淡水深流 2024-09-23 09:41:14

工具>选项>文本编辑器>所有语言>标签>缩进尺寸> 1
这不是一个很好的解决方案...:P

Tools > Options > Text Editor > All Languages > Tabs > Indent size > 1
Not a great solution... :P

小…楫夜泊 2024-09-23 09:41:14

您应该阅读 Bob Martin 的简洁代码。它有助于解决这个问题。在本例中,SomeFunction() 执行不止一件事。将它所做的每件事分解为它自己的方法。

 namespace SomeNamespace.SomeSubNamespace
 {
    class SomeClass
    {
        void WriteToFile()
        {
            using (var someFile = new StreamWriter(somePath))
            {
                lock(someCriticalSection)
                {
                   ExecuteCriticalSection();
                }
            }
        }
        void ExecuteCriticalSection()
        {
            using (var someDisposableThing1 = new DisposableThing())
            {
            DoSomething();
            ExecuteFinalCriticalSection();
            }   
        }

        void ExecuteFinalCriticalSection()
        {
            using (var someDisposableThing2 = new DisposableThing())
            {
            lock(someOtherCriticalSection)
                {
                DoSomethingMore();
                }
            }
        }
    }
}

另外,不要捕获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.

 namespace SomeNamespace.SomeSubNamespace
 {
    class SomeClass
    {
        void WriteToFile()
        {
            using (var someFile = new StreamWriter(somePath))
            {
                lock(someCriticalSection)
                {
                   ExecuteCriticalSection();
                }
            }
        }
        void ExecuteCriticalSection()
        {
            using (var someDisposableThing1 = new DisposableThing())
            {
            DoSomething();
            ExecuteFinalCriticalSection();
            }   
        }

        void ExecuteFinalCriticalSection()
        {
            using (var someDisposableThing2 = new DisposableThing())
            {
            lock(someOtherCriticalSection)
                {
                DoSomethingMore();
                }
            }
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文