Refactoring if/else logic

前端 未结 14 643
醉话见心
醉话见心 2020-11-30 01:38

I have a java class with a thousand line method of if/else logic like this:

if (userType == \"admin\") {
     if (age > 12) {
          if (location == \"         


        
14条回答
  •  星月不相逢
    2020-11-30 02:29

    If the code in the blocks fits within a few standard patterns, I would create a table with columns (type, location, minAge, maxAge, action), where 'action' is an enum indicating which of several types of processing to do. Ideally, this table would be read from a data file or kept in SQL.

    Then, you can just do a table lookup in the Java code to determine the action to take for a user.

提交回复
热议问题