How to explain why a developer should avoid case s

2019-04-11 00:00发布

I have a few times needed to try and explain to a developer why they should change a class which has multiple instances of

switch(m_type) {
  case TYPE_A:
    ...
    break;

  case TYPE_A:
    ...
    break

  ...
}

Where m_type is a (final) reference to an enum defining what type of instance this is.

Whenever I see this I think it's time to split the class out into multiple subclasses where the concrete class you create then defines the type. The argument I always get back is 'It didn't seem worth creating all these classes with hardly any code in, what are they going to have in there? like 2 methods or something!'

The best explanation I've seen is that unnecessary control logic should be avoided. If the code can be restructured to remove or avoid a branch, if, switch, etc. it should. If not, then use the control statement that is the most appropriate. From here but I'm not convinced this will be well received since the dev in question always thinks their code isn't too complex anyway so probably won't get why it matters.

This is a common enough mistake though with a very common attitude to why it exists, has anyone had any luck helping a developer understand the mistake they are making?

Thanks,

Robin

8条回答
孤傲高冷的网名
2楼-- · 2019-04-11 00:21

Tell him that he needs to use an extra variable to store the object type. Tell him about the vtable (if using C++) and explain how that idiom is baked into the language and thus idiomatic.

查看更多
成全新的幸福
3楼-- · 2019-04-11 00:21

Well this is why object oriented programing was invented in the first place. Tell him with static binding, which a case statement is, any new type introduced needs old code to be rewritten, recompiled, retested and redistributed.

Whereas with classes and virtual functions new code can be called by old code.

查看更多
仙女界的扛把子
4楼-- · 2019-04-11 00:26

Lead by example, show some places where you have removed duplicate switch statements.

查看更多
甜甜的少女心
5楼-- · 2019-04-11 00:28

You're asking for justification for the refactoring: Replace Conditional with Polymorphism, or the more specific: Replace Type Code with Subclasses. The Anti-If Campaign has some relevant articles.

The sourcemaking website says:

The advantage of Replace Type Code with Subclasses is that it moves knowledge of the variant behavior from clients of the class to the class itself. If I add new variants, all I need to do is add a subclass. Without polymorphism I have to find all the conditionals and change those. So this refactoring is particularly valuable when variants keep changing.

I don't think there's much to add to that.

查看更多
一夜七次
6楼-- · 2019-04-11 00:29

The code you described is in direct violation of the Open/Closed Principle (the "O" in SOLID). With respect to OOP, that should be enough to make your case. ;-)

查看更多
趁早两清
7楼-- · 2019-04-11 00:30

Make sure they write thorough unit tests for the code in question. Then explain that when a new 'type' is added in future and they need to change this code, they will now have broken unit tests that they need to fix.

查看更多
登录 后发表回答