nobraces: Python indentation for C
September 2007
When programmers first run into Python and discover its indentation rules, they often “recoil in reflexive disgust”, like Eric Raymond did. I exhibited something more like “extreme wariness”, but close enough.
Yet then I realised that it’s not actually “significant whitespace” — merely meaningful indentation. It’s just the language making use of what everyone does anyway, leading to shorter and less {noisy;}
code.
Anyway, I still use C quite a bit for embedded programming contracts, and Tim Hatch’s pybraces inspired me to write an analogue for C that gives you Python-style indentation for C.
Imagine coding C without these icky old braces (K&R brace style):
void push(int x) { if (stack_idx >= STACK_SIZE-1) { printf("Stack overflow!\n", STACK_SIZE); exit(1); } stack[++stack_idx] = x; }
… instead write beautiful, braceless code like this:
void push(int x): if (stack_idx >= STACK_SIZE-1): printf("Stack overflow!\n", STACK_SIZE) exit(1) stack[++stack_idx] = x
No more wars over which is The One True Brace Style — nobraces gets rid of the problem completely, not to mention helps you avoid a number a common bugs.
So grab the source, nobraces.ci, update your makefiles, and you’re away. And yes, it really is written in itself. I’ve left bootstrapping it as an exercise for the reader. :-)
Okay, okay, it’s a joke. Mostly. If someone made it handle continuation lines and comments a bit more robustly, it might actually be quite nice. I think I’d use it.
In fact, I really do think I’d use it. So I’ve started a nobraces project on microPledge and pledged $25. Feel free to add your own pledge (click the widget on the right) or sign up to develop it and run away with the riches and glory.
Comments
w-g 25 Sep 2007, 22:33
So long as you’ll make it emit correct error messages (with correct line numbers) and work with gdb… (I want to be able to tell exactly in which line a problem is).
Orbiscerbus 25 Sep 2007, 22:43
I always thought the beauty of the code is in it’s problem solution elegance, and not in the physical manifestation. So, few braces here and there, who cares, so long as the algorithm is okay and solves the problem effectively and correctly.
Ben 25 Sep 2007, 22:50
Orbiscerbus, that’s partly true — elegance of algorithm is pretty important. But is COBOL really as beautiful as, say, Python? (Perhaps that’s comparing apples with oranges, but I wanted the point to be clear.)
she 25 Sep 2007, 23:46
“I always thought the beauty of the code is in it’s problem solution elegance, and not in the physical manifestation. “
I dont think so … no matter how smart perl algorithm is, i hated it for its overly verbose line noise. I actually dont think the {} are so bad, what I hete in C are the ; (and also structs accessing -> )
Paul 25 Sep 2007, 23:53
I’d be more interested in a way to add braces to Python, actually, as well as Ruby (get rid of that dangling “end”). Vastly easier than lobotomizing C!
Haskell at least has an alternate syntax with {} and ;. It’s not complete enough to be usable, unfortunately, but it does make code much more readable.
Ben 25 Sep 2007, 23:59
Paul, Tim Hatch has already added braces to Python, as mentioned above. But you’re not really serious, are you? It wouldn’t be Python with braces! (Though I suppose you could say “it wouldn’t be C without them”. :-)
Andrew 26 Sep 2007, 00:15
That’s beautiful. It’ll never be accepted by my fascist build master in a million years, though. I mean, by his successor in a million years’ time. And our successors will still be limited to 80 columns in their C source even though our monitors will still be more wide than tall. And having to put braces around single statements in ‘if’s because some idiot might still not have learned C after a million years, and might accidentally forget to put braces in when they add another statement, even though it would look completely wrong. Sigh. But anyway, I love it.
magnus 26 Sep 2007, 00:16
ONE WORD, FORCED INDENTATION OF THE CODE, THREAD OVER (Troll)
withmagnus 26 Sep 2007, 01:03
i’m with magnus on this one. ^T^T^Tlame.
Ben 26 Sep 2007, 01:16
You’re free to disagree. I’m not actually trying to start a flame war — this guy does, so I don’t have to :-). Oh, Mr Magnus & Mrs Withmagnus: note that we Have The Power to moderate semi-anonymous comments, that is, comments with a dud email address.
Anonymoose 26 Sep 2007, 01:27
Actually, I like how braceless C looks much better. It has three advantages – ending the brace wars (BTW, K&R style was better anyways), saving visual space (you save one line per block), and forcing idiots to indent their code. And the cost? None, since good programmers are already indenting that.
I for one, welcome the forced indentation of code.
mark 26 Sep 2007, 01:31
The only reasonable way for me to write Python code is to ALWAYS use ‘pass’ at end of each block (even if non empty).
This makes emacs smart indentation work properly, which is a non-negotiable feature for a language/editor (In C, I’ve found dozens of cases where code had extra ‘;’ but was indented incorrectly).
Adding braces to python, very cool.
Orbiscerbus 26 Sep 2007, 01:58
@she: How do you know I program in Perl? :)
Seriously, I scan the source code more easily if there are some block markers, than without it. I feel somewhat lost reading Python code.
Phil Hassey 26 Sep 2007, 03:50
Nice :) I implemented something like that for C/C++ about a year ago:
http://www.imitationpickles.org/pyplus/
Though my implementation is written in python, it handles most stuff pretty well. I use it for writing C modules for the games I develop in python. A couple other people use it too.
-Phil
BJ Upton 26 Sep 2007, 06:59
“it’s not actually ‘significant whitespace’ — merely meaningful indentation.”
That’s a brilliantly succinct statement that tells the story much better.
I approached Python with some trepedation, maybe I would have been more initially enthusiastic if it had been stated this way first.
Well, I am enthusiastic now, so no long term damage! :-)
Ben 26 Sep 2007, 08:16
Thanks, BJ Upton.
Phil, good work. But pyplus (like my nobraces) doesn’t handle comments very well. Try this:
printf("10//3 is not a comment");
It also doesn’t do continuation lines — see these project details for a few of my ideas.
And it looks like Mattihas Goergens has signed up to develop the project on microPledge, with a $100 target. Sweet. Another $35, anyone? :-)
drewp 28 Sep 2007, 12:32
just use pyrex– it’s already got python syntax, with all the python libraries and datatypes, on top of the ability to use C types and libraries.
brbr 28 Aug 2008, 07:53
I use this brace style when writing code in C#. By appending the end brace to the end of the last line in the block, the code looks like Python – at least indent-wise…