Refactoring a button control

In a button control in one of my apps I have 120 of code. Intuitively I know that that this is too many, and practically when I try to make changes If I end up stuffing things up I am in trouble.

I have found that I cannot export a button (to delete then import if I muck up). Am am using a local version of Git, should I start a new feature “fixing submit button mess” and if I get through refactoring OK merge it, or delete it the feature if I get stuck? Should I duplicate the entire app, and reinstate the duplication if things go wrong?

This is an app that is working OK in the wild, so I don’t want to break it, but it really does need some help as I originally wrote it when I didn’t really no what I was doing.

Any insights from experts would be great help.

How large is your app? How urgent is fixing your mess? How good are you with oo programming?

If you can answer these questions then you can decide for yourself how you want to proceed. Once upon a time I did something similar as you and had a big mess of code. I had to completely redo the app. But in those times my app wasn’t that large. So today I do refactoring whenever necessary. But this depends on how solid your foundation of classes is.

Why don’t you simply copy & paste the button? It will have the same code, just a 1 added to the name.

If you mucked up your button then just copy the code in the new button back into the old button.

Alternatively simply copy it to a new project.

Btw I prefer a top down approach when programming.

Write in plain English as comments what you want to do

Put lengthy and/or reusable bits into methods like AskForFileDestination, SaveFile, or UpdateInfo

So my code in a button usually looks like this:

[code]// FileDestination is a property of type folderitem of the Window
If FileDestination = Nil then AskForFileDestination

// SaveFile is a method which saves the document to the specified file destination
SaveFile( FileDestination )

// now update any info as required
UpdateInfo[/code]

[quote=283377:@Beatrix Willius]How large is your app? How urgent is fixing your mess? How good are you with oo programming?

If you can answer these questions then you can decide for yourself how you want to proceed. Once upon a time I did something similar as you and had a big mess of code. I had to completely redo the app. But in those times my app wasn’t that large. So today I do refactoring whenever necessary. But this depends on how solid your foundation of classes is.[/quote]

Thnaks Beatrix. The app itself has turned out to be quite large, for me. But the worst place is in this submit button action event. It is just so easy for me to make a mistake when altering some behaviour, which is why I want to split it up into Methods each method named so I reasonably know what is going on, and may help me change the correct code.

I do have about three months to get this tidied up, but I sure don’t want to stuff it up and make the situation worse than not going anything (it does work currently, until I want to make the next change :slight_smile: ).

OOP is coming on slowly. Until I got back into Xojo it was complete mystery to me. Now I know how to create a Class and Instantiate it; but more importantly, I have found how to store each newly Instantiated Class (the entire secret as far as I can see), I just have to learn where to store those Instantiations, apparently in the Window (or other place) where they are going to be accessed. I usually end up saving them them in Modules because they are Global and easy to access. I mention this because it seems you are suggesting that I use Class Instances instead of Methods - and I haven’t a clue how to do that.

[quote=283382:@Markus Winter]Why don’t you simply copy & paste the button? It will have the same code, just a 1 added to the name.

If you mucked up your button then just copy the code in the new button back into the old button.

Alternatively simply copy it to a new project.[/quote]
Thanks Markus!

Not sure why I didn’t think of this. My only defence is that probably I am overthinking the problem!

You can start by creating a method with your 120 lines of code, instead of leaving the logic in the button. Then all there will remain in the Action event is a call to that method.

Then you can duplicate that method to keep a backup, before you modify it.

Thank you Michel, that sounds like an idea. I will start from that basis (small steps, I guess).

Thanks all for input, I am starting to feel better about this.