-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exception to a rule for class that contains only static members. #5077
Conversation
Visit the preview URL for this PR (updated for commit 022bc98): https://dart-dev--pr5077-polina-c-patch-1-in77qeqy.web.app (expires Mon, 31 Jul 2023 20:07:04 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: d851bc446d3c4d7394c5406c6f07255afc7075f3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be singleton objects instead of single instance objects ?
Can you give an example of what you mean? |
Assuming I'm understanding this correctly, I'm not sure it's even worth mentioning the exceptions. With enhanced enums, there's really no reason to have create new classes of constants, and "enum-like types" are also covered by the functionality introduced with enhanced enums. "Single instance" objects are just singletons and by definition are of a type that is instantiated at least once, making their inclusion in this list of exceptions unnecessary. |
'singletone' is a pattern that is useful when exactly one object is needed. So, 'singletone' is a pattern, and 'single object' is a situation, the same as 'when exactly one object is needed', but shorter. We need situation here. So, I think 'single object' works better. Am i missing something? |
https://api.dart.dev/stable/3.0.6/dart-developer/Service/Service.html |
Enhanced enums are limited to constants. singleton is a pattern to implement single instance So, "Single instance" objects are sometimes implemented as singletons, and sometimes differently. Makes sense? |
That class should just be made Although... I think having an abstract class with all static members is a technically a smell as well since namespacing should be done using the |
Yes, you can import a library with a namespace to create grouping. However, it means manual crafting (opposite to just adding import with quick fix), that have to be consistent across the client package, i.e. the same namespace should be used everywhere. And, meaningful grouping for static methods and variables, provided by the package authors, really helps to navigate the code, taking advantage of InteliSense. May be some day we should stop demonizing namespacing and introduce keyword 'namespace' to use instead of 'class'?... |
Yes, this isn't a Singleton, there is no instance at all. In fact, the Service class is exactly what this rule is advising against, it is a direct violation of the suggestion. As mentioned above, if this was a singleton it would have a constructor (probably private) and an instance, and then it would be allowed to have these methods.
You can use an extension with static members instead to create a more pure namespace without introducing a type that has no instances. And probably the lint won't trigger either. It is still going against the spirit of this guideline, but it is arguably a better way of creating a namespace, if you feel one is necessary. |
Service is conceptually an object, "a thing". But, as there is no state, there is no need to instantiate it, so there is no need to implement Singleton pattern. However, per me, it makes perfect sense to define abstract class to describe behavior of this "thing". Anything wrong? |
The "wrong" part is described in the rule - This is why the extension approach is arguably better, it doesn't introduce this extra Type into the program. I do think there are valid reasons you might want a namespace in Dart, although I wouldn't use them heavily. This rule isn't a |
Thanks to everybody for discussion! |
Classes like this happen so often that it worse mentioning them as exception: https://api.dart.dev/stable/3.0.6/dart-developer/Service/Service.html