3

I was wondering why in the Angular (2+) Framework source, they use static class for features like Validators ? See an example here : https://github.com/angular/angular/blob/9.1.0/packages/forms/src/validators.ts

I assume the goal of this was to have a Validators namespace where all the validation methods are grouped in ? But then this will prevent the code to be Tree shakeable if I'm not wrong ? (BTW could this change with Ivy ?)

So why did they decide to use this pattern ? When creating my custom Validator function, should I also stick with this static class pattern for any reason or I could just use export methods (that are tree shakeable) as long as I don't need any class feature ?

ylerjen
  • 4,109
  • 2
  • 25
  • 43
  • Agreed, this is a horrible pattern. My guess is either the code was written by Java developers, or their documentation framework only supports classes. It could have been a simple object literal, or, as you suggest, just multiple named exports. – Bergi Apr 06 '20 at 15:11
  • I've closed as a duplicate, but will happily reopen if you meant to ask specifically about the Angular source code. – Bergi Apr 06 '20 at 15:13
  • 2
    Tx for your message. In fact it was specifically about Angular. As I know some pro/con of each solution I don't get why experienced programmers like the Angular team chose to use what is often considered as an anti-pattern in JS. So I probably miss something and would be happy to know what is their reason and why in this case it's good to use that class+static. (your reason about documentation framework could be one tho ) – ylerjen Apr 06 '20 at 15:20

0 Answers0