Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 28
    Love this answer, in special after reading all other ones. I can add almost all (good) new coders are too much dogmatic about principles by the simple fact they really want to be good but don't learned yet the value of keeping things simple and don't overkill. Commented Aug 14, 2014 at 17:48
  • 1
    Another problem with public constructors is that there's no nice way for a class Foo to specify a public constructor should be usable for the creation of Foo instances, or the creation of other types within the same package/assembly, but should not be usable for the creation of types derived elsewhere. I don't know of any particularly compelling reason a language/framework couldn't define separate constructors for use in new expressions, versus invocation from subtype constructors, but I don't know of any languages that do make that distinction. Commented Feb 11, 2015 at 16:12
  • 2
    A protected and/or internal constructor would be such a signal; this constructor would only be available to consuming code whether in a subclass or within the same assembly. C# does not have a keyword combination for "protected and internal" meaning only subtypes within the assembly could use it, but MSIL has a scope identifier for that type of visibility so it's conceivable the C# spec could be extended to provide a way to utilize it. But, this really doesn't have much to do with the use of factories (unless you use the restriction of visibility to enforce a factory's use). Commented May 4, 2015 at 15:52
  • 3
    Perfect answer. Right on point with the 'theory meets reality' part. Just think of how many thousands of developers and human time spent cargo-cult praising, justifying, implementing, using them to then falling to the same situation you described, is just maddening. Following YAGNI, Ive never identified the need to implement a factory Commented Dec 9, 2017 at 1:54
  • 2
    Nice answer, but I have one disagreement, new being evil is more of C++ thing. for C#/Java new is perfectly alright with most programmers. Commented Jun 4, 2020 at 11:43