loading...

Is declaring helper variables which are simply removal of `this` bad practice?

hayashiay profile image hayashi-ay ・1 min read

Hi there, I'm newbie for dev.to and this is my first post.

Our company has a convention to declare helper variables which are simply removal of this like the following.

export class SampleClass {
  constructor() {
    // this is a constructor
  }

  get sampleGetter() {
    // return something
  }

  sampleMethod() {
    // What do you think about the following?
    const sampleGetter = this.sampleGetter;

    // do something
  }
}

I was thinking that it's efficient when the variables are used multiple times.
However, I've been recently pointed out that it's premature optimaization and just adds a tiny bit of complexity with no clarity.

What do you think? I'd like to hear your opnions.

Discussion

markdown guide
 

I don't know what the performance implications are, but just offhandedly I wouldn't think of doing something like that. It seems uglier and I doubt it noticeably affects performance.