Your observations are correct. C++ is a complicated beast, and the new
keyword was used to distinguish between something that needed delete
later and something that would be automatically reclaimed. In Java and C#, they dropped the delete
keyword because the garbage collector would take care of it for you.
The problem then is why did they keep the new
keyword? Without talking to the people who wrote the language it's kind of difficult to answer. My best guesses are listed below:
- It was semantically correct. If you were familiar with C++, you knew that the
new
keyword creates an object on the heap. So, why change expected behavior?
- It calls attention to the fact that you are instantiating an object rather than calling a method. With Microsoft code style recommendations, method names start with capital letters so there can be confusion.
Ruby is somewhere in between Python and Java/C# in it's use of new
. Basically you instantiate an object like this:
f = Foo.new()
It's not a keyword, it's a static method for the class. What that means is that if you want a singleton, you can override the default implementation of new()
to return the same instance every time. It's not necessarily recommended, but it's possible.
Two of the major influences to C were the Algol family of languages (Algol 60 and Algol 68) and BCPL (from which C takes its name).
BCPL was the first curly bracket programming language, and the curly
brackets survived the syntactical changes and have become a common
means of denoting program source code statements. In practice, on
limited keyboards of the day, source programs often used the sequences
$( and $) in place of the symbols { and }. The single-line '//'
comments of BCPL, which were not taken up in C, reappeared in C++, and
later in C99.
From http://www.princeton.edu/~achaney/tmve/wiki100k/docs/BCPL.html
BCPL introduced and implemented several innovations which became quite
common elements in the design of later languages. Thus, it was the
first curly bracket programming language (one using { } as block
delimiters), and it was the first language to use // to mark inline
comments.
From http://progopedia.com/language/bcpl/
Within BCPL, one often sees curly braces, but not always. This was a limitation of the keyboards at the time. The characters $(
and $)
were lexicographically equivalent to {
and }
. Digraphs and trigraphs were maintained in C (though a different set for curly brace replacement - ??<
and ??>
).
The use of curly braces was further refined in B (which preceded C).
From Users' Reference to B by Ken Thompson:
/* The following function will print a non-negative number, n, to
the base b, where 2<=b<=10, This routine uses the fact that
in the ASCII character set, the digits 0 to 9 have sequential
code values. */
printn(n,b) {
extern putchar;
auto a;
if(a=n/b) /* assignment, not test for equality */
printn(a, b); /* recursive */
putchar(n%b + '0');
}
There are indications that curly braces were used as short hand for begin
and end
within Algol.
I remember that you also included them in the 256-character card code
that you published in CACM, because I found it interesting that you
proposed that they could be used in place of the Algol 'begin' and
'end' keywords, which is exactly how they were later used in the C
language.
From http://www.bobbemer.com/BRACES.HTM
The use of square brackets (as a suggested replacement in the question) goes back even further. As mentioned, the Algol family influenced C. Within Algol 60 and 68 (C was written in 1972 and BCPL in 1966), the square bracket was used to designate an index into an array or matrix.
BEGIN
FILE F(KIND=REMOTE);
EBCDIC ARRAY E[0:11];
REPLACE E BY "HELLO WORLD!";
WRITE(F, *, E);
END.
As programmers were already familiar with square brackets for arrays in Algol and BCPL, and curly braces for blocks in BCPL, there was little need or desire to change this when making another language.
The updated question includes an addendum of productivity for curly brace usage and mentions python. There are some other resources that do this study though the answer boils down to "Its anecdotal, and what you are used to is what you are most productive with." Because of the widely varying skills in programming and familiarity with different languages, these become difficult to account for.
See also: Stack Overflow Are there statistical studies that indicates that Python is “more productive”?
Much of the gains would be dependent on the IDE (or lack of) that is used. In vi based editors, putting the cursor over one matching open/close and pressing %
will then move the cursor to the other matching character. This is very efficient with C based languages back in the old days - less so now.
A better comparison would be between {}
and begin
/end
which was the options of the day (horizontal space was precious). Many Wirth languages were based on a begin
and end
style (Algol (mentioned above), pascal (many are familiar with), and the Modula family).
I have difficulty finding any that isolate this specific language feature - at best I can do is show that the curly brace languages are much more popular than begin end languages and it is a common construct. As mentioned in Bob Bemer link above, the curly brace was used to make it easier to program as shorthand.
From Why Pascal is Not My Favorite Programming Language
C and Ratfor programmers find 'begin' and 'end' bulky compared to { and }.
Which is about all that can be said - its familiarity and preference.
Best Answer
There needs to be some way of telling where the condition ends and the branch begins. There are many different ways of doing that.
In some languages, there are no conditionals at all, e.g. in Smalltalk, Self, Newspeak, Io, Ioke, Seph, and Fancy. Conditional branching is simply implemented as a normal method like any other method. The method is implemented on booleans objects and gets called on a boolean. That way, the condition is simply the receiver of the method, and the two branches are two arguments, e.g. in Smalltalk:
In case, you are more familiar with Java, this is equivalent to the following:
In the Lisp family of languages, the situation is similar: conditionals are just normal functions (actually, macros) and the first argument is the condition, the second and third argument are the branches, so they are just normal function arguments, and there is nothing special needed to delimit them:
Some languages use keywords as delimiters, e.g. Algol, Ada, BASIC, Pascal, Modula-2, Oberon, Oberon-2, Active Oberon, Component Pascal, Zonnon, Modula-3:
In Ruby, you can use either a keyword or an expression separator (semicolon or newline):
Go requires the branches to be blocks and doesn't allow expressions or statements, which makes the curly braces mandatory. Therefore, parentheses aren't required, although you can add them if you want; Perl6 and Rust are similar in this regard:
Some languages use other non-alphanumeric characters to delimit the condition, e.g. Python:
The bottom line is: you need some way of telling where the condition ends and the branch begins. There are many ways of doing so, parentheses are just one of them.