Recent

Author Topic: basic question for good style  (Read 1030 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1068
basic question for good style
« on: March 26, 2025, 11:44:17 am »
As I decided it makes sense, to have a special unit only for checking routines, I started to work on something like this below.

My question about:
TPruefe = class(TObject) : Is TObject the best choice?


For non-German-Speakers: "Prüfe" means "check".
So I can say to my computer: "mystring:=Check.string();"

This is how the code will look alike sooner or later:


Code: Pascal  [Select][+][-]
  1. type TPruefe = class(TObject)
  2.  
  3.   // Prüft und korrigiert Strings, die in tbEinkommen geschrieben werden sollen
  4.   function PruefeString(Eingabestring, Hinweis: string; ErlaubteLaenge: Integer): string;
  5.  
  6.   function Datum(DatumAlsString, Hinweis: string; var NeuesDatum: TDateTime;
  7.     Leerpruefung: Boolean=True): Boolean;
  8.  
  9.   function DoubleWert(Eingabestring, Hinweis: string; MaxDezimalstellen: Integer): Double;
  10.  
  11.   // Prüft und korrigiert Integer-Werte: Input: string, Output: die richtige Integer oder -1
  12.   // z.B.:  Erfolg := TPruefe.Integerzahl('12345', 'Alter', 100);
  13.   function Integerzahl(eingabestring, Hinweis: string; MaxWert: Integer): Integer;
  14.   end;
  15.  
  16. Var: Pruefe: TPruefe;    
  17.  


Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: basic question for good style
« Reply #1 on: March 26, 2025, 12:01:15 pm »
If it is only about
Code: Pascal  [Select][+][-]
  1. // you propose
  2. TMyclass = class(TObject);
  3. // I write
  4. TMyClass = class;
That is because for the root object TObject is always implied.
For any higher derived classes, though, you would always want to write
Code: Pascal  [Select][+][-]
  1. TMyClass = class(TMyCustomClassToInheritFrom);
In your case I suggest
Code: Pascal  [Select][+][-]
  1. TMyClass = class;
Or even make the methods all class methods, so you do not need to instantiate the class.
« Last Edit: March 26, 2025, 12:07:24 pm by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Warfley

  • Hero Member
  • *****
  • Posts: 1872
Re: basic question for good style
« Reply #2 on: March 26, 2025, 04:23:51 pm »
Why do you use a class to begin with? If it's just a collection of functions, just make them functions. No need for a class if you don't manage state.

Nicole

  • Hero Member
  • *****
  • Posts: 1068
Re: basic question for good style
« Reply #3 on: March 26, 2025, 04:33:27 pm »
.... because I cannot remember the names of the functions.
When I need a check, I have no idea, where to find the specific one. So I make a class, call it "check" and write "check." This makes the editor offer me, which checks there are available.

As I name them in German, there is no problem with reserved words. My computer understands me, if I say in German to it: check.stringForBeingAValidDate.

Yes, this can be done by the Try-library as well. But mine have input fields for correcting them.

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Re: basic question for good style
« Reply #4 on: March 26, 2025, 04:41:16 pm »
Well, then use a record, not a class, or a separate unit.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Nicole

  • Hero Member
  • *****
  • Posts: 1068
Re: basic question for good style
« Reply #5 on: March 26, 2025, 04:49:36 pm »
 How to put functions into a record?
and: is there a disadvantage to use a class?

BrunoK

  • Hero Member
  • *****
  • Posts: 696
  • Retired programmer
Re: basic question for good style
« Reply #6 on: March 26, 2025, 05:09:36 pm »
Create a unit named Pruefe like :
Code: Pascal  [Select][+][-]
  1. unit Pruefe;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.   // type TPruefe = class(TObject)
  10.  
  11. // Prüft und korrigiert Strings, die in tbEinkommen geschrieben werden sollen
  12. function PruefeString(Eingabestring, Hinweis: string;
  13.   ErlaubteLaenge: integer): string;
  14.  
  15. function Datum(DatumAlsString, Hinweis: string; var NeuesDatum: TDateTime;
  16.   Leerpruefung: boolean = True): boolean;
  17.  
  18. function DoubleWert(Eingabestring, Hinweis: string;
  19.   MaxDezimalstellen: integer): double;
  20.  
  21. // Prüft und korrigiert Integer-Werte: Input: string, Output: die richtige Integer oder -1
  22. // z.B.:  Erfolg := TPruefe.Integerzahl('12345', 'Alter', 100);
  23. function Integerzahl(eingabestring, Hinweis: string; MaxWert: integer): integer;
  24. //  end;
  25.  
  26. implementation
  27.   { Implementations of
  28.     pruefe functions }
  29. end.
Add it to the uses clause and then you  type Prue -> will suggest Pruefe unit.
Then press "." and code tools will propose the various functions.

BrunoK

  • Hero Member
  • *****
  • Posts: 696
  • Retired programmer
Re: basic question for good style
« Reply #7 on: March 26, 2025, 05:12:27 pm »
And of course, you could define a class TPruefe and put only class methods / functions in it, thus no need to instanciate a TPruefe instance.

Thaddy

  • Hero Member
  • *****
  • Posts: 16783
  • Ceterum censeo Trump esse delendam
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Nicole

  • Hero Member
  • *****
  • Posts: 1068
Re: basic question for good style
« Reply #9 on: March 26, 2025, 06:42:17 pm »
(TObjekt)
is removed already.  ;)

Warfley

  • Hero Member
  • *****
  • Posts: 1872
Re: basic question for good style
« Reply #10 on: March 27, 2025, 12:34:17 am »
.... because I cannot remember the names of the functions.
When I need a check, I have no idea, where to find the specific one. So I make a class, call it "check" and write "check." This makes the editor offer me, which checks there are available.
Auto complete also works for functions, you don't need to write classname.xxx for the window to pop up you can just press Ctrl+space and it pops up anywhere and you can type parts of the function name and it will show them to you.

Also you don't need classes for namespacing, units also provide a namespace. If you have all your functions in MyUnit you can just type "MyUnit." And auto complete will show you all the functions defined in that unit.

Using a class is just wholly unnecessary if all you want is namespaces and auto complete

anse

  • New Member
  • *
  • Posts: 10
Re: basic question for good style
« Reply #11 on: March 27, 2025, 09:10:28 am »
Exactly, unit names work perfect with the completion proposal: "Pruefe."

I also highly recommend not to use other than English names for units, functions, procedures and anything you write. I'm myself located in Germany, but that was one of the very first lessons to learn for me. Not only because you cannot use umlauts like äöüß, but also for a better code readability, especially when at some point some other will have to read and/or extend your code. Pure English even helps when you post code in a forum :)

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: basic question for good style
« Reply #12 on: March 27, 2025, 09:22:47 am »
How to put functions into a record?
https://wiki.freepascal.org/Record/de
Scroll down to "Erweiterte Record"
Important: {$modeswitch advancedrecords}

Quote
and: is there a disadvantage to use a class?
As always in life: Depends.....
A class always has to be created (except all members of that class ar Class vars, class Procedures etc.) and has to be freed.
A record can just be "used" as long as you have a variable pointing to it
NotaBene: Depending what you do with it, a record might still need memory-alloc and dealloc ("New" and "Dispose")
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4296
Re: basic question for good style
« Reply #13 on: March 27, 2025, 09:29:14 am »
Not only because you cannot use umlauts like äöüß, ...
note discussion at issue #40933

Quote
... but also for a better code readability, especially when at some point some other will have to read and/or extend your code. Pure English even helps when you post code in a forum :)
I agree with that part :)
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2086
    • http://www.cdbc.dk
Re: basic question for good style
« Reply #14 on: March 27, 2025, 09:50:53 am »
Hi
You use a record  like this:
Code: Pascal  [Select][+][-]
  1. unit prueferec;
  2. {$mode ObjFPC}{$H+}
  3. {$modeswitch advancedrecords}
  4.  
  5. interface
  6. uses classes, sysutils;
  7.  
  8. type TPruefe = record /// also here you can do 'Ctrl + shift + C' ///
  9.  
  10.   // Prüft und korrigiert Strings, die in tbEinkommen geschrieben werden sollen
  11.   function PruefeString(Eingabestring, Hinweis: string; ErlaubteLaenge: Integer): string;
  12.  
  13.   function Datum(DatumAlsString, Hinweis: string; var NeuesDatum: TDateTime;
  14.     Leerpruefung: Boolean=True): Boolean;
  15.  
  16.   function DoubleWert(Eingabestring, Hinweis: string; MaxDezimalstellen: Integer): Double;
  17.  
  18.   // Prüft und korrigiert Integer-Werte: Input: string, Output: die richtige Integer oder -1
  19.   // z.B.:  Erfolg := TPruefe.Integerzahl('12345', 'Alter', 100);
  20.   function Integerzahl(eingabestring, Hinweis: string; MaxWert: Integer): Integer;
  21.   end;
  22.  
  23. Var: Pruefe: TPruefe;    
  24.  
  25. implementation
  26.  
  27. /// implementation code goes here, just like a class' would ///
  28.  
  29. end.
  30.  
It's not much different from a class, apart from no inheritance.
Have fun coding...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018