Objective benefits of C-style syntax

There are indeed a large number of programming languages ​​that are heavily influenced by C-style syntax (kinky, half-columned, etc.), possibly more than any other syntax style. And so far, many modern and successful or even recently invented languages ​​use this syntax - just think about Java, C ++, C #, PHP, JavaScript, C, Perl, etc.

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

+7
c syntax
source share
10 answers

IMHO, the only thing that makes C-style syntax popular is that most people know this. Thus, using the C-style for the new language allows the use of old habbits (for example, naming conventions). This is a good thing! Although, I think the syntax is the least problem for learning a new language. But good syntax can help avoid errors.

Microsoft has made a lot of efforts to make VB.NET as important as C # (remember everything “ null ( Nothing in Visual Basic) on MSDN, which annoys me quite a bit), but still C # is the dominant language for the .NET platform. . VB.NET seems to have a problem with the poor reputation of its predecessors. And yet, using C-style seems to make for a more professional look.

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

Examples:

  • Case sensitivity, although case sensitivity is more natural for people (not computers). This does not mean that you must specify identifiers differently than they were declared (beware!), But they can lead to confusion. Real world (Java) example: getWhiteSpace () or getWhitespace ()?

EDIT: Another nice example: What's the worst in C # or .NET? . But, of course, once you get used to it and with the help of the IDE, this is no longer a big problem, sometimes even more natural, because it better resembles how computers work.

  • Operator Priority

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

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

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

     init; while (condition) { statement; step; } 
  • switch statement; ever forgot a break? Why not allow ranges as label labels, as most other languages ​​do?

  • if(condition) . Using parentheses was not such a good choice, as it can be used in the condition expression itself:

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

  • Braces. This is controversial. I disagree with the arguments that claim that these “do not use many screen properties” are more complicated or faster to write. First, a language must be designed to be easy to read, not just write. Secondly, depending on your style, using curly braces uses the same amount of screen space as the keywords: you write them on the same line. Isn't that a lot of lost space?

    In addition, people criticize LISP for the mess of parentheses. Has it ever happened to you that you have to count your braces to find out where you missed it? I sometimes add a comment after closing to indicate what should end here. BASIC syntax is already enabled. And you don’t even need the equivalent of an opening parenthesis. In part, I agree that the brackets are good: they are almost invisible, and indentation is the dominant visual characteristic. It can be seen that the next step will be python.

  • Semicolons - terminator terminator or separator. Why is one semicolon valid?

     if (condition); DoSomething(); 
  • Indistinguishable keyword sequences

     public static string main() 

    Is this a method declaration? Or a variable declaration? Or a function prototype? Or something different? Some punctuation (and keywords for each type of ad) could help here, for example, to clearly separate the return type. This is what makes C ++ difficult to parse.

  • orthogonality. {} while (condition) is suitable for constructs of another language, where the statement is followed by a block. I think VB

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

    A good solution, because you have 4 possible combinations with different semantics: before / after the do / loop keyword.

  • Strange order of variable type modifiers.

     int * const & i []; 
  • The type and name of the variable just appear one after another, the token is not a local variable declaration. Scala uses val and var indicates the declaration of a finite / mutable variable and the type is separated by a colon. In most cases, Scala uses Java syntax.

  • An assignment operator that returns a value; No difference between statements (with effects) and expressions (which just return a value)

EDIT: Below are a few examples: https://stackoverflow.com/questions/163026/what-is-your-least-favorite-syntax-gotcha

You probably will not agree to many of these points, and not all of them are necessarily negative (for example, semicolons) or that I knew a solution that is best for all cases. Even if I, the resulting language would not be an ideal language. Programming languages ​​will always evolve, and the new invented languages, we hope, will learn from its predecessors. So, why not stay in the well-known syntax, and not develop a new one out of every ten years?

However, when a language developer is able to avoid programming errors that only print errors, why not change it? For example, this was done in a C # switch statement, which makes break (or goto) mandatory. And once the worst flaws have been eliminated, an advantage that most programmers know, the rest of the syntax syntax far outweighs the benefits of redesigning the language from scratch. But I still wonder why so many programmers still advocate C-syntax so impatiently, although they are used for this progress in computer science, it needs to be reviewed almost all regularly.

In conclusion, I believe that the only reason syntax dominates syntax is that it is known to almost all professional programmers, they just got used to it. The actual syntax is less important, although other languages ​​may have advantages. This is for the same reason why electrical engineers use the convention of electric charge as such.

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

(Maybe there will be a comic strip about a programmer who will visit Dennis Ritchie, too: "Please do not make break s optional in switch operations!")

+14
source share

As I can see, in fact there are only three basic syntax elements that have been transferred from C to the rest of the world: blocks indicated by curly braces, half-columns to indicate the ends of lines, and the general "ruff" of the style.

Blocking blocks in one character makes sense; firstly, it prints quickly, does not take up a lot of screen real estate (unlike the pair of BEGIN-END keywords). Secondly, the syntax is quite flexible, so you can format your blocks as you wish in special cases (which you cannot do in something like Python). Finally, your code can be a little spoiled by something like an email client and still readable by both people and the compiler (this is the only real problem that I encounter with python-for-blocks style indentation).

Why braces? I don’t know what historical precedent for their use in C (or rather BCPL) was, but I can beware of guesswork. There are not so many paired characters on the "standard" American keyboard: {} [] () and <> about it. If we want to make life easy in the compiler, we need unique characters for BEGIN and END, so use something like | or # for block endings. Of our pairs {}, really the only thing that does not mean anything is () and [], both have a lot of mathematical baggage (which is more or less directly translated with functions and arrays) and> means all kinds of things. I would choose {} for blocks too.

With a new language, if you don’t go with keywords or indentation, why change it? Legions of programmers are used to use them to designate blocks, why are all of these muscle cells invalid?

The same applies to using semicolons. Using something to mark the end of a line makes life easier for the compiler. Using only one character makes life easier for the programmer. Scanning single characters on a keyboard, a half-column is one of the few, which does not mean much, mathematically speaking. From the point of view of English grammar, a period (or maybe a comma) makes the most sense, but they are already used as decimal points. And, if you squinted a little, having a half-time, since your line terminator has a fairly similar meaning, as in English. And again, if you are starting a new language, why change it?

Regarding the basic terminology, I would say that this was the only one that you could say, objectively, was a good idea. The fewer characters I can type to transfer the idea to a computer, but still close enough to read English, the better.

(You can argue that most C-type languages ​​also occupy most of the vocabulary of keywords, but in fact most C keywords are taken from older languages ​​such as ALGOL, FORTRAN and BCPL, and indeed they are all (mostly ) common sense. And again, once you have prepared the programming community, what is a "while loop", why change the name?)

I would say that any language today that does not use C-like syntax does this because of some fundamental paradigm shift (like Python's indented approach). If you make a language that works basically the same way, why change something? Your target audience can already hit the key with the figurines of their little ones, why make this skill worthless?

Or, to make this last offer one more step, if you are trying to sell the programming community in your new, game-changing language (Java, C #, etc.), you will have much less obstacles to transition if your customers are already know the syntax.

+4
source share

I think this is just a matter of style, not a matter of advantage.

+3
source share

Block structured languages ​​must somehow indicate blocks. The relative unpopularity of the Pascal family of languages ​​seems to indicate that keywords are not a good way to do this. On the other hand, the popularity of Python may mean that in the future more languages ​​will use indentation alone to indicate structure - you, I hope not.

+1
source share

Tell me why invent a completely new syntax when c is concise and easy to understand. It also helped to make most programs familiar with c. And the languages ​​themselves were implemented in c. This is the case why you are trying to improve something that already works very well.

+1
source share

People are used to it. When it was invented, every language was ugly. At that time, C gained popularity for sucking less. (and possibly for a lower level than LISP).

Today, other languages ​​reuse syntax because it is familiar with programmers.

I do not think there is much more. I prefer brackets over start / end (although brackets are a pain on many non-English keyboards), but there are still a lot of C syntax quirks that could be done better. C ++ detects that the return type may simply better match the parameters (C ++ 0x allows this syntax because it works better with other new functions, such as decltype).

And most functional languages ​​have realized that parentheses around parameters are often not needed. In this case, explicit typing is also often not needed. But most languages ​​inherit this from C, because "this is the syntax." Enter first, then the variable / function name.

And even if it does not fall into the abomination, which is a pointer to a function. Of course, we can find a more elegant syntax for their types. Or try entering an array type.

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

Syntax

C is not pleasant. It does the job, and we are so used to it that it is probably here to stay. But this is not "good."

+1
source share

C-style syntax is very compact. Depending on the situation, this is a disadvantage or advantage. In any case, this is an increase in performance for older C-encoders.

Many new languages ​​officially declare their legacy with C, for example. C ++, C #, Objective-C.

In addition, I think that many language creators have a great background C. Intentionally or not, they can reproduce in their new language what they know best and what they consider the most effective.

0
source share

Afaik's popularity with C is directly related to the popularity of Unix, not its syntax. From this point of view there are several faces; its low-level language (*) and a thin compulsory library suitable for kernel development, the availability of compilers, relative early attempts at mutual compatibility.

If I had to name the second reason, it would be a relative free assembly model (compilation units are found only in the linker, only the linker sees (mostly a digested program) for the first time), which is great for low memory systems.

Often talk about code density, but this is later revisionism. For later languages ​​that use syntax, this is more in the hope that this will simplify the upgrade path than the superior syntax. This is clearly visible in something like C #, which is pretty far from C, with the exception of blockyntax and the name.

(*) I hint more at the lack of a large number of compiler helpers. IOW more or less the contents of libgcc if you shorten it to the K & R level.

0
source share

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

Not entirely objective, but C had three main historical advantages:

  • it was a bit more than other languages ​​at the time (using {}, not the beginning / end of Algol)
  • it had no obvious flaws (for example, Fortran had ambiguities and did not support multiple statements on the same line)
  • after it became popular, almost every developer of other languages ​​knew C and probably worked in C to create their own language toolkit

Are there certain advantages over the syntax of other languages?

Well, having explicit separators for blocks and operators, allows expressions with multiple operators; for example, you cannot do multibyte lambda expressions in Python (not that you have lambdas in C, although you do it in the latest C ++). Having only one character for blocks is a minor advantage, but not massive (it might be easier to configure the editor to match "begin" to "finish" than match C ("{" OR "? <") To ("}" OR " ?> "), and if input speed is a limiting factor in your programming, you probably won't automate the task you should be).

0
source share

As for why braces are caught ... Two reasons:

  • The effect of the wind tunnel. There are only so many good solutions for any given problem, and the more the problem is analyzed, the more similar the solutions to these problems. Consequently, the 2009 Chevrolet is more reminiscent of a Ford 2008 than the 57-inch Chevy makes the “57 Ford ... The new Chevy and the new Ford designed in the same wind tunnel. Curly-braces and semi-colons create a simple engineering sense, making C significantly easier to parse (both for computers and people) than comparable "block" style languages ​​... Therefore, C # is so similar to Java that I sometimes forget for a moment that langauge I use.

  • (As said earlier) It is much easier for programmers to learn a new language that “looks and feels” the previous model. Do not reinvent the wheel and it will not roll over on you; -)

Greetings. Whale.

PS: I predict that for 50 years we will use compilers of the "natural language" ... and lovingly recall the good "days of languages ​​in love" when people, where men are, and sheep, where they are afraid.

-one
source share

All Articles