Search code examples
mainframeadabas-natural

Join statements in a line


I'd like to join a few instructions in a single line in my NATURAL program. Just like this :

**before
    Statement1
    Statement2
    Statement3
 **after
    Statemen1 statement2 statement3

I know that is quite easy doing in most languages, but I'm not sure it's possible in Natural.


Solution

  • Douglas Bader, the British Flying Ace & others are quoted as saying
    "Rules are for the guidance of wise men and the obedience of fools"

    I always say:
    "The only rule that's always valid is that no rules are always valid"

    In programming we have a number of useful rules, one of which is:
    "Every Statement on a new line"
    ...and it is wise to follow that adage, but there are exceptions.
    Sometimes, you can actually improve the readablity of code by breaking the rule.

    Here's an example (written in Natural):

    define data
        local 1     DATE-FROM (D)  /* (D=Natural Date-Format)
              1     DATE-TO   (D)
    end-define
    
        DATE-FROM := *DatX  /* System Variable = "Today"
        DATE-TO   := DATE-FROM + 1
        perform   P800-WRITE-DATE
    
        DATE-FROM := DATE-TO
        DATE-TO   := DATE-FROM + 2
        perform   P800-WRITE-DATE
    
        DATE-FROM := DATE-TO
        DATE-TO   := DATE-FROM + 3
        perform   P800-WRITE-DATE
    
        DATE-FROM := DATE-TO
        DATE-TO   := DATE-FROM + 4
        perform   P800-WRITE-DATE
    
    define subroutine P800-WRITE-DATE
        write  DATE-FROM(em=DD.MM.YYYY)
               DATE-TO  (em=DD.MM.YYYY)
    end-subroutine
    END
    

    Now, I would write the doing-bit of that as follows:

    DATE-FROM := *DatX      DATE-TO := DATE-FROM + 1    perform P800-WRITE-DATE
    DATE-FROM := DATE-TO    DATE-TO := DATE-FROM + 2    perform P800-WRITE-DATE
    DATE-FROM := DATE-TO    DATE-TO := DATE-FROM + 3    perform P800-WRITE-DATE
    DATE-FROM := DATE-TO    DATE-TO := DATE-FROM + 4    perform P800-WRITE-DATE
    

    It's much easier to understand, spot typos & deliver quality.

    The Output today was:

    28.02.2021  01.03.2021
    01.03.2021  03.03.2021
    03.03.2021  06.03.2021
    06.03.2021  10.03.2021
    

    Getting back to the original question:
    in Natural, you may end a Statement with a ";" but it is not necessary. The following is perfectly valid:

    DATE-FROM := *DatX    ;  DATE-TO := DATE-FROM + 1  ;  perform P800-WRITE-DATE;
    DATE-FROM := DATE-TO  ;  DATE-TO := DATE-FROM + 2  ;  perform P800-WRITE-DATE;
    DATE-FROM := DATE-TO  ;  DATE-TO := DATE-FROM + 3  ;  perform P800-WRITE-DATE;
    DATE-FROM := DATE-TO  ;  DATE-TO := DATE-FROM + 4  ;  perform P800-WRITE-DATE;
    

    While we're at it, another little typing-aid is the continuation character "-", originally conceived for continuing literals on the next line, but can also be used to increase readability:

    define data
        local 1     DATE-A8 (a8)
    end-define
    
        DATE-A8 := '20211231'
    
        DATE-A8 := '2021' - '12' - '31'
    
        DATE-A8 := '2021'
                -  '12'
                -  '31'
    END
    

    I hope you found that useful.