当前位置: 首页 > 科技观察

史上最坑爹的代码!所有人都崩溃了!

时间:2023-03-14 10:51:45 科技观察

Java就是这么神奇的语言。任何水平的人都能写出能运行的代码,但是看代码就知道水平高低了。作为程序员,你见过哪些作弊代码,写过多少作弊代码,还有多少作弊代码你不知道?有意思的是码云上已经搭建了一个代码仓库:bullshit-codes。仓库的目的就是收集这些狗屁代码,让别人不掉坑或者减少掉坑,自己也能避免掉坑。也许哈哈!上面汇集了各种编程语言,仓库地址如下:https://gitee.com/oschina/bullshit-codes本文为大家整理了几个作弊代码。整理了几个,实在是整理不出来仅供大家参考,看看能不能崩溃!一、几个作弊代码目录1、这种方式使用StringBuffer有哪些坑?2.你写过的最长的一行代码是多长???3、循环+条件判断,最多可以嵌套几层?4.为了优化后期的查询速度~挺有商业头脑的!5、你是怎么被异常玩弄变成异常的?6.Stream播放最多的代码6,看过的人都惊呆了!2.坑码|这样使用StringBuffer有什么坑?你用过Java的StringBuffer类吗?/***CreateTime2019/5/24*StringBufferappend痴情的写法*@authorcailong**/publicclassAppend{publicstaticvoidmain(String[]ares){StringBuffersb=newStringBuffer();//这里大家可以理解sb.append("");for(inti=0;i<10;i++){//为什么要这样写这里???既然使用了StringBuffer(这里省略集合遍历,用i代替,理解意思)sb.append(""+""+i+""+""+i+""+""+i+""+""+i+""+""+i+""+"sjzs"+""+i+""+"");}sb.append("");System.out.println(sb.toString());}}三、作弊代码|你写过最长的代码行是多少???你写过最长的代码行是多少?为什么要写这么久?是帅不帅,还是想减少代码行数?列表purchaseInfoList=sheet.getPurchaseInfoList().stream().filter(purchaseInfo->purchaseInfo.getExteriorOperation().getExteriorPart().getExteriorOperationList().stream().filter(exteriorOperation->exterioressState().getProcessequals(ExteriorOperation.ProcessState.PROCESSING)).count()!=0//订单明细中进程对应的工件下的其他进程正在处理中,//已发送给供应商且供应商不是当前ordersupplier,需要判断&&(purchaseInfo.getExteriorOperation().getExteriorPart().getTeamwork()==null||!purchaseInfo.getExteriorOperation().getExteriorPart().getTeamwork().equals(sheet.getTeamwork())).collect(Collectors.toList());上面的代码虽然拆成多行显示,但本质上就是一行,极其复杂的赋值语句!这种代码是为了显示你的编码水平,让别人看不懂吗?小编觉得JavaStreamAPI,各种函数式编程方式,各种语法糖,某种程度上让这种烂代码越来越多!那么大家一起来批判一下这段代码,或者大家有什么好的解决办法吗?4.坑码|循环+条件判断,最多能嵌套几层?for循环和if条件判断语句必不可少,但是嵌套循环和条件判断你见过最多的是多少层呢?换句话说,你最多能容忍多少层嵌套?先来看看极端作弊代码://这个***循环嵌套只是整个循环的一部分。..我已经在黄昏立交if(recordList.size()>start){for(inti=start;imap=recordList.get(i);Mapfield11=(Map)map.get("field");//name->codeMaprecord11=(Map)map.get("record");//code->valueStringcatagory1=map.get("categoryId").toString();//查询***类型对应的其他类型SalaryDataVoss=JSON.parseObject(JSON.toJSONString(map),SalaryDataVo.class);Pagepage3=salaryManagerService.getAllRecordsByCondition(ss);if(page3.getRecords().size()>0){List>salaryDataVos=page3.getRecords();salaryDataVos=this.reSetMap(salaryDataVos,null,null);for(Mapmap2:salaryDataVos){Mapfield2=(Map)map2.get("field");Maprecord2=(Map)map2.get("record");Stringcatagory2=map2.get("categoryId").toString();ListgroupList2=salaryGroupService.getSalaryGroupsItems(this.getUserCorpId(),catagory2);for(SalaryGroupVOcc:groupList2){cc.setCode(cc.getParentId()+cc.getCode());}//计算for(Map.Entryentity:field2.entrySet()){StringkeyName=entity.getKey();for(SalaryGroupVOs2:groupList2){if("bigDecimal".equals(s2.getItemType())&&s2.getCode().equals(field2.get(keyName).toString())&&("部门"!=keyName)&&("姓氏"!=keyName)&&StringUtils.isNotEmpty(s2.getItemType())){if(field11.containsKey(keyName)){if(field11.containsKey(keyName)){Stringcode1=field11.get(keyName).toString();DoublenewValue=0.0;DoubleoldValue=0.0;if(!record11.get(code1).toString().matches("^[0-9]*$")){oldValue=Double.parseDouble(record11.get(code1).toString());if(record2.containsKey(entity.getValue().toString())&&(!record2.get(entity.getValue().toString()).toString().matches("^[0-9]*$")){Stringvalue2=record2.get(entity.getValue().toString()).toString();newValue=Double.parseDouble(value2);}record11.remove(field11.get(keyName)。toString());}if(code1.startsWith(catagory1)||code1.startsWith(catagory2)){Stringco=code1.replace(catagory1,"hz");field11.put(keyName,co);record11.put(co,oldValue+newValue);}}}else{Stringcode=entity.getValue().toString();Stringstr=entity.getValue().toString();Objectvalue2=record2.get(entity.getValue().toString());if(str.startsWith(catagory1)&&str.replace(catagory1,"").startsWith("hz")){code=str.replace(catagory1,"");}elseif(str.startsWith(catagory2)&&str.replace(catagory2,"").startsWith("hz")){code=str.replace(catagory2,"");}field11.put(keyName,code);record11.put(code,value2);}}}}}}列表sList=salaryGroupService.getSalaryGroupItemsByParentId(catagory1);for(SalaryGroupVOs:sList){if(field11.containsKey(s.getName())&&s.getCode().startsWith("hz")){Stringk=field11.get(s.getName()).toString();field11.put(s.getName(),s.getCode());Stringvalue=null;if(record11.containsKey(k)){value=record11.get(k).toString();}record11.put(s.getCode(),value);}}resultList.add(map);pageInfo.setRecords(resultList);}}我数了一下,一共嵌套了11层!!!有什么好的方法可以重构这样的代码逻辑吗?五、作弊码|为了后期优化查询速度~挺有商业头脑的!什么样的程序员才是好程序员?我们在给客户开发系统的时候,会预留一些埋点,以备后期优化。通过对这些埋点的优化,客户可以瞬间感受到系统运行速度的质的飞跃,让公司顺利签约二期开发。合同,收取巨额开发费用。在公司看来,这样的程序员才是好程序员。——这句话不是出自红薯!比如:我想说的是:姬蝶,你能不能不狠一点?建议优化代码,改成Thread.sleep(1000);——这句话不是红薯说的。或者你有更好的建议吗?不要觉得骇人听闻,真的有这样的人,这样的代码!!!6.作弊码|你是怎么被异常玩弄然后变成异常的?玩Java的人一开始都会被各种异常虐到,空指针应该是最常见的了。又玩了两年,开始玩异常,各种奇葩异常的玩法层出不穷。您认为以下异常定义正确吗?/***处理业务异常*其实还有一堆静态异常,准备好随时抛出??*错误码是字符串*/publicclassCommandExceptionextendsBaseException{privatestaticfinallongserialVersionUID=-6354513454371927970L;publicstaticCommandExceptionPARAM_NULL=newCommandException("Command_assemble_01","ParameterisNeededButEmpty");publicstaticCommandExceptionDEVID_NULL=newCommandException("Command_assemble_02","DevIdCannotBeNull");publicstaticCommandExceptionMDCODE_NULL=newCommandException("Command_assemble_03","ModelCodeCannotBeEmpty");publicstaticCommandExceptionORDER_NULL=newCommandException("Command_assemble_04","OrderCannotBeEmpty");publicstaticCommandExceptionTYPE_NULL=newCommandException("Command_assemble_05","Upstream/DownstreamTypeCannotBeEmpty");publicstaticCommandExceptionMENUID_NULL=newCommandException("Command_assemble"ExpublicCommentId_06");onCTRLTYPE_NOT_RANGE=newCommandException("Command_assemble_07","CtrltypeCannotBeRecognized,WhichisnotinRange");publicstaticCommandExceptionCMD_NULL=newCommandException("Command_analyze_01","CMDCannotBeNull");publicstaticCommandExceptionPAYLOAD_NULL=newCommandException("Command_analyze_02","PayloadCannotBeNull");publicstaticCommandExceptionFRAMEWORK_FAILED=newCommandException("Command_analyze_03","FrameworkFailedtobeChecked");publicstaticCommandExceptionCHECK_FAILED=newCommandException("Command_analyze_04","CommandFailedtobeChecked");publicstaticCommandExceptionCONFIGURE_NOT_EXIST=newCommandException("Command_error_1001","ConfigureisnotExist");publicstaticCommandExceptionREDIS_ERROR=newCommandException("Command_error_1002","CacheCommandinRedisError",true);//省略构造函数,get/set方法}如果错了,是什么问题?七、作弊码|Stream玩过最多的6代这不,看到的人都惊呆了!Stream作为Java8的一大亮点,与java.io包中的InputStream和OutputStream是完全不同的概念。Java8中的Stream是对集合(Collection)对象功能的增强,它专注于执行各种集合对象。非常方便高效的聚合操作,或者批量数据操作。StreamAPI借助同样新兴的Lambda表达式,极大地提高了编程效率和程序可读性。那么Java8的Stream到底是王者还是巨坑,就看你怎么玩了?不得不说,下面的代码是我见过20年最厉害的Stream的用法(谁,谁从业20年,我才19岁!!!)我见过://Stream使用66finalEventActioneventAction=redisObj(EventActionKey+distributionEventId,()->Optional.of(distributionEventId).map(eventId->{finalEventActionExampleexample=newEventActionExample();example.createCriteria().andEventIdEqualTo(eventId).andTriggerTypeEqualTo(EnumEventTriggerValode.DISTIONValode))());returnexample;}).map(eventActionMapper::selectByExample).filter(StringUtil::isNotEmpty).map(e->e.get(0)).orElseThrow(()->ExceptionUtil.createParamException("event触发信息不存在。")),EventAction.class);finalAwardConfigawardConfig=redisObj(EventConfigKey+eventAction.getId(),()->Optional.ofNullable(eventAction.getId()).map(actionId->{finalAwardConfigExampleexample=newAwardConfigExample();example.createCriteria().andActionIdEqualTo(actionId);returnexample;}).map(awardConfigMapper::selectByExample).filter(StringUtil::isNotEmpty).map(e->e.get(0)).orElseThrow(()->ExceptionUtil.createParamException("xxx")),AwardConfig.class);Optional.of(req).map(e->e.clueUid).map(id->{finalClueExampleexample=newClueExample();example.createCriteria().andClueUidEqualTo(id).andDeletedEqualTo(false).andReceivedEqualTo(false).andCreateTimeGreaterThan(now-cluetime);example.setOrderByClause("create_timeasc");returnexample;})//获取所有未过期未领取的被邀请人的线索.map(clueMapper::selectByExample).filter(StringUtil::isNotEmpty).ifPresent(clues->{finalClueRespclueResp=Optional.of(req).filter(c->{c.count=clues.size();returntrue;}).map(this::awardValue).orElseThrow(()->ExceptionUtil.createParamException("参数错误"));finalIntegerspecialId=req.getIsHead()?clues.get(0).getId():clues.get(clues.size()-1).getId();clues.stream().peek(线索->{finalAwardConfigawardConfigclone=Optional.of(awardConfig).map(JSONUtil::obj2Json).map(json->JSONUtil.json2Obj(json,AwardConfig.class)).orElseGet(AwardConfig::new);awardConfigclone.setMoney(Optional.of(clue.getId()).filter(specialId::equals).map(e->clueResp.specialReward.longValue()).orElse(clueResp.otherAverageReward.longValue()));eventActionService.assembleAward(awardConfigclone,clue.getAdviserUid(),clue.getAdviserUid(),clue.getClueUid(),eventAction,newPasMessageParam(),clue.getId(),AwardRelationType.Clud.code());}).forEach(线索->{clue.setOrderNo(req.orderNo);clue.setCommodityName(req.commodityName);clue.setOrderAmount(req.orderAmount);clue.setReceived(true);clue.setModifyTime(现在);clueMapper.updateByPrimaryKeySelective(线索);});});Java就是这样一门神奇的语言,任何水平的人都可以写出可以运行的代码,但是一看就知道代码的水平,但说的一定是这样的Stream代码!8.编辑死了。实在是整理不下去了。感兴趣的可以去:https://gitee.com/oschina/bullshit-codes进一步投诉!