Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Over using 'this' in typescript

Tags:

I am just starting to learn typescript/Angular having come from a c++/java background having never touched any web development stuff before. I wrote a small piece of code with some FormControls and someone made a suggested improvement that I don't understand.

The code is as follows:

this.myFormData = { ...data };    
this.myForm.controls['name'].setValue(this.myFormData.name);
this.myForm.controls['address'].setValue(this.myFormData.address);
this.myForm.controls['age'].setValue(this.myFormData.age);
this.myForm.controls['gender'].setValue(this.myFormData.gender);
this.myForm.controls['mailing_list'].setValue(this.myFormData.mailing_list);

I've been told that I should change it to:

this.myFormData = { ...data };
const formControls = this.myForm.controls;
formControls['name'].setValue(this.myFormData.name);
formControls['address'].setValue(this.myFormData.address);
formControls['age'].setValue(this.myFormData.age);
formControls['gender'].setValue(this.myFormData.gender);
formControls['mailing_list'].setValue(this.myFormData.mailing_list);

with the reason being it would save memory. I questioned this point thinking it was maybe some premature optimization but the peer reviewer is sure that accessing 'this' and searching for 'myForm' and getting 'controls' each time is bad practice stating the DRY principle of software development.

I am still learning so I guess I should just take the reviewers word for it but I really do want to understand the difference in the example posted and why one was better than the other. I have searched online for an answer but i'm not having any success (maybe due to not knowing the technical terms to search for?).

An explanation for a novice would be great or links to where I could find an answer would help too.