Forum > General

Seeking guidance for a constructor calling a constructor

(1/4) > >>

Gustavo 'Gus' Carreno:
Hey Y'all,

I've been quite contempt with the following code, and I even thought it was rather clever.
But, now, after letting it on the back burner for a while, I feel I'm missing something like caveats or pitfalls that I'm blind to.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---interface type  TSomeClass = class(TObject)  private    FIntField: Integer;    FStringField: String;     procedure LoadFromJSON(AJSON: String);  public    constructor Create;    constructor Create(AJSONS: String);  end; implementation procedure TSomeClass.LoadFromJSON(AJSON: String);begin  //  Load JSON into fpjson structures  { ... }  //  From fpjson structures set the fields  { ... }end; constructor TSomeClass.Create;begin  FIntField:= 0;  FStringField:= '';end; constructor TSomeClass.Create(AJSON: String);begin  Create;  LoadFromJSON(AJSON);end;
What I'm thinking is that I should refactor the code like thus:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---interface type  TSomeClass = class(TObject)  private    FIntField: Integer;    FStringField: String;     procedure InitiateFields;    procedure LoadFromJSON(AJSON: String);  public    constructor Create;    constructor Create(AJSON: String);  end; implementation procedure TSomeClass.InitiateFields;begin  FIntField:= 0;  FStringField:= '';end; procedure TSomeClass.LoadFromJSON(AJSON: String);begin  //  Load JSON into fpjson structures  { ... }  //  From fpjson structures set the fields  { ... }end; constructor TSomeClass.Create;begin  InitiateFields;end; constructor TSomeClass.Create(AJSON: String);begin  InitiateFields;  LoadFromJSON(AJSON);end;
What does the community think about these two pieces of code?

I'm paging the experts, the ones that are more active: @PascalDragon, @marcov and @Martin_fr.

But I do, I really do, appreciate whatever nugget of wisdom anyone else can add to this in doubt mental state.

As per usual, you have my most sincere thanks in advance for your time and patience with my predicaments ;)

Cheers,
Gus

HeavyUser:
1) the more calls you add the slower you code becomes.
2) the prefix T is used only on type definition not on variables.
3) If you do use the initializeFields idea do make sure that at least its virtual.

Personally I used both methods, I used your second method in c++ where I had problems with virtual constructors and your first method as my primary go to method in pascal.

In short do not break your methods unless absolutely necessary and in your example breaking the initialization code out to a 3rd method is excessive.

The only way that might be acceptable if you had a reset method of some sort.

mas steindorff:
looks like you have one approach figured out

another way, if your class is complex or may be useable in a simpler object...  is to define 2 classes, a base class "TSomebase" and then TsomeClass = class(TSomebase)

then in each of your class creates, do:
  inherited Create();
 ... and then your other init code


bpranoto:

--- Quote from: Gustavo 'Gus' Carreno on May 06, 2022, 06:36:11 pm ---Hey Y'all,

I've been quite contempt with the following code, and I even thought it was rather clever.
But, now, after letting it on the back burner for a while, I feel I'm missing something like caveats or pitfalls that I'm blind to.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...constructor TSomeClass.Create;begin  InitiateFields;end; constructor TSomeClass.Create(AJSON: String);begin  InitiateFields;  LoadFromJSON(AJSON);end;

--- End quote ---

In this case, I prefer to define only one constructor with Optional parameter like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---constructor TSomeClass.Create(AJSON: String='');begin  InitiateFields;  if AJSON <> '' then  LoadFromJSON(AJSON);end;
Simpler and clearer..

Regards,
Bambang

Gustavo 'Gus' Carreno:
Hey HeavyUser,


--- Quote from: HeavyUser on May 07, 2022, 04:23:15 am ---1) the more calls you add the slower you code becomes.

--- End quote ---

Thanks for the tip. I'll keep this in mind when doing code that is called many times in a cycle.
This code is mainly for marshalling and un-marshalling from JSON <--> TObject.
Nothing that needs any speed tuning, I guess :)


--- Quote from: HeavyUser on May 07, 2022, 04:23:15 am ---2) the prefix T is used only on type definition not on variables.

--- End quote ---

 :-[ Shuck, mate, thanks!! Now I feel embarrassed for the stupid mistake. Let's put it down to being too early in the morning when I typed the code, LOL!!!
I've corrected the code to display the appropriate F for field.


--- Quote from: HeavyUser on May 07, 2022, 04:23:15 am ---3) If you do use the initializeFields idea do make sure that at least its virtual.

--- End quote ---

I think I've never fully understood what virtual means and how best to use it. Could you elaborate or point me out for a good quick refresher?
Many thanks in advance!!


--- Quote from: HeavyUser on May 07, 2022, 04:23:15 am ---Personally I used both methods, I used your second method in c++ where I had problems with virtual constructors and your first method as my primary go to method in pascal.

--- End quote ---

Good to know, many thanks!!


--- Quote from: HeavyUser on May 07, 2022, 04:23:15 am ---In short do not break your methods unless absolutely necessary and in your example breaking the initialization code out to a 3rd method is excessive.

The only way that might be acceptable if you had a reset method of some sort.

--- End quote ---

I agree that, in this very simplistic example, the InitiateFields method is a bit of an overkill.

But in my full code there are methods to LoadFrom[JSON|JSONData|JSONObject|JSONArray], which in my opinion, needs the InitiateFields as a reset button.

I will take you tips onboard as they are quite useful.
Many thanks for them!!

Cheers,
Gus

Navigation

[0] Message Index

[#] Next page

Go to full version