[Oberon] A slightly better procedure

Jan Verhoeven jan at verhoeven272.nl
Sat Dec 11 02:59:31 CET 2010


Chris hinted me to the following improvement.

= = = = = 

1. BOOLEAN expressions
----------------------

PROCEDURE IsDigit (chr  : CHAR) : BOOLEAN;
BEGIN
  IF  chr < '0'  THEN  RETURN FALSE  END;
  IF  chr > '9'  THEN  RETURN FALSE  END;
  RETURN TRUE
END IsDigit;

It is not a good idea to get into the habit of having multiple exits 
from a procedure. In this case they are unnecessary as the recommended
Pascal / Modula-2 / Oberon programming style would result in:

PROCEDURE IsDigit(ch: CHAR): BOOLEAN;
BEGIN
  RETURN (ch >= '0') & (ch <= '9') 
END IsDigit;

= = = = = = 

which can be further improved to

PROCEDURE IsDigit(ch: CHAR): BOOLEAN;
BEGIN
  RETURN (ch <= '9') & (ch >= '0')
END IsDigit;

since there are many more characters above '9' than below '0'.

-- 
Met vriendelijke groeten,

Jan Verhoeven
http://www.verhoeven272.nl



More information about the Oberon mailing list