02 Jun 2006 

DE_flag GB_flag

for english readers

Alles zusammenhalten …

Beginnt man mit 4D, hat man Methoden im Formular, in den Formularobjekten, in globalen Methoden und noch einer globalen Methode und noch einer und ….
Zuerst fängt man an, in den Formularobjekten nur noch Aufrufe von globalen Methoden einzubauen. Das ist effizienter und sehr viel besser zu warten, als über kopieren und einsetzen. Ich für meinen Teil, verwende meist nur eine Methode pro Modul.

Ausgangsbasis

BasisDie Methode bekommt als ersten Parameter einen Text, der benennt, was sie tun soll. Wie sich das gehört, ganz vorne weg die Variablen- und Parameter-Deklaration. Ganz ohne Parameter erlaube ich so, daß der Run-Button ab der 2004 verwendbar bleibt.

Erster Ausbau

Der Selbstaufruf als Prozess-Starter. case2.jpg Ist $what leer wird entweder der Prozess gestartet oder eingeblendet und nach vorne geholt. Das Prozessstarten ruft sich selber auf (Current methode name) und übergibt als Parameter für $what den Wert "StartProcess". case3.jpg

Hier führt es hin

case4.jpgEine fertige und bis auf die oberste Ebene eingeklappte Methode sieht dann so aus:
ohne Parameter wird der Prozess gestartet. Der Prozess öffnet ein Fenster, schließt es wieder und setzt die Prozessvariable auf 0. Andere Abschnitte behandeln die Form-Methode und die durch Buttons angestossenen Aktionen.

Rekursion

Ganz wichtig: der rekursive Aufruf ist ein fest eingebautes Konzept. "newMessage" z.B. ist eine Dienstleistung für mehrere Abschnitte der Methode. case5.jpgDer Selbstaufruf wird z.B. in der On Load-Phase verwendet:

Konzept

Ein weiterer Anlauf das Konzept verständlich zu machen, diesmal graphisch aufbereitet: In der einen Methode läuft alles zusammen, der Start und das Beenden des Moduls – auf Wunsch als eigener Prozess, die Form-Methode mit allen Form-Events, die Button-Aktionen und ggf. der rekursive Aufruf, um Dienstleistungen innerhalb der Methode verwenden zu können und nicht eine eigene Dienstleistungsmethode dazustellen zu müssen. case5.jpg

Prozessvariablen

Ein Nebeneffekt ist, daß die Prozessvariablen – ohne die ein Layout nicht zu gestalten ist – nur in dieser einen Methode leben. Sie werden nur in dieser Methode angelegt, mit Inhalt gefüllt, verwendet und zum Schluß wieder initialisiert (Arrays, Blobs, Bilder, hierarchische Listen, …). Das spart mir Verwaltungs-Aufwand – was ich auf einer Ebene der Methodenstruktur anlege wird am Ende des gleichen Abschnittes wieder gekillt – und damit manche Sorge. Prozessvariablen sind in diesem Vorfahren fast so gut wie lokale Variablen: es gibt sie nur in dieser Methode und am Ende des Prozeßes entfernt sie 4D für mich aus dem Speicher.

Nachtrag

Ohne Pointer auf lokale Variablen ist das System un-realisierbar, also ab 2004.2. Ohne Ein- und Ausklappen der Methoden-Struktur würde ich mir solche Monstermethoden auch nicht antun. Die Anzahl der Parameter reduziert sich auf 3.

DE_flag GB_flag

lieber auf deutsch

This is old 2004-code. Even more generic today. This was written in 2006. That's many years ago and has proven itself very well.

Everything at one place …

4D-beginners like to spread logic all over the place, into forms, into every form-object and a function here and one here and some global methods. That's how the manual and examples suggests. Makes no sense in the long run. It needs to be efficient and one important point of efficiency is maintenance. I'm down to one global method per modul … component-like.

Base

Basis First parameter is of type text, which names what needs to be done. No parameter given means start from the Run-Button or from a menu-command.

Of course, all variables are typed, all! At one place at the very top, so it can be found easily.

Then

The only thing the parameterless call does, is calling itself and start a process or show the process and bring it to the front. case2.jpg If $what is empty New Process calls itself (Current methode name) and sets $what to "StartProcess". case3.jpg

Next

case4.jpg A done modul. Every level closed besides the top one.
No parameter, starts the process. StartProcess opens a window or two, does things and closes the windows. Further down, FormMethod and Button-Actions, and Services.

Recursion

… is important: this is not that kind of bad recursion but a well managed concept. "newMessage" is a service, used for different parts of the method.

Concept

Trying to convince of the concept graphically. There is just one method that handles everything, from start to end, form-events, button-actions, services for the same method and for other processes. The line-count of a method increases moderately, the count of methods decreases dramaticaly. There is no searching across methods, debugging through the levels of code-spread, everything is where it belongs to.

case5.jpg

Processvariables

As a sideeffect, the count of processvariables goes down and they behave like local to the module ones. No more memory-leaks if done properly, which is destroying of potential leaks just a couple of lines down of where it is created. Think of hierarchical lists and never again forget to kill the leaker.

Appendix

Without V12 method-editor remembering collapse- and expand-state it would be less helpful. Read also about standard-count of parameters reduces to 3.