Automation

last person joined: 2 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  jcs:empty question

    Posted 04-02-2012 07:00

    Hi All

     

    Should be a silly question, but anyway. I'm trying to understand the need of

    using jcs:empty() function. What is the difference between the following code blocks

     

    if ($a) { ... }

     

    if (not(jcs:empty($a)) ) { ... }

     

    It looks like $a will be converted to false anyway (if it is empty).

    So is there a case when jcs:empty is really needed?



  • 2.  RE: jcs:empty question
    Best Answer

    Posted 04-02-2012 09:55

    Well, you could summarize this as follows:

     

    if( $a == true() )

     

    versus:

     

    if( not( jcs:empty( $a ) ) )

     

    So the first one tests if the argument is true, while the second one checks if the argument is empty. These two checks are identical for strings and node-sets because their boolean test consists of checking if they are empty or not; however, these two tests are not equivalent for numbers or booleans since those two data types can never be empty but they can be false.

     

    For example:

     

    var $a = 0;

     

    if( $a )

     

    Is not the same as:

     

    if( not( jcs:empty( $a ) ) )

     

    Because a number is never empty, but a number with a value of 0 is considered to be false.

     

    As far as why jcs:empty() is needed, you're correct that you can achieve the same result using other means, but the advantage of jcs:empty() is it makes it very clear what you are testing for, whereas node-set boolean tests might not be as obvious to script writers with less experience.



  • 3.  RE: jcs:empty question

    Posted 04-02-2012 13:09

    Hi Curtis,

     

    Very good explanation, thank you very much!