A coding 101 question

Hypothetically, in a method I have code that flows as follows…

CodeBlock1
ProcessCode1
CodeBlock2
ProcessCode1
CodeBlock3
ProcessCode1
CodeBlock4

ProcessCode1 is code that is only needed in this method - so I prefer not to create another method for it. Rather, I would like to embed a subroutine in the method called ProcessCode1 and just reference that.

I thought Sub would do this, but that is for XojoScript - which I am unfamiliar with, and I think is not applicable here.

Is there a way to embed a reusable piece of code inside a method?

[quote=121241:@Mark Pastor]Hypothetically, in a method I have code that flows as follows…

CodeBlock1
ProcessCode1
CodeBlock2
ProcessCode1
CodeBlock3
ProcessCode1
CodeBlock4

ProcessCode1 is code that is only needed in this method - so I prefer not to create another method for it. Rather, I would like to embed a subroutine in the method called ProcessCode1 and just reference that.

I thought Sub would do this, but that is for XojoScript - which I am unfamiliar with, and I think is not applicable here.

Is there a way to embed a reusable piece of code inside a method?[/quote]

What you wish for seems to be similar to what was GOSUB subroutines in ancient ages. Does not exist in Xojo. What is wrong with a method ?

In Xojo there is not a way to define an embedded method as there are in some other languages. It’s rather nice, but Xojo does not have the capability. You need to create a method and pass in any state variables necessary.

The situation I have includes a couple such ProcessCodeX sections - so if I create methods for each, then I will be adding two methods to my list of methods. Then if I have another situation in the same program where I want to do something similar, I will be adding another couple methods for that other portion of code.

It just feels like I am cluttering my environment with methods that are external to code, where they really aren’t needed external to that code.

Sort of a cleanliness thing - if nothing else.

Mark, maybe if you can post something more concrete one can give ideas on how to better handle the situation. Generally many small methods are much better than a single large one though.

[quote=121261:@Mark Pastor]The situation I have includes a couple such ProcessCodeX sections - so if I create methods for each, then I will be adding two methods to my list of methods. Then if I have another situation in the same program where I want to do something similar, I will be adding another couple methods for that other portion of code.

It just feels like I am cluttering my environment with methods that are external to code, where they really aren’t needed external to that code.

Sort of a cleanliness thing - if nothing else.[/quote]

You need only one ProcessCode1 as I read it. Why not do :

CodeBlock1 call ProcessCode1() CodeBlock2 call ProcessCode1() CodeBlock3 call ProcessCode1() CodeBlock4

Just set ProcessCode1’s scope to private in the module or class you define it.

Oh ; I forgot : I hear the screams of horror, but cannot resist the pleasure to trigger them :
Goto can be used to create subroutines :wink:

Before I embarrass myself further, I will take a closer look at the structure of my code and see if indeed I can restructure for a better flow. If I conclude my code is nice and clean, and still have structural concerns, I will post it.

Goto…ARGHHHHHHHHHHHHHHHHHHHHHHHHH!
:slight_smile:

[quote=121264:@Michel Bujardet]You need only one ProcessCode1 as I read it. Why not do :

CodeBlock1 call ProcessCode1() CodeBlock2 call ProcessCode1() CodeBlock3 call ProcessCode1() CodeBlock4[/quote]

That will work, but as I mentioned, I have two Processes to start with, so ProcessCode1() and ProcessCode2()… and if similar situations later there will be ProcessCode3(), ProcessCode4(), …

Was just looking for other options to keep things clean.

You have to have pretty thick skin for this crowd!

You should explain better what you are trying to achieve here. For example what do you mean by:

I just mean that if the code structure is legitimate and I want to do something like that again somewhere else in my app - but different portion of the app, then I may get in the habit of creating a bunch more methods that are only needed in private code settings, but are cluttering the method list.

Why don’t you use an if statement to only call the parts which you require?
You could also put your numerous methods into a folder in the navigator, in order to reduce clutter?

Sorry, I don’t get what your problem is here. You can’t make the method list shorter, if you need these methods in your app. In the end it is a question of the scope. Create them in the module or class you need them, and make them private.

[quote=121264:@Michel Bujardet]You need only one ProcessCode1 as I read it. Why not do :

CodeBlock1 call ProcessCode1() CodeBlock2 call ProcessCode1() CodeBlock3 call ProcessCode1() CodeBlock4[/quote]

OK - further self incrimination…

You did give me the idea of something like this…

CodeBlock1
call ProcessCodeX(option1)
CodeBlock2
call ProcessCodeX(option2)
CodeBlock3
call ProcessCodeX(option3)
...

So then, yes, only one ProcessCodeX method is required, and I can just put the code snippets in a conditional path based on the option variable that is passed.

[quote=121274:@Mark Pastor]That will work, but as I mentioned, I have two Processes to start with, so ProcessCode1() and ProcessCode2()… and if similar situations later there will be ProcessCode3(), ProcessCode4(), …

Was just looking for other options to keep things clean.[/quote]

If indeed the nature of ProcessCode is changing enough to grant different kinds of it, you cannot reuse code and better enter it within your method as such. Separate methods, just as well as subroutines, should be used when the process remains the same, and variables (parameters) vary.

The mere notion of reusability is more important than brievity of code.

[quote=121286:@Mark Pastor]OK - further self incrimination…

You did give me the idea of something like this…

CodeBlock1
call ProcessCodeX(option1)
CodeBlock2
call ProcessCodeX(option2)
CodeBlock3
call ProcessCodeX(option3)
...

So then, yes, only one ProcessCodeX method is required, and I can just put the code snippets in a conditional path based on the option variable that is passed.[/quote]

That is exactly the spirit of what methods are for : instead of a collection of methods, you have only one and calling parameters change. Then it becomes reusable, which is the idea behind OOP.

[quote=121287:@Michel Bujardet]If indeed the nature of ProcessCode is changing enough to grant different kinds of it, you cannot reuse code and better enter it within your method as such. Separate methods, just as well as subroutines, should be used when the process remains the same, and variables (parameters) vary.

The mere notion of reusability is more important than brievity of code.[/quote]
Pretty much what I have today - just wondering if there was something more elegant to draw from.