How to write code

Hi all

It's a very stupid question but I can't formulate my question (just by looking at the title) in order to find an answer on intermet:

I use the Vba language through the SW API to create an interface so I have a UserForm on which I have buttons. It's in the procedure of the latter that I am used to coding.
But I came to wonder if it wasn't better, clearer, to create several modules with conditional loops etc and then call them in the button code. The goal might be to lighten the code and make it more structured.
In addition, it would perhaps be faster and less complicated for the computer?

I don't know if I've been clear but I'd like some feedback in order to propose the best structure for my project.

Thank you

1 Like

Good evening

For my part, the userform only has internal codes to interact directly on the layout or combine actions in the userform.

The buttons directly calling modules in my main code.

For the processing time, I'm not sure that it fundamentally changes things unless the code has to go back and forth between the userform and the main code.

I will try to answer in order:

But I came to wonder if it wasn't better, clearer, to create several modules with conditional loops etc and then call them in the button code

Yes and No (it's a good start, huh?)

Yes, everything that is reused in the running of a program must be placed in a public module.

Yes, any portion of code that is reused in a class must be factored into a procedure or function.

No, it's not optimized to use modules to put all the loops or parts of code that need to be called into them.

The goal might be to lighten the code and make it more structured

For the code to be lightened, if you respect the "Yes" points of my previous answer, you will have already done 80% of the work.

For the remaining 20%, it's the comments that make them. An uncommented code is a poorly made code.

If you or someone else goes back to the code and they have to type 5000 lines of code just to understand where it gets stuck, it will take them 3 days (3 days to understand the code and 15 seconds to put a period where it was missing)

In addition, it would perhaps be faster and less complicated for the computer?

No

To optimize code (on vba, it's not very useful given the heaviness of this code), you have to start by limiting the variables or using them again.

You have to type (give a type) the variables so that they have the same type as their future value

You should not use the "Object" type and limit the "Variant" type .

You have to create classes for complex objects instead of using the Variant or Object type.

You should use the "With" keyword when you have several properties to assign to an object:

Dim MaVoiture As Voiture

With MaVoiture
.Couleur="Bleu"
.Moteur="V16 Twin Turbo 12l " 'HOOOO C'EST BEAU DE REVER
End With

It's also cleaner.

Use Arrays for Object Collections

There are still many but there is no point in continuing.

Below 10000 lines of code, you will save only a few milliseconds:)

1 Like

As usual Yves.T, your answers are complete and clear, thank you very much!

Have a nice day!