Recent

Author Topic: Code Review: a very basic class  (Read 4552 times)

bulrush

  • Jr. Member
  • **
  • Posts: 86
Code Review: a very basic class
« on: June 29, 2016, 09:02:56 pm »
Would someone mind looking this over? I'm making a very basic class. Do I have all the required parts in the right place?

Code: Pascal  [Select][+][-]
  1. Unit TClass1Unit
  2.  
  3. TClass1 = class(TObject)
  4. {
  5. Properties:
  6. .Num1:integer
  7. .Num2:integer
  8.  
  9. Methods:
  10. Sum(x,y) //Sums properties .Num1 and .Num2.
  11. }
  12. protected
  13.   Fnum1:integer; //For property "num1"
  14.   Fnum2:integer; //For property "num2"
  15.  
  16.   Constructor Create(AOwner:TObject);
  17.   destructor Destroy; override;
  18.  
  19.   procedure Setnum1(tmp:integer); //property "num1"
  20.   procedure Setnum2(tmp:integer); //property "num2"
  21.  
  22. public
  23.   // Public Methods
  24.     function sum:integer; //Method "sum"
  25.  
  26. published
  27.   //Published Properties
  28.     property num1:integer read Fnum1 write Setnum1;
  29.     property num2:integer read Fnum2 write Setnum2;
  30.  
  31. end; // TClass1
  32.  
  33. procedure Register;
  34.  
  35. //Procedure defines not done yet.
  36.  
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Code Review: a very basic class
« Reply #1 on: June 29, 2016, 09:31:13 pm »
No big remarks. Only the FNum1 etc could have gone in the private part but that's up to you (if you need them protected or private).

I do have a hint. If you would have typed this in the interface part and pressed Ctrl+Shift+C anywhere inside the class-declaration (in Lazarus !!) you would have automatically gotten the implementation part for free :) and the properties would have automatically gotten a Set-function.

Code: Pascal  [Select][+][-]
  1.   TClass1 = class(TObject)
  2.   protected
  3.     constructor Create(AOwner: TObject);
  4.     destructor Destroy; override;
  5.   public
  6.     function Sum: integer;
  7.   published
  8.     property Num1: integer;
  9.     property Num2: integer;
  10.   end;

Ctrl+Shift+C results in:
Code: Pascal  [Select][+][-]
  1.   { TClass1 }
  2.  
  3.   TClass1 = class(TObject)
  4.   private
  5.     FNum1: integer;
  6.     FNum2: integer;
  7.     procedure SetNum1(AValue: integer);
  8.     procedure SetNum2(AValue: integer);
  9.   protected
  10.     constructor Create(AOwner: TObject);
  11.     destructor Destroy; override;
  12.   public
  13.     function Sum: integer;
  14.   published
  15.     property Num1: integer read FNum1 write SetNum1;
  16.     property Num2: integer read FNum2 write SetNum2;
  17.   end;
  18.  
  19. implementation
  20.  
  21. { TClass1 }
  22.  
  23. procedure TClass1.SetNum1(AValue: integer);
  24. begin
  25.   if FNum1=AValue then Exit;
  26.   FNum1:=AValue;
  27. end;
  28.  
  29. procedure TClass1.SetNum2(AValue: integer);
  30. begin
  31.   if FNum2=AValue then Exit;
  32.   FNum2:=AValue;
  33. end;
  34.  
  35. constructor TClass1.Create(AOwner: TObject);
  36. begin
  37.  
  38. end;
  39.  
  40. destructor TClass1.Destroy;
  41. begin
  42.   inherited Destroy;
  43. end;
  44.  
  45. function TClass1.Sum: integer;
  46. begin
  47.  
  48. end;
  49.  
« Last Edit: June 29, 2016, 09:33:42 pm by rvk »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Code Review: a very basic class
« Reply #2 on: June 29, 2016, 09:37:31 pm »
And all classes inherit from tobject, so the (TObject) is redundant or style.

I usually omit it.

bulrush

  • Jr. Member
  • **
  • Posts: 86
Re: Code Review: a very basic class
« Reply #3 on: June 30, 2016, 03:07:51 pm »
Thanks rvk. I wasn't sure what I had to type in for ctrl-shift-c to do it's magic.
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: Code Review: a very basic class
« Reply #4 on: June 30, 2016, 03:52:40 pm »
Code: Pascal  [Select][+][-]
  1.  
  2.   if FNum2=AValue then Exit;
  3.   FNum2:=AValue;
  4.  

Is that style of checking for equality instead of doing an unconditional assign really saving time? It seems like an assignment statement would be similar in size to the if, but even if there is a slight savings from the if, that only occurs in cases in which you are assigning the same value. In all other cases it takes longer.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Code Review: a very basic class
« Reply #5 on: June 30, 2016, 03:59:11 pm »
Code: Pascal  [Select][+][-]
  1.  
  2.   if FNum2=AValue then Exit;
  3.   FNum2:=AValue;
  4.  
Is that style of checking for equality instead of doing an unconditional assign really saving time?
Not sure but sometimes there is more code than this. Although in that case you would need to decide if you want that code to execute or not. I think the fact that assigning a different value might take slightly longer outweighs the fact that assigning the same value should be skipped. But that is also the programmers choice.

Also... In Delphi you often see it like this:
Code: Pascal  [Select][+][-]
  1. if FNum2 <> AValue then
  2. begin
  3.   FNum2 := AValue;
  4.   // other initialization
  5. end;

But the Ctrl+Shift+C in Delphi produces:
Code: Pascal  [Select][+][-]
  1. procedure TClass1.SetNum2(const Value: integer);
  2. begin
  3.   FNum2 := Value;
  4. end;
« Last Edit: June 30, 2016, 04:04:35 pm by rvk »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Code Review: a very basic class
« Reply #6 on: June 30, 2016, 05:06:02 pm »
Is that style of checking for equality instead of doing an unconditional assign really saving time?

As a general rule assignments are always more expensive on cpu resources than reads and lookups ("if" merely checks a value without changing it). There are perhaps exceptions, but assignments require copying of data from one location to another in memory/registers, which generally takes longer than simply reading the value of data.
Given the present speed of processors and number of cores and caching mechanisms available there is of course no discernible difference in program speed between including the check for equality (or inequality) and forcing the assignment whether or not it is actually needed. It is unlikely to save a time-consuming disk access.
Nevertheless, it is a principle worth perpetuating to avoid making unnecessary assignments in code. As rvk mentions it can also avoid causing side-effects in some cases, as well as avoiding a waste of cpu cycles.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Code Review: a very basic class
« Reply #7 on: June 30, 2016, 06:46:46 pm »
Not necessarily. Specially for strings an assign is more expensive than an assign.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Code Review: a very basic class
« Reply #8 on: June 30, 2016, 07:13:44 pm »
... Specially for strings an assign is more expensive than an assign.

Surely you meant to type something else  O:-)

Bart

 

TinyPortal © 2005-2018