July 21, 2019, 04:21:07 AM

Author Topic: Call a block chunk of code instead of duplicating?  (Read 294 times)

Offline Yellownakji

  • Full Member
  • ***
  • Posts: 243
  • Whoop-de-doo-ba-dooo!
Call a block chunk of code instead of duplicating?
« on: January 25, 2019, 09:05:51 AM »
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.

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 385
Re: Call a block chunk of code instead of duplicating?
« Reply #1 on: January 25, 2019, 10:10:17 AM »
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:
Code: [Select]
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.
Code: [Select]
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.

Offline Yellownakji

  • Full Member
  • ***
  • Posts: 243
  • Whoop-de-doo-ba-dooo!
Re: Call a block chunk of code instead of duplicating?
« Reply #2 on: January 25, 2019, 10:30:03 AM »
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.

Offline Henri

  • Full Member
  • ***
  • Posts: 206
Re: Call a block chunk of code instead of duplicating?
« Reply #3 on: January 25, 2019, 11:13:57 AM »
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

Offline fielder

  • Jr. Member
  • **
  • Posts: 75
Re: Call a block chunk of code instead of duplicating?
« Reply #4 on: January 26, 2019, 08:03:36 PM »
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...

Code: [Select]
Function BlockOfIfs()
...
...
End function