Posts

Problems of the 'new' keyword

How do we create an object in most languages? It's by using the 'new' keyword and there's a big problem lurking in there! Some languages of-course skip it, like Python, where you can create an instance of class just by doing 'ClassName()' instead of 'new ClassName()' but the underlying problem remains the same. The new keyword causes our codes to get coupled. Let's take this simple example: interface IShape { public string Name { get; } } class Square : IShape { public string Name { get; private set; } public Square() { Name = "Square"; } } class Circle : IShape { public string Name { get; private set; } public Circle() { Name = "Circle"; } } class ShapeHolder { public IShape Shape { get; private set; } ShapeHolder() { shape = new Square(); } } If we see the above example, even though we have used an interface 'IShape', our ShapeHolder is now tightly coupled to the Square insta...
There's nothing here!