Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid wasting screen space writing sparse C# code?

Tags:

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!

like image 983
Dimitri C. Avatar asked Aug 27 '10 13:08

Dimitri C.


1 Answers

  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.
like image 69
Blair Conrad Avatar answered Oct 09 '22 02:10

Blair Conrad