August Karlstrom
August Karlstrom

Reputation: 11377

Why are module and procedure names repeated after the body?

In Modula-2 and Oberon each module and procedure declaration must end with the name of the module or procedure. It is not needed in Pascal. I have never really understood the motivation for this. Can someone enlighten me?

Upvotes: 2

Views: 208

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12698

It's possible you get something like this:

MODULE A;
  ...
  PROCEDURE B;
    ...
    PROCEDURE C;
    ...
    BEGIN (* C *)
      ...
    END C;
  BEGIN (* B *)
    ...
  END B;
BEGIN (* A *)
  ...
END A.

In that case, for readability you have three bodies (and more if you have defined nested procedures and functions) at the end of your code. In order to see which one is the one ending in the END clause, it's better if the compiler can check that you ar closing things correctly (as you see, I even put it ---as a comment, but would be nice if the compiler accepted it as a valid identifier and checked just to be sure things match correctly--- at the start BEGIN body clause)

Upvotes: 2

kabanus
kabanus

Reputation: 25980

After reading some (I am not an expert) I would wager this is just a syntax demand of the function for better readability.

I'll go one step further, and say in large, old, badly written procedures/function in other languages, this is sometimes done without the language requiring it. I've often seen:

int veryLongC++Function() {
    ...
    ...
    ... 3000 code lines
} //veryLongC++Function

So a reader jumping near the end knows what in the mess they are looking at. August mentions in the comment this is much less robust when not enforced by the compiler - in case of a name change.

Another important aspect is nested procedures - here the explicit ending makes things much more readable - checkout chapter 7 for an example - a nested procedure is declared between a declaration and before the BEGIN. The syntax makes this looks much better (in my opinion).

So long story short - I think the main benefit is readability.

Upvotes: 5

Related Questions