Discussion
· 7 hr ago

Quit or Return?

Hi developers!

I'm testing vibecoding with ObjectScript and my silicon friend created a code-block that got me thinking "what's wrong"?

Here is the piece of code:

for i=0:1:(json.%Size()-1) {

set p = json.%Get(i)

if (p="value1")!(p="value2") {

quit 1
}

 

AI wanted to quit from a method with a return value. Good intention, but bad use of the command.

And ObjectScript compiler compiles this code with no error(?) (syntax linter in VSCode says it's a syntax, kudos @Brett Saviano ).

But in action, it produces <COMMAND>, of course.

Wanted to share with you as this is a good case where return command is better than quit, so if we change the snippet to use return, everything will be fine:

for i=0:1:(json.%Size()-1) {

set p = json.%Get(i)

if (p="value1")!(p="value2") {

return 1
}

So I told my AI-companion to go with return further while returning method results.

Do you use return? Or quit? 

Discussion (7)3
Log in or sign up to continue

QUIT and RETURN are both commands used to terminate execution in ObjectScript code. However, they behave differently depending on their context:

  1. QUIT Command:

    • It exits only the current structure and continues execution outside of it. For example, when used within a FOR or WHILE loop, QUIT will exit the loop and continue with the code after the block.
    • When issued outside of a structure or within an IF, ELSEIF, or ELSE block, QUIT exits the current routine and returns to the caller with or without arguments. With an argument, it provides a return value [1].
  2. RETURN Command:

    • It directly exits the entire routine or method, regardless of whether it's within a loop or a block structure, and returns control to the calling context or ends the program.
    • RETURN is particularly useful for readability when you intend to terminate execution and return a value from methods or nested structures [1][2].

In your example:

  • The syntax is valid because ObjectScript allows both commands in these scenarios.
  • The compiler does not return an error for the QUIT usage as it exits the routine correctly.
  • To align with your intention of explicitly returning a method result, the RETURN command would be clearer and preferable over QUIT [1][2].

Sources:

Whether I use Quit or Return depends on what I'm trying to do. If the aim is to just terminate a method and/or return a value then I go for Return. But if I'm within a code block that I'm trying to escape out of, then I use Quit (as long as that block isn't an if/else statement then I need to resort to trickery to get out of it without ending the routine)

Quit also has a neat feature when used to debug from the terminal in that it allows you to go back up the call stack by giving it a numeric argument.

Hi Evgeny,

In ISOS, the "quit" command is inherited from MUMPS ("Q[UIT]") and has two forms (see the documentation for details) : 

  • with arguments : terminates a method, function or routine, returning it's arguments as value
  • without arguments : exits the current context (e.g. for, while, ...)

The "return" command was introduced by InterSystems, I think in Caché 2016.2  (@all please, correct me if I'm wrong).  It was added to avoid confusion, making ISOS clearer and also more aligned with 'modern' programming conventions.

"return" is equivalent to "quit" with argument, thus is has only one form and one, clear meaning : returning a value.

When the code intent is to return a value, nowadays I favor using "return".

p.s. IMHO, generative AIs, while useful in some contexts, are nor artificial, because they are trained with human sourced corpus, nor intelligent, because they are not able of any real creativity and are not doing any reasoning (even if they can mimic both rather well) 😇

Thank you, @Robert Barbiaux ! Love the historic review! It's great to see you find the utility for return command.

p.s. IMHO, generative AIs, while useful in some contexts, are nor artificial, because they are trained with human sourced corpus, nor intelligent, because they are not able of any real creativity and are not doing any reasoning (even if they can mimic both rather well) 😇

Agree! In fact humans can mimic the reasoning perfectly too :)