Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Making a case for case


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: Making a case for case
Date   Tue, 06 Feb 2007 17:21:01 -0600

David Elliott <[email protected]> wrote about nested braces, 

>       if strlen(trim("`exp'"))<6 {  // Too short text field
>               local error 1
>               local type "unkn"
>               }
>               else { //#1 (counter for number of close braces needed)
>
>               ...a lot of additional code...
>
>       // 2digit year leading separated
>       if regexm("`exp'","([0-9][0-9])[-,./\ ]+([0-9][0-9]?)[-,./\
>            ]+([0-9][0-9]?)")  {
>               local type y2/m/d
>               local order 20ymd
>               local error 7
>               }
>       else { //#8
>       // 0 to O substitution
>       if regexm("`exp'","[O]") {
>               local error 2
>               }
>       else { //#9
>               // Nothing matched
>               local error 3
>               }
>               }
>               }
>               }
>               }
>               }
>               }
>               }
>               } // close all 9 elses

and made a case for a -case- statement.  Nick Cox <[email protected]>
made a point about indentation:

> I would lay out like this:
> 
>         if #1 {
>                 something
>         }
>         else {
>               if #2 {
>                       something else
>               }
>               else {...

I would like to remind both of you that Stata allows -else if-:

        if #1 {
                something 
        }
        else if #2 {
                something else
        }
        else if #3 { 
                something else again
        }
        ...
        else {
                default case
        }

-else if- is not, appearance wise, very different from case:

        docase {
                case #1 {
                        something
                         }
                case #2 {
                        something else
                        }
                otherwise {
                        and something else again
                        }
        }

In fact, the -else if- way of coding requires less identation!

The advantage of a -case- statement has to do with constants and having a
compiler that build efficient branch tables.  That is, in "case #1" above, you
might be imagining "x==y" or det(x)!=0 or some other expression.  That,
however, does away with all the efficiency that -case- can bring to the table.
What you should be imagining is something like

        docase (x) {
                case 1  {
                        something
                        }
                ...
        }

-case 1- code would be done when x==1.  Nice, simple, and constant.  The
advantage of these constants is that a compiler can build a table of of where
to go.  Think of a vector with (address of routine for 1, address of routine
for 2, ...).  Then vector[i] is where to branch, and the compiler just grabs
the element and off it goes.  Importantly, the speed of the branch is no
longer a function of the number of alternatives, or even where in the list the
alternative appears.

Anyway, a case can be made for -case- in Mata.  For Stata, being an
interpreter, the case can only be made on the basis of style and, as I've
said, -else if- is a good alternative, stylewise.

I can tell you that -case- is on the list for Mata, but not high on the list.
In my experience in statistical programming, constant branch tables do not
arise often.

Am I missing something?

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index