Expressions

  • Do not use scientific notation
  • Do not omit the leading 0 of float values.
  • When initializing float dictionary keys with integer values, write .0 e.g, beam.length = 0.0
  • Use spaces around binary operators: * / % + - ** & | exor = < > <> # <= >=
    a = (b + c % d) * e
  • Do not use space after a unary operator -. Do not use + as a unary operator; use a space instead for aligning coordinates in a row.
    line2  x, y,
          -y, x
  • Use space after , but not before.
  • Use tab(s) before and space after !
    add2 a, b       ! top-right corner
  • Use tab(s) before logical operators in multi-line conditions:
    if  iShape = SHAPE_RECT      |\
        iShape = SHAPE_ELLIPSOID |\
        ...
  • Use space outside () and [] only when other operators require it.
  • Do not use space before ( of function calls or before dimension [.
  • Do not use space immediately after ( or [ to make searching easier.
    _from = _iPoint
    _to   = (_iPoint % nPoints) + 1               ! connect end to start
    line2 _points[_from][1], _points[_from][2],
          _points[_to  ][1], _points[_to  ][2]
  • Some operators have two forms; use only these:
    % instead of mod
    ** instead of ^ or a * a
    & instead of and
    | instead of or
    exor instead of @
    <> instead of # when comparing float values
    # instead of <> when comparing integer or string values
  • Do not use = <> <= >= with float values. Use a constant EPS inaccuracy instead. Graphisoft’s standard EPS values:
    • 0.0001 for lengths (1/10 mm)
    • 0.0001 **2 for scalar products
    • acs(1 - 0.0001 **2) for angles (0.0081°)
  • When comparing with constants, (e.g. i = 5) the constant should be the second operand.
  • Do not use # < > <= >= operators on integer options, unless checking an option that can be interpreted as boolean (e.g. iLeg # LEG_NONE). This makes understanding and extending the options easier.
  • When assigning values to bool variables the logical expression should be parenthesized:
    bBoolValue = (i > j)
  • Do not use the bool result of logical negation of integer values or variables. E.g. instead of not(iIntVal) please use iIntVal = 0.
    Of course, bool variables and expressions can be negated, e.g. not(bBoolVal).
  • Do not compare bool variables and expressions to true or false; use the value of bool or its negated value:
    if bBoolVal then ...       ! instead of: if bBoolVal = 1
    if not(bBoolVal) then ...  ! instead of: if bBoolVal = 0
  • Complex expressions (e.g. where and and or are both present) should be parenthesized to clarify precedence.
  • Put parentheses around rarely used operator combinations.
  • Logical expressions involving many options of the same variable should be split into multiple lines:
    if   iLeafType = TYPE_A |\
         iLeafType = TYPE_C |\
         iLeafType = TYPE_D |\
    ...
  • Logical expressions consisting of many independent parts should be split into many logical expressions using descriptive variable names.
  • Splitting long expressions with multiple \ ‘s, make clear the precedence of the sub-expressions with parentheses and by keeping them in the same line:
    _specialCase =  (iSomeOption = SOME_SIMPLE |\
                    (iSomeOption = SOME_MEDIUM & iOtherOption = OTHER_SIMPLE))
  • Split lines only after operators.