Sunday, February 27, 2005

The break, continue and return statements are not branching statements

Introduction

Branching statements are statements that let you make decisions in your program (Holzner 2003).

Discussion

So, break, continue and return statements are not branching statements but jump statements, because those statements are for jumping, not for making decisions.

The branching statements (also called conditional statements) are:

  • if statement
  • else statement
  • switch statement

References

Wednesday, February 23, 2005

Keywords are not commands

Introduction

Generally, keywords are not commands, operators, methods or something that does anything useful — well, at least most of the time, with the exception of the instanceof operator.

Visually, we use a space after the keyword token to indicate that it is not a method, although the space is not required by the syntax.

Code Convention: Always space after keyword
if (condition) { // don't write: if(condition) {
    statements;
}
Code Convention: Never space after method identifier
System.exit(0); // don't write: System.exit (0);

Discussion

Keywords are reserved because they are part of the language syntax, making the syntax easier for programmers to read, not because they have any meaning. (With the exception of languages such as PostScript, where keywords are lists of rules.)

For example, Dahl and Nygaard (SIMULA 1965) say that the symbol new is redundant. If its redundant, then obviously it can't be a command. Dahl and Nygaard (SIMULA 1965) also say that the symbol new often can be used to advantage to improve readability.

Concept: The real function of keywords is to make it look more like english, making it easier for humans to remember the syntax and to read a program. You should always consider the whole grammar rule that the keyword is a part of. Do not assume that a keyword should do anything by itself. If you do think really hard about what a keyword should be expected to do, you will most likely just end up confused.

keywords as operators

In some languages like Simula 67, a keyword could also be an operator, which is a bad idea because keywords as operators complicates the language. For example, the operator > could be represented as the keyword gt. Probably they used keyboards without that character (1). In Java, most keywords are not operators, except for instanceof.

Conclusion

Keywords are not commands. Keywords are part of grammar rules, making programs easier to read and understand for humans. You may convince yourself by examining The Java JLS3 Grammar.

References

1. If you have an reference for this one, please tell me!

Dahl and Nygaard, 1965, SIMULA, If you have an reference for this one, please tell me!

Keywords are not identifiers

Introduction

In Java, keywords are not identifiers, but tokens (with the exception of languages such as PostScript, where keywords are lists of rules.)

Discussion

Louden (1997:31) say that keywords are fixed strings of letters and that identifiers are user-defined strings. Obviously keywords are defined in the grammar, and can't be user-defined strings. Therefore, keywords can't be identifiers.

The Java Grammar explicitly say that identifiers are not keywords or boolean literals or the null literal:

Identifier =
IdentifierChars - (Keyword | BooleanLiteral | NullLiteral) ;

The Java Grammar also say that both identifiers and keywords are tokens:

Token =
Identifier | Keyword | Literal | Separator | Operator ;

References