What does the worst code in history look like? Have you seen it?

The more projects I have gone through, the more weird codes I have seen. The purpose of finding bad code is not to criticize and tease, but to avoid letting yourself or people around you write similar codes.

There is a popular warehouse bullshit-codes[1] on Code Cloud,   which collects the most cheating code snippets in history. Here are a few more interesting ones for everyone to see:

1. It can be done in one line of code, and you have to write long and long-winded. Don't tell me about readability, the following line of code is more readable.

What does the worst code in history look like?  Have you seen it?

 

2. Intelligent AI customer service system designed by former programmers

What does the worst code in history look like?  Have you seen it?

 

Show results:

What does the worst code in history look like?  Have you seen it?

 

3. As long as I have enough code, it is difficult for you to see what is wrong

/**
 * 三年前接手的一个政府网站项目,真假不知,据说是一个两年web开发经验的程序员写的, 用的spring系列还是3.x版本,前端还是jsp,可能是老项目。 * 主要特点是:实体类中不写注释,数据库表中也没有注释,命名经常使用英文拼音混合,而且还可能是简写(grzlChangeStatus:个人资料变更状态) * * 挖坑不填坑,菊花塞电灯 */@Controllerpublic class DemoController { /**  * 这个方法一共有800多行,有30多个if或else if的条件判断,只写代表性的几个
  *  * 具体方法名称记不得,逻辑是这样,前端jsp提交一个请求过来,界面中除了一些必填信息外,还有有5个下拉框,后台判断各种组合,  * 分别往request里面塞入不同的对象,返回的页面视图也可能不同。  */ @RequestMapping("/submit")
 public String template(HttpServletRequest request, HttpServletResponse response, HttpSession session) {  String param1 = request.getParameter("param1");
  String param2 = request.getParameter("param2");
  String param3 = request.getParameter("param3");
  String param4 = request.getParameter("param4");
  String param5 = request.getParameter("param5");
  // 你没看错,各种状态全部存字串,而且判断的时候不考虑空指针问题  if (param1.equals("已消毒")) {
   // 逻辑   request.setAttribute("attr", "value");
   return "jsp1";
  } else if (param1.equals("已消毒") && param2.equals("1.8米以上")) {
   // 逻辑   request.setAttribute("attr", "value");
   // 你没看错,可能又是另一个视图   return "jsp2";
   // 你没看错,还可能出现多个条件,|| 和 &&  } else if (param1.equals("已消毒") && param2.equals("2米以上") || param3.equals("缅甸")) {
   // 逻辑   request.setAttribute("attr", "value");
   return "jsp3";
   // 你没看错,因为他的状态或选项没有用数字类型,所以不能用 > < 之类的来判断,要列举所有状态  } else if (param1.equals("未进站") || param1.equals("已进站") || param1.equals("已消毒") && param2.equals("2米以上")) {
   request.setAttribute("attr", "value");
   return "jsp3";
   //你没看错,还会出现 true==true 或false==false这样的代码
  }else if(param4.equals("禁止入境")==true && param5.equals("农用车")) {
   request.setAttribute("attr", "value");
   return "jsp4";
   //你没看错,还会出现逻辑非,而且,要先判断什么条件,从来不用括号包起来,要理他的逻辑,还要先理条件判断优先级  }else if(param1.equals("未进站") || param1.equals("已进站") || param1.equals("已消毒") && param2.equals("2米以上") && !param3.equals("缅甸")) {
   request.setAttribute("attr", "value");
   return "jsp5";
  }  //省略剩下的30多个类似的判断  else {
   return "default";
  } }}

Problems with the above code:

  • Null pointer exception
  • The logic is chaotic, part of the else if can’t get in at all

4. Get the date one day later: let the thread sleep for a while

What does the worst code in history look like?  Have you seen it?

 

5. The code naming of the new project means that it is not easy to understand

What does the worst code in history look like?  Have you seen it?

 

6. In order to get the total number of users, query all User objects.

/**
  统计用户总数,来源于N年前某个用户抱怨执行效率低,贴出类似逻辑的代码.
  @author wendal
*/
@IocBean
public class CountUserTable {
    @Inject Dao dao;
    @Test
    public void test_user_count() {
        // 取出全部用户对象(dao.query返回List<User>),然后取list大小就可以啦,是不是很机智
        int count = dao.query(User.class, null).size();
        System.out.println("用户总数是" + count);
    }
}

Guess you like

Origin blog.csdn.net/weixin_45132238/article/details/108629040