tcl question: leaving switch clause

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf tcl question: leaving switch clause

  • Creator
    Topic
  • #54062
    Anonymous

      During error investigation the following tcl question came up: Is it allowed in tcl to exit from a switch clause with a return command?

      (Bad) sample:

      proc MyTPS { args } {

         set mode [keylget args MODE]

         switch -exact — $mode {

      start {

      return “” ;# Nothing specific

      }

      run {

      # do something

      return “{CONTINUE $mh}”

      }

      default {

      return “” ;# don’t know what to do

      }

         }

      }

      IMHO any return command from within switch command should omitted, as the switch construct is not finished! It should look like this (good sample):

      proc MyTPS { args } {

         set mode [keylget args MODE]

      set dispList {}

         switch -exact — $mode {

      start {

      # Nothing specific

      }

      run {

      # do something …

      lappend dispList “CONTINUE $mh”

      }

      default {

      ;# don’t know what to do

      }

         }

         return $displist

      }

      Am I wrong, or can the first example create an issue?

      All replies are appreciated.

    Viewing 5 reply threads
    • Author
      Replies
      • #80045
        Steve Carter
        Participant

          As far as I know, you can return from any point within a script.  I know that it’s OK within a switch statement as I do this quite frequently.  

          My style of scripting is to ‘get in and out’ as quickly as possible.  I look at it from an efficiency point of view.  The quicker you can do what you need to do and get out the better.  If you’ve already completed what you need to do or didn’t meet the requirements for an action, why hang around to let the code run to completion when you can pass the message on to the next step?

          Everyone has their own style and that is mine.  This could make for a very interesting conversation.  ðŸ˜‰

          Thanks.

          Steve

        • #80046

          I 100% completely agree with Steve. This is exactly what I teach new developers, explain in Site Evaluations, etc.

          -- Max Drown (Infor)

        • #80047

          Pretty typical tps proc layout …

          proc MyTPS { args } {
          [code]proc MyTPS { args } {

          -- Max Drown (Infor)

        • #80048
          Terry Kellum
          Participant

            Think about volumes of messages as well.  If you have three conditions you are looking at, and you could decide for 90% of the messages with the first condition, place that “stanza” first….  If you put it last, 90% of your messages will go thru unnecessary logic (and time).

          • #80049
            David Barr
            Participant

              Multiple return statements are allowed by the interpreter, so it comes down to programming style and clarity. There’s an interesting discussion of the pros and cons of each style over here:

              http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement

              I tend to use only one return at the end of a TPS proc. If the only disposition that I can return is CONTINUE, then I write it as return “{CONTINUE $mh}”, otherwise I store the disposition in a variable and return “{$disp $mh}”.

            • #80050
              Jeff Dinsmore
              Participant

                My approach is, generally, one way in, one way out.  

                The one exception is for an error condition where it might be expedient to return immediately.

                While it’s “legal” from a syntax viewpoint, it’s much easier – for me anyway – to maintain code that is more structured.  If we return from multiple points in a procedure, it’s more difficult to see what the code is doing.  

                Also, if you return from multiple points in the code, the possibility exists that  you’ll miss a condition and will fail to return anything.  If there’s only one way out of the procedure, you guarantee a predictable return.  With simple code, this is not a practical problem, but as a procedure gets larger and more complex, the chances of invalid return increase.

                The case for returning immediately for efficiency is not a concern if your code is written with a single return in mind.

                In the simple switch example, when a particular case is matched, the others are never executed, so a single return after the switch statement is equivalent to a return in each of the switch cases.

                Jeff Dinsmore
                Chesapeake Regional Healthcare

            Viewing 5 reply threads
            • The forum ‘Cloverleaf’ is closed to new topics and replies.