8

After getting back into Python, I'm starting to notice and be annoyed more and more by my C# coding style requiring braces everywhere

 if (...)
 {
     return ...;
 }
 else
 {
     return ...;
 }

preferring the (subjective) much cleaner looking python counter-part

if ...:
    return ...
else
    return ...

is there any way I can go about hiding these braces (as they do take up about 30% of my coding screen on average and just look ugly!)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe
  • 11,147
  • 7
  • 49
  • 60
  • What program are you using to edit your code? – Cameron Jan 30 '11 at 23:40
  • 6
    Just omit them. For single statements you don't need them in C#. – Rauhotz Jan 30 '11 at 23:40
  • 2
    I think this is an unfortunate consequence of using C#. You could try same-line curly braces which looks a lot better but that's not the community standard for C# :( – ide Jan 30 '11 at 23:41
  • +1 yeah, awesome! didn't know i could do it with the else and else if as well, thx – Joe Jan 30 '11 at 23:42
  • 1
    One possibility is to set the editor settings to give { } a very bright color (or dark if you're using a dark background) – sinelaw Jan 30 '11 at 23:42
  • @sinelaw thought about that but then i'd have *ALOT* more white spaces and it would make the code even less readable – Joe Jan 30 '11 at 23:43
  • 2
    Just be careful - http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces – ide Jan 30 '11 at 23:43
  • how's that going to reduce the fact that they take 30% of the coding screen? they'll still take 30% of the screen and be an eye sore. in fact 30% of a bigger screen's gonna be a BIGGER eye sore! i should downvote ur comment hehe – Joe Mar 15 '11 at 02:34

10 Answers10

10

Sorry, but if you're coding in C# and doing more than just simple single-expression blocks, you're going to have to suck it up. Python's "indent-denotes-scope" grammar may be nice, but it's Python, not C#.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
5

You could switch to:

 if (...) {
     return ...;
 } else {
     return ...;
 }

to gain some screen.

I think it's a bad idea to pretend that C# works like Python though.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • alas the screen space is only half the problem, if i could hide them altogether it would look better. i actually switched to having the { on one line to improve readability. – Joe Jan 30 '11 at 23:44
  • @Joe I don't think "it would look better" is enough reason to do this. If you're going to be using C# a lot you'll do a lot better to get used to the syntax. – Skilldrick Jan 30 '11 at 23:46
  • oh believe me i'm used to the syntax, but it's just that i had more or less a wow moment as i was working on my pet project in python on the weekend and when i came into work this morning i couldn't help but notice all the braces that i'd had written over the years... and thought wouldn't it be coooool if i could hide it – Joe Jan 30 '11 at 23:52
2
if (condition) {
    statement1
    statement2; }
else {
    statement3;
    statemen4; }

much uglier in my opinion :(

Marlon
  • 19,924
  • 12
  • 70
  • 101
1

If you're feeling really evil:

if (iAmEvil)                                                                                                             {
    LookMa();
    NoBraces();
    OrSemiColonsForThatMatter()                                                                                         ;}
else {
    IfYouDontGetIt();
    ScrollRight();
}

Of course, it's probably more trouble than it's worth.

YellPika
  • 2,872
  • 2
  • 28
  • 31
  • i was wondering how this was possible, copy paste into linqpad and saw the 'hidden' text. Not sure if this is appropriate as an answer, tempted to downvote (from a noise point of view). – Joe Sep 23 '13 at 04:31
  • that sounded harsher than intended, it was funny :) not downvoting... but still not sure if it should be an answer... O_O – Joe Sep 23 '13 at 04:34
  • Well, http://stackoverflow.com/help/privileges/vote-down states that downvotes are for "an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect". My 'solution' *does* get the job done (hides braces as well as semicolons), and certainly took a bit of effort on my part to write out all those spaces. By those standards I'm pretty sure I've made a perfectly valid answer. If you want to downvote, then go ahead - the humor was worth the price :) – YellPika Sep 23 '13 at 05:24
1

If you need .NET but like Python syntax, you might like to check Boo.

  • +1 i've had a read, whilst boo might not meet my simplistic needs of hiding curly braces, it might eventually satisfy my desire of writing like python in .net... but isn't that what IronPython does? ..googling... apparently there's some differences there too – Joe Jan 31 '11 at 06:38
0

Well, you can omit the braces in C#, so long as the if and else "blocks" are just single statements:

// perfectly legal
if (...)
    return ...
else
    return ...

If you need multi-line if and else blocks then you're stuck with braces.

LukeH
  • 263,068
  • 57
  • 365
  • 409
0
  1. You don't need the brackets if you have only one statement. For example, the following is working:

    for (int i = 0; i < 10; i++)
        if (i == 9)
            break;
    

(even though that code does not make any sense)

  1. You can change the settings in Visual C# for formatting C# code as Skilldrick says, to make the code shorter:

Go to Tools -> Options -> Text Editor -> C# -> formatting (or something similar, I only have the german version of visual studio).

Hope that helps

Sören
  • 2,661
  • 2
  • 19
  • 22
0

C/C++/Java/C# folks have an ages old racist kind of family feud with the Cobol/BASIC/Python clan. It's a real either/or thing... No best-of-the-both-world's, having-cakes-and-eating-them-too fun allowed :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
explorer
  • 11,710
  • 5
  • 32
  • 39
-1

For single statements you do not need the curly braces. For times you need them you might be OK with placing the braces like:

if(expr) {
    ...
} else {
    ...
}
Pekkasso
  • 419
  • 2
  • 8
-2

Check out scribes, its a really nice editor that is customizable to a large extent, and I'm quite sure that you can do what you needed with it.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • scribes is not even for windows, and how is it going to do what i need with it? – Joe Jan 31 '11 at 02:28