When I was a beginner it took a while to learn the language syntax and the idea that languages couldn't improve after they were invented.
But now we're seeing new language features added every year with ES5, ES6 the so on.
If I don't write software for a few weeks or months when I come back to it I have often found for the first few days found myself writing if statements with the literal words and
and or
.
if (isSinglePost and isLoggedIn) {
// do something
}
AND I've seen other languages use literal and
and literal or
in their language syntax.
I'd like to have the option to use and
and or
or ||
and &&
. I'm not saying it's the best practice but it would be nice to have that option for readability and writability.
So now I'm wondering is there any reason languages didn't use and
and or
in their language syntax in the first place when they were invented? And would there be any negatives to adding it now?
ES2019candidate
Best Answer
In short
It's historical reasons.
The long history
Many older languages created between the 50's and the end of the 60's, as well as used the logical operators that you like such as
not
or
andand
:.NOT.
.AND.
.OR.
¬
for not,∧
(and=intersection) and∨
(or=union).¬
,∧
and∨
Their modern descendants (e.g. ADA) have kept this keyword style.
You can however see that already in this first list, there was a quest for concise expressions in many languages. But the character sets in those years were not portable and the later work on the ASCII character set didn't let many of the special characters survive.
Other languages used also the concise approach but chose characters that were more lucky in the standardisation process (for example see here the rationale that lead to the inclusion of | in the ASCII character set):
&
for and,|
for or and¬
for not&
for and,|
for or and~
for not. It also offered keyword alternatives. But those were not so appealing:LOGOR
,LOGAND
andLOGNOT
&
and|
. But as C is system oriented these were taken as bitwise operators. K&R identified also the need to have short circuit operators for conditional expressions to know that they can skip the rest of the expression if it's already known that it's true or false (the purpose was to write concise error checking conditions). And for these logical operators, they just doubled the symbol, so&&
and||
Then came C++ inspired by C, then Java inspired by C++ then JavaScript inspired from Java... and this is why nowadays so many languages have opted for the well known
||
and&&
P.S.: Note, that if JavaScript would have adopted
and
or
rather than&&
||
, it would probably have adoptedbegin
..end
rather than{
..}
, making it overall a lot more verbose than we are used to ;-)P.S.2: Note, that psshill points out in comment that C++ funilly supports
and
,or
,bitand
,bitor
and a couple of other alternative tokens. But nobody uses them. Interestingly, these are not a recent language features: Stroustrup explains in his book "The design and evolution of C++" that these keywords were introduced by the C++ ISO committee in November 1993, because in that pre-unicode world, ISO-646 used the ascii code of[]{}
and|
to map European characters, which made C++ very complex on terminals using this encoding. Strangely, though, there is nobegin
...end
to replace{
...}
and instead<%
and%>
. I guess that the alternate keywords not really won traction, because around the same period ISO-8859 encoding started to be used with all ascii characters available. Usage and habits did certainly do the rest: Stroustrup reports highly controversial discussions around alternative tokens.