Versions Compared

Key

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

Please Don't consider this document as the final one.Currently I am working on it.
 

This document contains the Code snippet taken from the Commit notification.
Most of tricks on Groovy are taken from the code committed by Scott Gray , Joe Eckard , Marco Risaliti and others.

...

  1. Start the name of groovy file from capital letter and then follow the camel case pattern.For example "PendingCommunications.groovy".
     The reason behind is shown below (Comments from Joe on Developer Mailing List) :-
    • The main reason for this is that when a script is run without a class declaration, the filename is used to create the class name and you can experience problems with untyped variables. For example, a script named product.groovybecomes class "product", and if the script contains an untyped variable "product", it assumes you're trying to access the class "product" instead of a new variable "product".
    • A secondary reason would be consistency, as some scripts have already been named this way. (EditProductFeatures.groovy, etc.
      Info
      titleGroovy Features

      Important thing to note down here is that we can use the Beanshell files as it is in Groovy but we should go with Goodies provided with Groovy Programing language.Here I am including few code snippet that is taken from Beanshell files and its equivalent Groovy code snippet.






  2. Wiki Markup
    *Primary Key Definition*
    *Beanshell :\-*
    \-productAssoc = delegator.findByPrimaryKey("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", productIdTo, "productAssocTypeId", productAssocTypeId, "fromDate", fromDate)*);*{*}Groovy :\-*
    \+productAssoc = delegator.findByPrimaryKey("ProductAssoc", \['productId' : productId, 'productIdTo' : productIdTo, 'productAssocTypeId' : productAssocTypeId, 'fromDate' : fromDate*\]);\*
    \\
    *Note :\-* Removed the usage of UtilMis.toMap(). Instead of findByPrimaryKey try to use findOne()*.*
  3. Null check
    Beanshell :-
      -    if (payment == null) continue;
    Groovy :-
    +    if (!payment) continue;
  4. Null + Size greater then Zero Check
    Beanshell :-
    -        if (glAccounts != null && glAccounts.size() > 0) {
    Groovy :-
    +        if (glAccounts*) {*
  5. One line assignment
    Beanshell :- 
    -nowDate = UtilDateTime.nowDate();
    -context.put("nowDate", nowDate);

          Groovy :-
          +context.nowDate = UtilDateTime.nowDate();

           Note :- Sentence can be kept in single line with the usage of DOT (.) for putting some values in context.

...