Changes between Version 2 and Version 3 of OpenModelicaStyleGuide


Ignore:
Timestamp:
2015-03-19T15:36:15Z (10 years ago)
Author:
Per Östlund
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OpenModelicaStyleGuide

    v2 v3  
    44    {{{#!mo
    55    // Prefer this.
    6     case DAE.ARRAY(ty = array_ty
     6    case DAE.ARRAY(ty = DAE.T_REAL()
    77
    88    // Not this.
    9     case DAE.ARRAY(array_ty, _, _)
     9    case DAE.ARRAY(DAE.T_REAL(), _, _)
    1010    }}}
    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  
    2011  Use {{{else}}} for catch-all::
    2112    Prefer using {{{else}}} when writing a catch-all case:
    2213    {{{#!mo
    2314    outValue := match(inExp1, inExp2)
    24       local
    25         Integer i1, i2;
    26 
    27       case (DAE.ICONST(integer = i1), DAE.ICONST(integer = i2)) then i1 + i2;
     15      case (DAE.ICONST(), DAE.ICONST()) then inExp1.integer + inExp2.integer;
    2816      else 0;
    2917
     
    5341    end match;
    5442    }}}
     43== Error Handling ==
     44  Use the appropriate method to handle errors::
     45    For errors that are of interest to the user, use one of the Error.addXXXMessage functions to print an error/warning message. Always supply the source info when posssible, i.e. prefer using addSourceMessage or addMultiSourceMessage. For cases where it might be of interest to the developers if a function fails, use failtrace and Debug.traceXXX:
     46    {{{#!mo
     47    if Flags.isSet(Flags.FAILTRACE) then
     48      Debug.traceln("- Foo.bar failed!");
     49    end if;
     50    }}}
     51    For functions that should never fail, use internal errors:
     52    {{{#!mo
     53    function foo
     54      input Integer inN;
     55    algorithm
     56      if inN < 0 then
     57        Error.addInternalError("Negative number given to foo!", sourceInfo());
     58      end if;
     59    end foo;
     60    }}}
     61  Only print internal errors as a last resort::
     62    Internal errors should only be used when something has gone really wrong, and ideally they should never be shown to users. In many cases it's therefore appropriate to check that some other error hasn't already been printed before printing an internal error:
     63    {{{#!mo
     64    function foo
     65      ...
     66    protected
     67      Integer err_count = Error.getNumErrorMessages();
     68    algorithm
     69      try
     70        // Something which can fail, and might or might not print an error message if it does fail.
     71      else
     72        // Only print an internal error if the above code failed without printing an error message.
     73        if err_count == Error.getNumErrorMessages() then
     74          Error.addInternalError(...);
     75        end if;
     76      end try;
     77    end foo;
     78    }}}