Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective advantages of C-style syntax

Tags:

c

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?

like image 291
Dario Avatar asked May 16 '09 11:05

Dario


People also ask

What are the advantages of Objective C?

Advantages (Pros) of Objective CObjective C is compatible to C and C++ programming language. As Objective C is a superset of C thus, the code of C and C++ runs smoothly on this. Objective C is stable. You don't need to spend money on migrating if you have developed your app on Objective C.

What is C and its advantages?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

What is the syntax of C language?

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction.


1 Answers

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!")

like image 51
Meinersbur Avatar answered Sep 18 '22 10:09

Meinersbur