Question
· Jun 5, 2018

$CASE For Long Stories

Hi, Community!

$CASE is a fine syntax sugar for "Ifs" with one-line/one-word expressions, like in docs:

SET daynum=$ZDATE($HOROLOG,10)
  WRITE $CASE(daynum,
              1:"Monday",2:"Tuesday",3:"Wednesday",
              4:"Thursday",5:"Friday",
              6:"Saturday",0:"Sunday",:"entry error")

But if my expression for a current case is a multi-line business logic? Can I use "{}" somehow or better go with "IF" instead?

Discussion (13)2
Log in or sign up to continue

Hi in this case I have already used a function that returns a string, with this I came to a utility very similar to the switch case of other languages Sorry for the possible inconsistency in the response, it was translated by google translator

Example:

Class Case.Teste Extends %SerialObject
{

ClassMethod Teste(pReq As %Integer)
{
WRITE $CASE(pReq,
              1:..Oi(),2:..Tchau(),: ..GOKU())
}

ClassMethod Oi() As %String
{
Quit "OI sou do Brasil"
}

ClassMethod Tchau() As %String
{
Quit "Um abraço"
}

ClassMethod GOKU() As %String
{
Quit "Oi Eu sou o GOKU"
}

Storage Default
{
<StreamLocation>^Case.TesteS</StreamLocation>
<Type>%Library.CacheSerialState</Type>
}

}

USER>do ##Class(Case.Teste).Teste(1)
OI sou do Brasil


USER>do ##Class(Case.Teste).Teste(2)
Um abraço


USER>do ##Class(Case.Teste).Teste(3)
Oi Eu sou o GOKU


USER>

Hi, Ed!

Thanks! 

I'll elaborate the problem a bit. Here is the $case everybody loves:

write $case(condition, 1:expression1, 2:expression2, expression3)

My problem is that with business logic change the expression1 and expression2 showed the need for multiline logic. And I was looking for something like:

write $case(condition,

1: {

New cool logic for expression 1 with line 1
and line 2


},

2:{

Perfect, better logic for expression2
maybe with 
few lines
more

},

expression3)

I'd go with if, but you can also resolve case expression beforehand:

set condition = .... several lines of logic...
write $case(condition, ...)

And condition then can be separaten into several different lines.

I'm against method chaining in general and several functions on one line/step in particular.

Each line should contain one small operation.

Choose to your taste:

Class dc.test Abstract ]
{

ClassMethod Public() [ InternalPrivateProcedureBlock = 0 ]
{
  q
Choice0()
  "Sunday"
Choice1()
  "Monday"
Choice2()
  "Tuesday"
Choice3()
  "Wednesday"
Choice4()
  "Thursday"
Choice5()
  "Friday"
Choice6()
  "Saturday"
Choice()
  set a="entry " _"error"
  return a
}

/// d ##class(dc.test).Test()
ClassMethod Test()
{
  daynum=$zd($h,10)

  "1) ",$case(daynum,
                0:$$Choice0,
                1:$$Choice1,
                2:$$Choice2,
                3:$$Choice3,
                4:$$Choice4,
                5:$$Choice5,
                6:$$Choice6,
                :$$Choice),!

  "2) ",@("$$Choice"_$case(daynum,
                              0:0,
                              1:1,
                              2:2,
                              3:3,
                              4:4,
                              5:5,
                              6:6,
                              :"")),!

  daynum="-"
  
  "3) ",$case(daynum,
                0:"Sunday",
                1:"Monday",
                2:"Tuesday",
                3:"Wednesday",
                4:"Thursday",
                5:"Friday",
                6:"Saturday",
                :$xecute("()"_
                          " set a=""entry "" _""error"""_
                          " return a")),!
}

}