News for hackers. Stuff that matters.
Re:Dennis Ritchie's first C compiler on Github by mseepgood on Wednesday May 22, 2013 @12:14AM (#5748849)
He already used the right brace style (hanging braces, cuddled else) and the right indentation (tabs, not spaces).
Re:Dennis Ritchie's first C compiler on Github by comex on Wednesday May 22, 2013 @01:36AM (#5749091)
Somehow over the years learning C on my own, perhaps because it seemed more consistent to me, I got used to writing

    if(foo) {
omitting the space after control statements' names, which is almost never done - except in very early C code. (It's not just this: V7 Unix often omits the space as well.). I should probably settle on a less idiosyncratic personal style, but I can't help but take a little heart from seeing "my" style in such a famous codebase. :)
Re:Dennis Ritchie's first C compiler on Github by mseepgood on Wednesday May 22, 2013 @02:56AM (#5749283)
It was also defined in the Plan 9 code style: http://plan9.bell-labs.com/magic/man2html/6/style

"no white space after the keywords if, for, while, etc."

Re:Dennis Ritchie's first C compiler on Github by brianberns on Wednesday May 22, 2013 @02:17AM (#5749194)

    if(foo)
I avoid doing this because it looks like a function call.
Re:Dennis Ritchie's first C compiler on Github by kybernetikos on Wednesday May 22, 2013 @03:26AM (#5749357)
In an appropriately powerful language, it could be a function call.
Re:Dennis Ritchie's first C compiler on Github by TheBoff on Wednesday May 22, 2013 @04:20AM (#5749496)
This is all very well if you have a friendly, high level scripting language like Ruby, but I'm definitely glad I don't have to write a C compiler where functions can take arbitrary blocks of code.
Re:Dennis Ritchie's first C compiler on Github by rat87 on Wednesday May 22, 2013 @10:02AM (#5750863)
> In an appropriately powerful language, it could be a function call.

> This is all very well if you have a friendly, high level scripting language like Ruby, but I'm definitely glad I don't have to write a C compiler where functions can take arbitrary blocks of code.

That's a surprisingly good setup because in Smalltalk(one of ruby's main ancestor languages)

if/else is a method which takes a block closure.

    a ifTrue: [ l log: 'a is true'] ifFalse: [ 'a is false']
while

in ruby if else is a syntatic construct

    if a
        l.log ('a is true')
    else
        l.log ('a is false')
    end
probably more for perceived clarity/comfortability then speeds sake.
Re:Dennis Ritchie's first C compiler on Github by swift on Wednesday May 22, 2013 @05:30AM (#5749688)
Re:Dennis Ritchie's first C compiler on Github by a1369209993 on Wednesday May 22, 2013 @04:10AM (#5749473)
after

  #define if(X) LET(_it,X) if (_it)
it would be a macro call
Re:Dennis Ritchie's first C compiler on Github by yoklov on Wednesday May 22, 2013 @08:00AM (#5750150)
I'm fairly certain that shadowing a keyword with a macro is forbidden by the standard (or invokes UB?) but this will still work for most compilers, provided LET is defined reasonably. Hm, something like

     #define LET(name, X) \
       for (int let_once_=1, name=(X); \
            let_once_; \
            let_once_=0)
may be reasonable, though tokenpasting in __LINE__ for good measure might be necessary for nesting.
Re:Dennis Ritchie's first C compiler on Github by 30thElement on Wednesday May 22, 2013 @09:59AM (#5750850)
Shadowing keywords is perfectly valid, although I believe you can't shadow keywords in standard headers so that compiler writers don't need to worry about it. It gives rise to some "useful" C/C++ features that should never be used, like if you want to access private members in foo.cpp, do

     #define private public 
     #include "foo.cpp"
     #undef private
Re:Dennis Ritchie's first C compiler on Github by TazeTSchnitzel on Wednesday May 22, 2013 @12:24PM (#5751908)
I can imagine some insidious menace adding this to the top of a file:

    #define public protected
Re:Dennis Ritchie's first C compiler on Github by yoklov on Wednesday May 22, 2013 @03:51PM (#5753291)
I once someone jokingly do (the second only always works in C, not C++)

    #define else
    #define struct union
Evil!
Re:Dennis Ritchie's first C compiler on Github by breadbox on Wednesday May 22, 2013 @01:54PM (#5752561)
Which is exactly why you don't do it in a language where it isn't.
Re:Dennis Ritchie's first C compiler on Github by kybernetikos on Thursday May 23, 2013 @01:42AM (#5755522)
Why should a developer have to have headspace for a special class of construct? And what difference does it make in practice whether it is a keyword or a function - why do you think it's so important that they are obviously different in the code - does their being part of a special category have any practical implications?
Re:Dennis Ritchie's first C compiler on Github by PySlice on Wednesday May 22, 2013 @02:05AM (#5749166)
Never using a space before the paren is more consistent. I don't know why people put a space there...
Re:Dennis Ritchie's first C compiler on Github by rimantas on Wednesday May 22, 2013 @03:01AM (#5749295)
I don't want my function calls to be consistent with control structures.
Re:Dennis Ritchie's first C compiler on Github by justinhj on Wednesday May 22, 2013 @10:03AM (#5750868)
I don't recall ever mistaking function calls with control structures.
Re:Dennis Ritchie's first C compiler on Github by eru on Wednesday May 22, 2013 @03:19AM (#5749337)
That's the way you do it in normal text.
Re:Dennis Ritchie's first C compiler on Github by Mr_T_ on Wednesday May 22, 2013 @04:01AM (#5749447)
Recently some ignorant, nescient C++ programmer called me a Java programmer, because I use K&R style and not his preferred Allman style. Even Stroustrup uses hanging braces (with the exception of function body opening braces)!
Re:Dennis Ritchie's first C compiler on Github by Peaker on Wednesday May 22, 2013 @05:45AM (#5749721)
tabs are an infinite source of pain and inconsistencies...

Everyone must support the space character, it cannot be banned. But a simple commit hook to ban tabs can make indentation and alignment not get messed up over time with many collaborators with default-configured editors (that mess up and use tabs for alignment).

Re:Dennis Ritchie's first C compiler on Github by mikeash on Wednesday May 22, 2013 @10:34AM (#5751106)
The inventor of the tab character is on my list of people to assassinate when they're young if I ever get access to a time machine, the other people on the list being Hitler and Charles Douglass.

The tab character is a nice idea, but they do not seem to have worked out at all. I'd much rather have syntax-aware indenting in the editor, now that available compiler technology and CPU power make it practical.

Re:Dennis Ritchie's first C compiler on Github by GnarfGnarf on Wednesday May 22, 2013 @12:32PM (#5751960)
I don't understand what's so bad about tabs. With tabs, all you have to do is adjust the settings in your editor, and each programmer gets the indentation (s)he wants: 2, 4, 8, whatever...

Also, do you really want to be pressing the space bar twelve times, instead of tabbing three times?

I've seen the results of spaces-only: inconsistent, sloppy indentation, 7 or 9 spaces instead of 8, as long as it looks "indented" enough.

How, specifically does the tab character "not seem to have worked out at all"?

I'm not saying you're wrong for using spaces, just wondering...

Re:Dennis Ritchie's first C compiler on Github by mikeash on Wednesday May 22, 2013 @12:40PM (#5752015)
Don't confuse the tab character with the tab key.

The key is great. I use it all the time. Of course I don't want to press the space bar twelve times instead of tabbing three times. Of course I don't want sloppy indentation with 7 or 9 spaces instead of 8. Is this an actual problem? I've literally never seen either one in ~25 years of using languages that need indentation. The editor takes care of it.

The problem with the tab character is that there's no standard for how wide they're supposed to be, and so everyone uses them differently. Sure, in theory you use one tab character to indicate one level of indentation, and everybody can be happy. In practice, they're often not used that way. People will use two-space tabs with four-space indentation, using two tab characters to indent. People will use eight-space tabs with four-space indentation, using four spaces for one level of indentation, a tab character for two levels, a tab character followed by four spaces for three levels, etc. Some people just blithely mix and match for no particular reason. Put either one into an editor with different tab settings and it explodes into a huge mess.

Re:Dennis Ritchie's first C compiler on Github by JoeAcchino on Wednesday May 22, 2013 @01:58PM (#5752590)
Just use tabs for indentation and spaces for alignment.
Re:Dennis Ritchie's first C compiler on Github by Peaker on Wednesday May 22, 2013 @06:19PM (#5754170)
While you might do so, anyone you collaborate with will probably not do that. Why? Because almost every code editor in existence comes with that option turned off. And many of them don't even have that option at all! If you expect your codevelopers to manually adjust the alignment to be spaces rather than tabs on each line they edit, when their editor doesn't even distinguish tabs from spaces, oh boy.

Tabs just don't work out in a collaborative environment. It is too complicated for a heterogenous editing environment to get correct, so no such environment gets it correct in practice and code ends up a huge mess.

Re:Dennis Ritchie's first C compiler on Github by ma2rten on Wednesday May 22, 2013 @11:19AM (#5751426)
I am just thinking, why not solve this at the scm level, e.g. make a git plugin that will check out the sources indentation independent and only show changes to contents of the file not the indentation.

You could even make a plugin that works without collaboration by others: check out the file in your preferred style and transform the changes back into the original style.

Re:Dennis Ritchie's first C compiler on Github by mikeash on Wednesday May 22, 2013 @12:36PM (#5751981)
I totally agree. I seem to recall an experimental clang-based git plugin for this floating around. I think it even handled certain differences in naming convention.

We'll probably get there in a few more years, as we get comfortable with adding more power to these systems, and taking advantage of compiler technology for things other than generating code.

Re:Dennis Ritchie's first C compiler on Github by reledi on Wednesday May 22, 2013 @04:06AM (#5749458)
Your comment is flamebait.
Re:Dennis Ritchie's first C compiler on Github by brokenparser on Wednesday May 22, 2013 @01:58AM (#5749148)
Indentation, yes. Braces, no. He uses K&R style because he was the R (obviously). But nothing new is being written in K&R C these days, favouring ANSI C (99/11 or 89 if you're stuck on cl) with usually Allman/KNF style or a variation thereof instead.