4 ms·
For things where you don't have to know which type of shape you're actually trying to create - sure. But now consider you're making something that displays shap
by viraptor 3y ago
For things where you don't have to know which type of shape you're actually trying to create - sure. But now consider you're making something that displays shapes in one of two GUI toolkits, depending on where you're running it. You have basically 3 options:
- if/else everywhere to make ShapeA/ShapeB depending on the current toolkit
- generic Shape DTO and if/else + transforming all around the dispatcher to each toolkit
- pass in ToolkitContext instance which has a .createShape() which requires a single condition at app initialisation stage
Well... that ToolkitContext is a ShapeFactory. (Among other things) Then if you want to use a nicer system of handling this, you use DI, which is then effectively a FactoryFactory. Whether it looks obnoxious each time or you simply say AppContext.Get<Shape>() depends largely on your language though.
- contravariant 3y agoSolving the issue of having two different ways of drawing shapes by having two different classes of shapes and special logic to construct the right one seems like a bad idea. Seems to me that what you actually want is an algebraic data type and 2 different plot functions. Visitor patterns are a neater way to achieve that.
- viraptor 3y ago> seems like a bad idea. "Seems like a bad idea" seems like a bad way to make software engineering decisions. Define what you're losing / gaining, how it impacts readability, does it simplify future development, etc.
- contravariant 3y agoWouldn't one normally require that kind of analysis before embracing some design pattern, rather than dismissing it? I'm just saying what my prior is, I'm not saying I won't change my mind, but you seem to suggesting I should require less evidence to accept the option I'm least sure about. Which, you know, seems like a bad idea.
- deleted 3y ago[deleted]
- winrid 3y agoLet's say you have SwingButton and FLTKButton. I'd have a common Button that uses the fancy new pattern matching features. I like me some if statements. Debugger friendly.
- blowski 3y agoSome engineers see a chain of if statements as the problem, others see it as the solution. Who’s right is very subjective.
- djbusby 3y agoUse switch!
- viraptor 3y agoI'd go with: who's right depends on the code in question, with a large area of "it's a wash" in between. The extremes are not subjective.
- tacitusarc 3y agoI strongly disagree with this. If statements have caused me more headaches than any other single programming concept. This is not subjective. When coders use if statements to replace polymorphism, it scatters logic throughout many different files. This has two extremely negative effects: first, to understand the reach of a single component, you need to understand the entire codebase. Second, when multiple developers are working in tandem, you are almost guaranteed to go through an incredibly difficult rebase process since the structure requires everyone to work on the same files. If statements are useful for validation and bootstrapping decisions*, but if you embed them in your model as part of the operational structure, you’re making a rookie mistake. *High performance software and scripts are the exception to most rules.
- viraptor 3y agoThen you have an if statement per every place where you're operating on one or the other type. It may be very few places and it may be fine like that. It may be 4 options times a hundred paces and then it's not fine anymore. I'm not sure what you mean by debugger friendly. Your debugger knows what types things are.
- paulmd 3y agoI don't quite know what point this feature came to be, but in Java it's now compiler-correct for subclasses to declare their overrides as returning a more precise type than the superclass, as long as it doesn't disagree with the superclass. The place I've bumped into this is the "self"/"builder" pattern of setter. So for example if you have the superclass method: abstract public Shape setArea(Number area); it's absolutely correct for other interfaces to override it with more precise definitions that they expect their members to obey: @Override abstract public Quadrilateral setArea(Number area); @Override public Square setArea(Number area) { this.area = area; return this; } The downside here is that interfaces and classes have to override all such methods with their own implementation returning the proper type, even if it's just { super.setArea(area); return this } otherwise the superclass's looser definition will be used. For example if you don't override the area method then this doesn't work: //compiler error: method setSides is unknown for class Shape new Square().setArea(area).setSides(sides) But if subclasses override all their methods, for example if Square overrides the setArea method with one returning Square, the above example works. Obviously, again, only works if you know the instance you're being passed is a Quadrilateral or a Square, but if you don't know that you are reduced to casting based on some kind of list or handler.