Dario
Dario

Reputation: 49218

Objective advantages of C-style syntax

There is a really big number of programming-languages that are heavily influenced by C-style syntax (curly-braced, semicola, etc.), maybe more than by any other syntactial style. And till this day, many modern and successful or even newly invented languages use this syntax - just think of Java, C++, C#, PHP, JavaScript, C, Perl etc.

Are there any objective reasons that could explain the great spread and success of this syntax? Are there certain advantages over the syntax of other languages?

Upvotes: 10

Views: 3012

Answers (10)

Marco van de Voort
Marco van de Voort

Reputation: 26356

Afaik the popularity of C is directly linked to Unix' popularity, and not to its syntax. There are multiple facets to that point; its lowlevel language(*) and thin mandatory library suitable for kernel development, the availability of compilers, the relative early attempts at cross-system compatibility.

If I had to name a second reason it would be the relative loose build model (compilation units only meet at the linker, only the linker sees the (mostly digested program) complete for the first time), which is great for low memory systems.

Code density is often said, but that is later revisionism. As for later languages adopting the syntax, that is more in the hope that it would make an upgrade path easier than superiority of syntax. This is clearly visible in something as C# which is quite far from C, except for the blocksyntax and name.

(*) I'm hinting more on the absense of lots of compiler helpers. IOW more or less the contents of libgcc if you cut it down to K&R level.

Upvotes: 0

Meinersbur
Meinersbur

Reputation: 8071

IMHO, the only thing that makes C-style syntax popular is that most people know it. And thus, using C-style for a new language makes it possible to apply old habbits (like naming conventions). This is a good thing! Although I think, the syntax is the least concern for learning a new language. But a good syntax can help to avoid errors.

Microsoft did a lot of effort to make VB.NET as important as C# (remember all the "null (Nothing in Visual Basic)" in the MSDN, which quite annoys me), but still C# is the dominant language for the .NET platform. It seems that VB.NET has a problem with the bad reputation of its predecessors. And still, using C-style seems to make a more professional look.

After all, one of C's design goals was to make the compiler easy to implement. It was easier to use a preprocessor than defining a new language construct for constants. Or the semantics of a[5]. This does not apply to C++, which is very difficult to implement, partly because it tries to stay compatible with C.

Examples:

  • Case sensitiveness, although case insensitivity is more natural to humans (NOT to computers). This doesn't mean that you should spell identifiers differently than they have been declared (beware!), but can lead to confusion. Real-world example (Java): getWhiteSpace() or getWhitespace()?

EDIT: Another nice example: What is the worst gotcha in C# or .NET?. But of course, once you are used to it and with the help of an IDE, this isn't much of a problem anymore, sometimes even more natural because it resembles better how computers actually work.

  • Operator precedence

  • = for assignment and == for comparison. if (a = b) anyone? Similar, the && and &, || and |, (! and ~) are syntactically too close although they mean different things. Personally, I'd prefer and and or, because symbols should just support syntax instead of being the main part.

  • ++ and -- operators; Makes some statements just a little bit shorter, but introduces side effects to expressions (a = b+++b++). Originally, compilers could compile this more efficiently than i = i + 1.

  • for(init;condition;step) loop; Although best practise is to use it to increment a variable only, no explicit syntax exists for this. Instead, this for construct is redundant as it (nearly) the same as

    init;
    while (condition) {
      statement;
      step;
    }
    
  • switch statement; ever forgotten a break? Why not allowing ranges as case labels, as most other languages do?

  • if(condition) statement. Using parenthesis wasn't a that good choice as it can be used in the condition expression itself:

    if (!(var & 0x02))
    
  • Preprocessor

  • Braces. This is controversial. I don't agree to arguments that state that these "don't use a lot of screen estate", are terser or are faster to write. First, a language should be designed to be easy to read, not easy to write. Second, depending on your style, braces uses uses the exact same amount of screen space than keywords: You write them in a single line. Isn't that a lot of wasted space?

    Additionally, people criticise LISP for its clutter of parentheses. Never happened to you that you have to count your braces to find out where you have missed one? I sometimes add a comment after the closing brace to indicate what is supposed to end here. BASIC syntax has this already included. And doesn't even need an equivalent to an opening brace. Partly I agree that braces are good: They are nearly invisible and indention is the dominant visual characteristic. Seen this way, python is the next step.

  • Semicolons as statement terminator or separator. Why is a single semicolon a valid statement?

    if (condition);
      DoSomething();
    
  • Indistinguishable sequences of keywords

    public static string main()
    

    Is it a method declaration? Or a variable declaration? Or a function prototype? Or something else? Some punctuation (and keywords for every declaration type) could have helped here, for instance, to clearly separate the return type. This is what makes C++ hard to parse.

  • Orthogonality. {} while (condition) does fit to the other language constructs where the statement is followed by the block. I think VB's

    do [while/until condition]
      Statements
    loop [while/until condition]
    

    a nice solution because you have 4 possible combinations with different semantics: until/while after the do/loop keyword.

  • Strange order of variable type modifiers.

    int * const & i [];
    
  • Type and variable name just appear after each other, no marker that it is a local variable declaration. Scala uses val and var do indicate a declaration of a final/mutable variable and the type is separated by a colon. On most other things, Scala uses Java syntax.

  • Assignment operator returning a value; No distinction between statements (with effects) and expressions (which just return a value)

EDIT: Some more examples here: https://stackoverflow.com/questions/163026/what-is-your-least-favorite-syntax-gotcha

You certainly won't agree on many of these points, and not all of them are necessarily negative (semicolons, for instance), or that I knew a solution that is better for all cases. Even if I would, the resulting language would not be the perfect language. Programming languages will always evolve and newly invented languages hopefully learn from its predecessors. So, why not staying at a known syntax instead of designing a new one from every ten years?

However, when a language designer has the possibility to avoid programming errors which are just typing errors, why not changing it? For instance this was done in C#'s switch statement, which makes break (or goto) mandatory. And once the worst drawbacks have been removed, the benefit that most programmers know the rest of the syntax syntax far outweighs the advantages of redesigning a language from scratch. But I am still astonished why so many programmers still defend C-syntax so eager, although these are used to that progress in computer science requires revision of nearly everything regularly.

To conclude, I think the only reason that C syntax is dominant is because it known to nearly all professional programmers, these a just used to it. The actual syntax is less important, although other languages might have advantages. This is the same reason why electrical engineers use the convention of electric charge as it is.

https://imgs.xkcd.com/comics/urgent_mission.png

(Maybe there will be a comic about a programmer visiting Dennis Ritchie, too: "Please, don't make breaks in switch statements optional!")

Upvotes: 16

Electrons_Ahoy
Electrons_Ahoy

Reputation: 38603

As I see it, there are really only three major syntax elements that have been carried from C out to the rest of the world: blocks denoted by curly-braces, semi-colons to denote ends of lines, and a general "terseness" of style.

Wrapping blocks in a single character makes reasonably good sense; first, it's fast to type, doesn't take up a lot of screen real-estate (unlike a BEGIN-END keyword pair). Second, the syntax is fairly flexible, so you can format your blocks as you need/want to in special cases (which you can't really do in something like Python.) Finally, your code can be munged up quite a bit by something like an email client and still be readable by both humans and a compiler (which is the only real problem I have with python-style indents-for-blocks).

Why curly-braces, though? I don't know what the historical precedent for using them in C (or, more likely, BCPL) was, but I can hazard a guess. There aren't that many paired symbols on a "standard" American keyboard: {} [] () and <> are about it. If we want to make life easy on the compiler, we want unique symbols for BEGIN and END, so using something like | or # for the block ends is out. Of our pairs, {} is really the only one that doesn't already mean something - () and [] both have a lot of math baggage (which gets translated more or less directly with functions and arrays) and both < and > mean all kinds of things. I'd choose {} for blocks too.

With a new language, if you aren't going with keywords or indentation, why change it? Legions of programmers are used to using them to denote blocks, why invalidate all that muscle memory?

Much the same argument applies to using semicolons. Using something to denote the end of a line makes life much easier for the compiler. Using only a single character makes life a lot easier for the programmer. Scanning the single characters on the keyboard, the semi-colon is one of the few that doesn't mean much, mathematically speaking. From an English grammar point of view, the period (or maybe the comma) make the most sense, but they're already used as decimal points. And, if you squint a little, having a semi-colon as your line terminator has a reasonably similar meaning as it does in English. And again, if you're starting a new language, why change it?

As for the basic terseness, I'd posit that was the only one you could say, objectively, was a good idea. The fewer characters I can type to get the idea across to the computer while still being close enough to English to read, the better.

(You could argue that most C-type languages also borrow most of the keyword vocabulary, but really, most of C's keywords came from older languages like ALGOL, FORTRAN, and BCPL, and really - they're all (mostly) common sense. And again, once you've trained the programming community what a "while loop" is, why change the name?)

I'd argue that any language today that doesn't use a syntax a lot like C's is doing so because of some fundamental paradigm shift (like Python's approach with indents). If you're making a language that works basically the same way, why change anything? Your target audience can already hit the curly-brace key with their pinkies, why make that particular skill worthless?

Or, to take that last sentence a step further, if you're trying to sell the programming community on your new, game-changing language (Java, C#, etc.) you're going to have a lot less hurdles to jump if your customers all already know the syntax.

Upvotes: 6

corlettk
corlettk

Reputation: 13574

As to why curly-braces caught on... Two reasons:

  1. The wind-tunnel effect. There are only so many good solutions to any given problem, and the more the problem is analysed the more alike the solutions to those problems are likely to become. Hence a 2009 Chevrolet more closely resembles a 2008 Ford than a 57' Chevy does a '57 Ford... The new Chevy and the new Ford where designed in the same wind tunnel. Curly-braces and semi-colons make simple engineering sense, making C substantially easier to parse (for both computers and humans) than comparable languages of "the block" style... Hence C# so closely resembles Java that I sometimes momentarily forget which langauge I'm using.

  2. (As previously stated) It's much easier for programmers to learn a new language which "looks and feels like" the previous model. Don't reinvent the wheel and it won't roll over on you ;-)

Cheers. Keith.

PS: I predict that within 50 years we'll be using "natural language" compilers... and reminiscing fondly about the good 'ole days of curly-brace languages, when men where men, and sheep where scared.

Upvotes: 0

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247999

People are used to it. When it was invented, every language was ugly as hell. Back then, C gained popularity for sucking less. (and perhaps for being more down-to-earth than LISP).

Today, other languages reuse the syntax because it's familiar to programmers.

I don't think there's much more to it than that. I prefer braces over begin/end (although the braces are a pain on many non-english keyboards), but there are still a lot of quirks of C syntax that could be done better. C++ is discovering that the return type might just fit better after the parameters (C++0x is allowing that syntax because it works better with other new features like decltype).

And most functional languages have realized that the parentheses around the parameters are often not necessary. For that matter, explicit typing often isn't necessary either. But most languages inherit that from C because "that's the syntax". Type first, then variable/function name.

And let's not even get into the abomination that is function pointers. Surely we can find a more elegant syntax for their types. Or try typedef'ing an array type.

Then there is the quirky choice of operators. Why not just use "and" instead of &&?

C's syntax isn't nice. It does the job, and we're so used to it that it's probably here to stay. But it's not "good".

Upvotes: 1

Pete Kirkham
Pete Kirkham

Reputation: 49311

Are there any objective reasons that could explain the great spread and success of this syntax?

Not quite objective, but C had three main historic advantages:

  • it was a bit terser than other languages at the time ( use of {} rather than Algol's begin/end )
  • it had no obvious disadvantages ( eg Fortran had ambiguities and didn't support multiple statements on one line )
  • after it got popular, almost every other language designer knew C, and probably worked in C to build their language's toolset

Are there certain advantages over the syntax of other languages?

Well, having explicit block and statement delimiters allows multiple-statement expressions; for example, you can't do multi-statement lambda expressions in Python ( not that you have lambdas in C, though you do in the newest C++ ). Having to only type one character for blocks is a minor advantage, but not a massive one (it's probably easier to set up an editor to match "begin" to "end" than it is to match C's ("{" OR "??<") to ("}" OR "??>"), and if typing speed is the limiting factor in your programming, you're probably not automating a task you should be ).

Upvotes: 0

ennuikiller
ennuikiller

Reputation: 46965

True be told why invent a completely new syntax when c is a concise and easily understood. It also helped that the majority of programmings were familiar with c AND the languages themselves were implemented in c. It's a case of why try to improve on something that already works very well.

Upvotes: 1

mouviciel
mouviciel

Reputation: 67839

C-style syntax is very compact. Depending on situations this is a drawback or an advantage. Anyway this is a productivity boost for senior C coders.

Many new languages officially claim heritage from C, e.g. C++, C#, Objective-C.

Moreover, I guess that many language creators have a big C background. Deliberately or not, they may reproduce in their new language what they know best and what they judge most efficient.

Upvotes: 0

anon
anon

Reputation:

Block structured languages have to specify blocks somehow. The relative unpopularity of the Pascal language family seems to indicate keywords are not a good way of doing this. On the other hand, the popularity of Python may mean that in future more languages will use indentation alone to indicate structure - thougi I for one hope not.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351526

I think it is simply a matter of style rather than a matter of advantage.

Upvotes: 3

Related Questions