Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleExample 2
if(_out.AppleHarvestDate < _toDate('20210101'), 1,
    /* Evaluates to 1 if AppleHarvest Date is Before 1st January 2021*/

0
    /* and to 0 if it is not */

)

Layout for if()

...

if( condition, Statement 1 , // else Statement 2 )

...

Simple

For simple if() statements we recommend the following style of layout:

Note that when a statement in an if() clause is more than 1 line, wrap them in a do(), for example:

Code Block
// Example code layout 
if( condition expression, Statement 1, 
// else 
    Statement 2 
)

...


    do(
        Statement 1,
        Statement 2,
        ... Statement n
    ),
 
// else
    Statement A
)
Note that closing brackets should always line up with the start of the statement (see the do() and if() statements above)

Complex

For more complex if() statements we recommend the following style and that comments are added:


Code Block
// 

...

To make it easier to see that statements are correctly bracketed, the closing bracket should always line up with the start of the statement.

Layout for switch()

Code Block
switch( 
    [ condition expression 1, Statement 1 ], 
    [ condition expression 2, Statement 2 ], 

// default _NULL 
)

...

Short description on what is being achieved when the if statement is true
if ( $foo > 10 &&
     $bar == "X" &&
      ( $baz == "Y" ||
         $qux == "Z" ),
 
            // description of what the statement is doing
            do (
                    Statement 1
            ),
 
// else, description of what the statement is doing
    Statement 2
)


Layout for switch()

Simple

For simple switch statements we recommend the following layout:

Code Block
switch(
    [ condition expression 1, Statement 1 ],
    [ condition expression 2, Statement 2 ],
 
// default
    _NULL
)
 
switch(
    [ $foo > 100, $x = "HIGH" ],
    [ $bar > 50,  $x = "MEDIUM" ],
 
    // default
    $x = "LOW",
)

Complex

For switch statements that perform more advanced activities we recommend the following layout:


Code Block
switch (
      // description of what the statement is doing
      [$foo == 100,
           $x = "HIGH"
      ],
 
      // description of what the statement is doing
      [$bar == 50,
           do (
                 $x = "Medium",
                 $x = toUpper($x)
           )
      ]
       
      // default
      do (
           $x = "LOW"
      )
)