End class, function or task identifier

Hi all,

Could you help me understand the advantages of identifier at endclass, endfunction, endtask, etc.
For example:

  • With identifier at endclass

class test;
endclass : test

  • Without identifier at endclass

class test;
endclass

From my perspective, I don’t see any advantage of this identifier. Without it, I can save coding time and avoid compilation error.

Thanks,
Chris.

In reply to chris_le:

The advantage is the compiler error you get from mismatched labels is much easier to fix then without the labels. This is most useful when there are many levels of nested blocks.

module test;
initial 
forever begin
         if (a) begin
            // stuff
            if (b) begin
             // more stuff
         end else if (c) begin
              // even more stuff
         end
       end
endmodule

Imagine that stuff was many lines of code. The only error message you will get from this is a syntax error pointing to the endmodule. Using begin/end labels makes it harder to make these kind of mistakes and easier for the compiler to point out the incorrectly terminated block. Having end labels also makes it easier to read the code when the starting lablel is not on the same page/screen.

In reply to chris_le:

Historically if one wanted end labels they had to be in a comment, such as


module monkey;
endmodule // monkey

If the name of the construct should change, oftentimes the end label would not get updated as well, leading to


module dog;
endmodule // monkey

Having the compiler check the end label is a very nice feature, and I wish more languages would implement it.

Great, thanks for useful information.

In reply to sbellock:

Having the compiler check the end label is a very nice feature, and I wish more languages would implement it.

When sticking to guidelines like “one top level declaration per file (module, interface, package, class, etc.)” and “preferring multiple nested methods rather than one big one” end labels (in whatever form, whether comments or enforced by the compiler) become useless.

In reply to Tudor Timi:

Yeah for modules, classes, and interfaces, especially if there is only one per file, the end labels don’t add much value. But I like using them in tasks and functions.