Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How / if to refactor a Delphi program using only forms and data modules

Tags:

After years of coding Delphi programs as untestable code in forms and datamodules, including global variables, and the only classes are the forms themselves, containing all the code I need for the form UI itself.

How would I convert the code to a set of classes that do the actual work? would I need to stop using the datasources/datasets and do everything in classes? do I need an ORM?

There's usually zero need for reuse of the code in the forms, so does it make sense to convert the logic to classes?

like image 339
Osama Al-Maadeed Avatar asked Feb 14 '09 17:02

Osama Al-Maadeed


1 Answers

If I encounter a form (or other class) with too much responsibility, I usualy follow the pattern below:

  1. Define a new class for the logic.
  2. Create a member variable of the new class in the form.
  3. Create the class in the onCreate and free it in the onDestroy of the form.
  4. Move a single piece of logic (for example a variable) to the new class.
  5. Move or create all methods to the new class.
  6. Compile and test.
  7. Continue until all logic is put in the new class.
  8. Try to decouple the logic class from the form class. (You can even work with interfaces if you like).

There are situations where a single class is not enough, so it is no problem to create more classes. And these classes can have other classes to.

With these steps, you can tackle most of these problems.

like image 174
Toon Krijthe Avatar answered Nov 09 '22 03:11

Toon Krijthe