PBP: 043 Heredoc Terminators

Mr. Conway suggests making all heredoc terminators single uppercase identifiers which start with a standard prefix.  This is part of naming things regularly, and I support it.

I think the more senior an engineer you get to be, the more regular your names for things become.  It becomes second nature to end heredocs with END_WHATEVER, and to give WHATEVER a really unique name so that you don’t miss it.  Uniqueness and legibility are important here, so you don’t wonder where the heredoc stops and the program starts again.

Just like I name all my file handles with something_fh (file handle) and format dates as YYY-MM-DDZHH:MM:SS (where ‘Z’ means UTC) this seems a natural thing to do.  I, or whoever encounters these things later, will appreciate them.  Lots of little things add up to easier to work with code.

4 Responses to “PBP: 043 Heredoc Terminators”

  1. Andrew says:

    Also agreed, except I think that “EOWHATEVER” was already standard before PBP came along.

    • Laufeyjarson says:

      Probably true, and probably clear enough.

      It does make me want to sing “Old MacDonald Had A Farm”, with the E-O combination. So do some of the standard error numbers. EIO, ENOERR, etc. :)

      Maybe that’s just how my wacky brain works, though.

  2. I like leading underscores to fake indentation:

    while(…){
    my $sql =<<"__END_SQL";
    select * from wherever
    __END_SQL

    }

    The emacs extension mumamo.el can use the heredoc id as a hint as to what kind of syntax coloring to use for it, so it could help to be rigorous about always indicating the format e.g. "SQL", "HTML", etc.

Leave a Reply