# Best practice for early return

I need to (teach students how to) no-op early return out of a custom command block, for a base case of a recursion.

Is it more 'Snappish' (what's the Snap! analogue of 'pythonic'?) to have

if <base condition> {
// nothing
} else {
// do recursive stuff
}


or

if <base condition> {
[report [nothing] ]
} else {
// do recursive stuff
}

Or is there another possibility I'm missing?

I think the Snappish way is to save using up some pixels on the else

That's a good point. In my situation I'll actually have two (or maybe 3) base cases: length=0,1, or 2, and it would be good to avoid the visual clutter of so much nesting

I think a better option would be

Yes, if it were just binary, but I will have at least 2 base conditions to test before going into the main recursive algorithm

FYI I'm doing QuickSort, so base condition one is, is the range empty (do nothing), base condition two is, is the range length 1 (do nothing -- can be combined with previous base), base condition three is, is the range length 2 (just do one comparison and maybe swap); if none of those are early return, then do the whole pivot and recurse thing

If it's a command block, use stop [this block]. If it's a reporter, use report [0] (or report []. If it's a predicate, use report [<false>] (or true`, depending on what you want the default value to be).

Using STOP is definitely the Logo-ish way.

I don't know if we have a preferred way. Different cases are different. If I'm drawing a tree because I really want a tree in some project, I'll do
IF (level > 0) [draw stuff]
but if I'm teaching how to do recursion, I'll do
IF (level = 1) [move forward and back] else [draw stuff]
because I'll previously have defined TREE1, TREE2, etc., where the number is the number of levels of branches, and what goes in the IF branch is the body of TREE1, whereas what goes in the ELSE branch is a generalization of all the other numbered versions.

And of course if you're popping out of several levels at once, the cool kids will use a continuation. :~)

lol when you said "Using STOP is definitely the Logo-ish way", what I read at first was "the Lego-ish way", as if you were referring to ego-lay_atman-bay

In my case it is a command-block, I hadn't thought about stop [this block] (didn't realize that was one of the stop options). I think I like that best.

bh I took a quick gander at continuations in the manual, I'm not cool enough for that!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.