Changes between Initial Version and Version 1 of OpenModelicaStyleGuide


Ignore:
Timestamp:
2014-09-02T10:03:39Z (11 years ago)
Author:
Per Östlund
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OpenModelicaStyleGuide

    v1 v1  
     1== Matching ==
     2  Prefer named arguments::
     3    When matching a uniontype, prefer using named arguments to only match what is used. A match using named arguments only needs to be changed if the matched fields change in a uniontype, while a match using positional arguments will always have to be changed if the uniontype changes.
     4    {{{#!mo
     5    // Prefer this.
     6    case DAE.ARRAY(ty = array_ty) 
     7
     8    // Not this.
     9    case DAE.ARRAY(array_ty, _, _)
     10    }}}
     11    One exception is when matching all fields in a uniontype, doing some changes to them, and then constructing a new instance of the same type. In that case the code will need to be updated if the uniontype changes anyway, so using positional arguments will give cleaner code.
     12    {{{#!mo
     13    case DAE.ARRAY(array_ty, is_scalar, expl)
     14      equation
     15        expl = listRest(expl);
     16      then
     17        DAE.ARRAY(array_ty, is_scalar, expl)
     18    }}}
     19 
     20  Use {{{else}}} for catch-all::
     21    Prefer using {{{else}}} when writing a catch-all case:
     22    {{{#!mo
     23    outValue := match(inExp1, inExp2)
     24      local
     25        Integer i1, i2;
     26
     27      case (DAE.ICONST(integer = i1), DAE.ICONST(integer = i2)) then i1 + i2;
     28      else 0;
     29
     30      // Avoid this:
     31      case (_, _) then 0;
     32      // And this (using 'then' is legal, but unnecessary):
     33      else then 0;
     34    end match;
     35    }}}
     36  Use {{{as}}} only for partial matches::
     37    {{{as}}} is usually not necessary when matching a whole input argument, such as this:
     38    {{{#!mo
     39    _ := match(inExp, inStr)
     40      local
     41        DAE.Exp exp;
     42        String str;
     43
     44      case (exp as DAE.CALL(path = Absyn.IDENT("sum")), _) then ExpressionDump.printExpStr(exp);
     45      case (_, str as "someString") then str;
     46    end match;
     47    }}}
     48    Prefer using the input parameters directly instead:
     49    {{{#!mo
     50    _ := match(inExp, inStr)
     51      case (DAE.CALL(path = Absyn.IDENT("sum")), _) then ExpressionDump.printExpStr(inExp);
     52      case (_, "someString") then inStr;
     53    end match;
     54    }}}