What is the name of this bad practice / anti-pattern?

前端 未结 14 2139
独厮守ぢ
独厮守ぢ 2021-02-03 18:02

I\'m trying to explain to my team why this is bad practice, and am looking for an anti-pattern reference to help in my explanation. This is a very large enterprise app, so here

相关标签:
14条回答
  • 2021-02-03 18:22

    The Writing Too Much Code Anti-Pattern. It makes it harder to read and understand.

    As has been pointed out already it would be better to use an interface.

    Basically the programmers are not taking advantage of Object-Oriented Principles and instead doing things using procedural code. Every time we reach for the 'if' statement we should ask ourselves if we shouldn't be using an OO concept instead of writing more procedural code.

    0 讨论(0)
  • 2021-02-03 18:26

    Normally a better approach (IMHO) would be to use interfaces instead of inheritance

    then it is just a matter of checking whether the object has implemented the interface or not.

    0 讨论(0)
  • 2021-02-03 18:27

    IMO the fundamental design principle at play here is encapsulation. In your proposed solution you have encapsulated the logic inside of the Thing class, where as in the original code the logic leaks out into the callers.

    It also violates the Open-Closed principle, since if you want to add new subclasses that support X you now need to go and modify anywhere that contains that hard-coded list. With your solution you just add the new class, override the method and you're done.

    0 讨论(0)
  • 2021-02-03 18:28

    Don't know about a name (doubt such exists) but think of each "Thing" as a car - some cars have Cruise Control system and others do not have.

    Now you have fleet of cars you manage and want to know which have cruise control.

    Using the first approach is like finding list of all car models which have cruise control, then go car by car and search for each in that list - if there it means the car has cruise control, otherwise it doesn't have. Cumbersome, right?

    Using the second approach means that each car that has cruise control come with a sticker saying "I has cruise control" and you just have to look for that sticker, without relying on external source to bring you information.

    Not very technical explanation, but simple and to the point.

    0 讨论(0)
  • 2021-02-03 18:30

    I don't know if there is a 'pattern' for writing code that is not maintainable or reusable. Why can't you just give them the reason?

    0 讨论(0)
  • 2021-02-03 18:33

    In order to me the best is to explain that in term of computational complexity. Draw two chart showing the number of operation required in term of count(listOfThingsThatSupportX ) and count(listOfThings ) and compare with the solution you propose.

    0 讨论(0)
提交回复
热议问题