Call a block chunk of code instead of duplicating?

Started by Yellownakji, January 25, 2019, 09:05:51

Previous topic - Next topic

Yellownakji

Hello, In my little example project i have a block of code that's present in 5 if/else clauses all in the same function.   Is there a way i can just store that block ONCE and then call that block in each segment or should i just leave it duplicated.  Kind of ugly but i'm curious.   I'm just trying to learn shortening techniques.

TomToad

The simplest solution would be to put that block in its own function and call it when you need it.  Sometimes that might not be viable, such as when the block of code is modifying several variables.

If using a function is not viable, you can sometimes restructure your code to eliminate the duplication.  For example
pseudocode:
If Cond1
   BlockA{}
   BlockB{}
ElseIf Cond2
   BlockA{}
   BlockC{}
ElseIf Cond3
   BlockA{}
   BlockD{}
ElseIf Cond4
   BlockE{}
EndIf

In this example, you can't just simply put BlockA{} outside the If/EndIf as it shouldn't be executed in the case that Cond4 is true, but a little restructuring fixes that problem.

If Not Cond4
    BlockA{}
    If Cond1
        BlockB{}
    ElseIf Cond2
        BlockC{}
    ElseIf Cond3
        BlockD{}
    EndIf
Else
    BlockE{}
EndIf

Sometimes, there is just no way to do things without duplicating code.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Yellownakji

Sigh, i see what you mean.    I wish the "Label" functionality was designed like the Labels in MS-DOS to where you can have a function and create blocks with labels.

For example, i could make a Repeat/Until statement, like i use in my example, but during the finalization of certain clauses, jump out to read a label and then return to position.

It would be handy if we could have sub-functions for functions.  We could just call them blocks.   That would be handy.

I suppose i'm just going to leave the quin-bits of duplicated code because they modify function parameters that get returned.  Oh well, i tried.

Would be cool to see a feature like this in the future, though.   I could go from 160 lines of code to about 70, if i could truncate into a block or utilize labels like DOS does.

Thanks.

Henri

Hi,

Not sure if this is helpful in your case (or if you already knew this), but just to point out that you can modify function params directly using Var after the param declaration without the need to return them.

Pre-OOP I sometimes missed labels and not be able to use Goto, but after that period I just use methods much more, as you don't have to worry about global namespace contamination and duplicate names,  due to a fact that code is already in logical blocks (types).

-Henri
- Got 01100011 problems, but the bit ain't 00000001

fielder

Quote from: Yellownakji on January 25, 2019, 09:05:51
Hello, In my little example project i have a block of code that's present in 5 if/else clauses all in the same function.   Is there a way i can just store that block ONCE and then call that block in each segment or should i just leave it duplicated.  Kind of ugly but i'm curious.   I'm just trying to learn shortening techniques.

emh...

Function BlockOfIfs()
...
...
End function