Question: how would nullable flow analysis behave for method accessing non-nullable field called by constructor #1728
Replies: 8 comments
-
That's totally fine, no problemo. The field As far as At the moment though, if you call |
Beta Was this translation helpful? Give feedback.
-
I disagree. This would preclude the use of factory methods to create the value to store in the field. Such methods can't always be static because sometimes they need to be overridable - and it's a useful design technique. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek Factory method and consuming method are two different scenarios, and should be covered by seperated rules. |
Beta Was this translation helpful? Give feedback.
-
See also: CA2214. |
Beta Was this translation helpful? Give feedback.
-
Used judiciously, it solves problems that aren't otherwise solvable. |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan I was thinking about a private factory method called by the constructor:
@Joe4evr I agree that it can be misused - but whether it's an antipattern or not is up for debate. I've worked with C++ developers who were repulsed by it (no exaggeration) and who went to great lengths to avoid calling virtual methods from constructors - including the abomination of "Consumers need to always call In the Smalltalk community, as I understand it, using factory methods so that descendant classes can change the defaults is idiomatic and normal. @jnm2 I've primarily seen this in cases where a large class hierarchy has evolved over time, and it proved pragmatic to use a virtual factory method to accommodate changing requirements in a way that didn't require wholesale modification of every leaf class. |
Beta Was this translation helpful? Give feedback.
-
Yes, I've been extremely glad for it at times. |
Beta Was this translation helpful? Give feedback.
-
Suppose this code:
Have flow analysis in this scenario been discussed?
In my opinion, calling method before assigning all non-nullable field should give a warning.
Beta Was this translation helpful? Give feedback.
All reactions