Should I remove the OnInit implementation which is generated when a component is created by the Angular CLI if it isn't strictly necessary?

Issue

My team is working on an Angular project and are trying to establish team code style conventions. A teammate has encouraged me to remove the OnInit implementation which was generated when I created the component using Angular CLI.

My teammate’s argument in favor of removing the OnInit implementation is that this is unnecessary code and that failure to remove unused code may lead to misunderstanding by other developers who fail to recognize the implementation is unnecessary. This argument seems reasonable to me in general, but the idea of removing some generated code makes me somewhat uncomfortable.

I was curious if others in the Angular community have considered this type of coding convention and what factors led to a decision. Have you asked teammates to remove generated code which isn’t strictly necessary? Would you consider this an unreasonable convention?

Solution

So really there is no correct answer, it’s for you and your team to decide on such conventions. I prefer to remove empty functions for the same reason as removing blocks of commented-out code; it just takes up space and distracts from the code that matters.

It’s not a big deal either way but static code analysis tools such as SonarQube would pick this up as a code smell (see https://rules.sonarsource.com/javascript/RSPEC-1186).

Answered By – tjkeast

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.