码农日记

薄洪涛的个人博客

请规范你的代码,Coder

hello,大家好,好久不见,最近3周都没有研究新技术,出教程;原因是这样的:

两周前,我接手了一个水平极差的已经离职的同事的代码,并且要在此平台上做一些新的功能,万万没想到,这货居然在代码里下毒!!!

我们的其中一个需求是这样的,某项检查需要重复填写3次,这货居然没用循环,硬写了3个form表单,然后通过js去load到一个大表单中,提交表单的时候,也没用循环,硬生生的保存了,代码如下

视图文件:

<form id="treatment1" onsubmit="return false;"></form>
<form id="treatment2" onsubmit="return false;"></form>
<form id="treatment3" onsubmit="return false;"></form>

$('#treatment1').load('/bloweb/admin/first-treatment/form1');
$('#treatment2').load('/bloweb/admin/first-treatment/form2');
$('#treatment3').load('/bloweb/admin/first-treatment/form3');

视图中form1,form2,form3都是一样的页面,js函数为了防止冲突,这货是这么解决的,比如在form1中声明了nk1(),那么form2中就是nk2(),form3中就是nk3(),关键是这3个方法都几乎一样;所有的html的标签的id都是这么命名的,比如$("#name1"),$("#name2"),$("#name3")

控制器是这么写的:

public function addTreatment()
{
    $treatment = request()->post('treatment');
    $treatment1 = request()->post('treatment1');
    $treatment2 = request()->post('treatment2');
    $treatment3 = request()->post('treatment3');
    $newTreatment = array();
    $newTreatment1 = array();
    $newTreatment2 = array();
    $newTreatment3 = array();
    foreach ($treatment as $k => $v) {
        $newTreatment[$v['name']] = $v['value'];
    }
    foreach ($treatment1 as $k => $v) {
        $newTreatment1[$v['name']] = $v['value'];
    }
    foreach ($treatment2 as $k => $v) {
        $newTreatment2[$v['name']] = $v['value'];
    }
    foreach ($treatment3 as $k => $v) {
        $newTreatment3[$v['name']] = $v['value'];
    }
    $id = DB::table('first_line_treatment')->insertGetId($newTreatment);
    $newTreatment1['treatment_id'] = $id;
    $newTreatment2['treatment_id'] = $id;
    $newTreatment3['treatment_id'] = $id;
    $newTreatment4['treatment_id'] = $id;
    $newTreatment5['treatment_id'] = $id;
    $newTreatment6['treatment_id'] = $id;
    $newTreatment7['treatment_id'] = $id;
    $newTreatment8['treatment_id'] = $id;
    $newTreatment1['treatment_type'] = 1;
    $newTreatment2['treatment_type'] = 1;
    $newTreatment3['treatment_type'] = 1;
    $newTreatment1['treatment_assessment_week'] = 1;
    $newTreatment2['treatment_assessment_week'] = 2;
    $newTreatment3['treatment_assessment_week'] = 3;
    DB::table('treatment_assessment')->insert($newTreatment1);
    DB::table('treatment_assessment')->insert($newTreatment2);
    DB::table('treatment_assessment')->insert($newTreatment3);
    if ($id) {
        echo json_encode(['code' => 1, 'msg' => '添加成功']);
        die;
    } else {
        echo json_encode(['code' => 2, 'msg' => '添加失败']);
        die;
    }

}

毛病很多吧,多表保存事务不用?保存结果不判断?添加成功是仅用id判定?

我的噩梦开始了,开始的时候,客户要求改成循环10次,即检查数据要重复显示10遍,如果我按照他的想法来的话,就需要我复制粘贴剩下的7次,而且,js的函数名字也会相互影响,标签的id值也要分别替换,而且此人代码真的是又臭又长,从没有注释,数据库设计的数据表真的是一言难尽,没有注释,所有类型都是varchar255

我硬着头皮重构,先按照客户的需求改成了10循环,重构后部分控制器代码如下,然后view层的form表单改成了一个

    public function addTreatment()
    {
        $data = request()->post();
        $newTreatments = array();
        DB::beginTransaction();
        try{
            foreach ($data as $k => $v) {
                foreach($v as $kk=>$vv){
                    $newTreatments[$k][$vv['name']] = $vv['value'];
                }
            }
            $patient_id = $newTreatments['treatment']['patient_id'];
            $patient['update_time'] = date('Y-m-d H:i:s', time());
            $rs1 = DB::table('patient')->where(['patient_id' => $patient_id])->update($patient);
            if($rs1===false)
                throw new \Exception();
            $newTreatments['treatment']['is_recrudescence'] = 0;
            //一线治疗主表
            $id = DB::table('first_line_treatment')->insertGetId($newTreatments['treatment']);
            $max_week = $this->treatmentCount($id);
            //最大的评估次数加一
            empty($max_week) ? $max_week = 1 : $max_week = $max_week + 1;
            unset($newTreatments['treatment']);
            foreach ($newTreatments as $k=>$newTreatment){
                $newTreatment['treatment_type'] = 1;
                $newTreatment['treatment_id'] = $id;
                $rs2 = DB::table('treatment_assessment')->insert($newTreatment);
                if($rs2===false)
                    throw new \Exception();
            }
            DB::commit();
            return json_encode(['code' => 1, 'msg' => '添加成功','id'=>$id]);
        }catch (\Exception $e){
            DB::rollback();
            return  json_encode(['code' => 2, 'msg' => '添加失败']);
        }
    }

接下来令人吃惊的操作出现了,在其他的控制器中,我发现了更新操作是这样的

public function updateSign()
{
    $data = request()->post();
    $patient_id = '';
    foreach ($data as $k => $v) {
        is_array($v) ? $v=implode(',', $v) : '';
        if (strpos($k, 'sign_') !== false || $k == 'main_complaint') {
            $sign[substr($k,strpos($k, '_')+1)] = $v;
        } else if ($k == 'patient_id') {
            $patient_id = $v;
        } else if (strpos($k, 'table4_') !== false) {
            $sign4[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table5_') !== false) {
            $sign5[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table6_') !== false) {
            $sign6[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table7_') !== false) {
            $sign7[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table8_') !== false) {
            $sign8[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table9_') !== false) {
            $sign9[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table10_') !== false) {
            $sign10[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table11_') !== false) {
            $sign11[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table12_') !== false) {
            $sign12[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table13_') !== false) {
            $sign13[substr($k,strpos($k, '_')+1)] = $v;
        } else if (strpos($k, 'table14_') !== false) {
            $sign14[substr($k,strpos($k, '_')+1)] = $v;
        } else {
            $sign_laboratory[$k] = $v;
        }

    }

    $sign['patient_id'] = $patient_id;
    $sign4['patient_id'] = $patient_id;
    $sign5['patient_id'] = $patient_id;
    $sign6['patient_id'] = $patient_id;
    $sign7['patient_id'] = $patient_id;
    $sign8['patient_id'] = $patient_id;
    $sign9['patient_id'] = $patient_id;
    $sign10['patient_id'] = $patient_id;
    $sign11['patient_id'] = $patient_id;
    $sign12['patient_id'] = $patient_id;
    $sign13['patient_id'] = $patient_id;
    $sign14['patient_id'] = $patient_id;
    $sign_laboratory['patient_id'] = $patient_id;
    DB::table('diagnosis_sign')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table4')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table5')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table6')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table7')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table8')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table9')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table10')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table11')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table12')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table13')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_table14')->where(['patient_id' => $patient_id])->delete();

    DB::table('sign_laboratory')->where(['patient_id' => $patient_id])->delete();

    $result1 = DB::table('diagnosis_sign')->insert($sign);
    DB::table('sign_table4')->insert($sign4);
    DB::table('sign_table5')->insert($sign5);
    DB::table('sign_table6')->insert($sign6);
    DB::table('sign_table7')->insert($sign7);
    DB::table('sign_table8')->insert($sign8);
    DB::table('sign_table9')->insert($sign9);
    DB::table('sign_table10')->insert($sign10);
    DB::table('sign_table11')->insert($sign11);
    DB::table('sign_table12')->insert($sign12);
    DB::table('sign_table13')->insert($sign13);
    DB::table('sign_table14')->insert($sign14);
    $result2 = DB::table('sign_laboratory')->insert($sign_laboratory);
    if ($result1 && $result2) {
        $patient['update_time'] = date('Y-m-d H:i:s', time());
        DB::table('patient')->where(['patient_id' => $patient_id])->update($patient);
        echo json_encode(['code' => 1, 'msg' => '修改成功']);
        die;
    } else {
        echo json_encode(['code' => 2, 'msg' => '修改失败']);
        die;
    }
}

当时我的表情是这样的,这Tm什么操作

u=545870533,2273922327&fm=26&gp=0.jpg


你以为这就结束了?

public function addFollowUp()
{
    $treatment1 = request()->post('treatment1');
    $treatment2 = request()->post('treatment2');
    $treatment3 = request()->post('treatment3');
    $treatment4 = request()->post('treatment4');
    $treatment5 = request()->post('treatment5');
    $treatment6 = request()->post('treatment6');
    $treatment7 = request()->post('treatment7');
    $treatment8 = request()->post('treatment8');
    $treatment9 = request()->post('treatment9');
    $treatment10 = request()->post('treatment10');
    $treatment11 = request()->post('treatment11');
    $treatment12 = request()->post('treatment12');
    $treatment13 = request()->post('treatment13');
    $treatment14 = request()->post('treatment14');
    $treatment15 = request()->post('treatment15');
    $treatment16 = request()->post('treatment16');
    $treatment17 = request()->post('treatment17');
    $treatment18 = request()->post('treatment18');
    $treatment19 = request()->post('treatment19');
    $treatment20 = request()->post('treatment20');
    $newTreatment1 = array();
    $newTreatment2 = array();
    $newTreatment3 = array();
    $newTreatment4 = array();
    $newTreatment5 = array();
    $newTreatment6 = array();
    $newTreatment7 = array();
    $newTreatment8 = array();
    $newTreatment9 = array();
    $newTreatment10 = array();
    $newTreatment11 = array();
    $newTreatment12 = array();
    $newTreatment13 = array();
    $newTreatment14 = array();
    $newTreatment15 = array();
    $newTreatment16 = array();
    $newTreatment17 = array();
    $newTreatment18 = array();
    $newTreatment19 = array();
    $newTreatment20 = array();
    foreach ($treatment1 as $k => $v) {
        $newTreatment1[$v['name']] = $v['value'];
    }
    foreach ($treatment2 as $k => $v) {
        $newTreatment2[$v['name']] = $v['value'];
    }
    foreach ($treatment3 as $k => $v) {
        $newTreatment3[$v['name']] = $v['value'];
    }
    foreach ($treatment4 as $k => $v) {
        $newTreatment4[$v['name']] = $v['value'];
    }
    foreach ($treatment5 as $k => $v) {
        $newTreatment5[$v['name']] = $v['value'];
    }
    foreach ($treatment6 as $k => $v) {
        $newTreatment6[$v['name']] = $v['value'];
    }
    foreach ($treatment7 as $k => $v) {
        $newTreatment7[$v['name']] = $v['value'];
    }
    foreach ($treatment8 as $k => $v) {
        $newTreatment8[$v['name']] = $v['value'];
    }
    foreach ($treatment9 as $k => $v) {
        $newTreatment9[$v['name']] = $v['value'];
    }
    foreach ($treatment10 as $k => $v) {
        $newTreatment10[$v['name']] = $v['value'];
    }
    foreach ($treatment11 as $k => $v) {
        $newTreatment11[$v['name']] = $v['value'];
    }
    foreach ($treatment12 as $k => $v) {
        $newTreatment12[$v['name']] = $v['value'];
    }
    foreach ($treatment13 as $k => $v) {
        $newTreatment13[$v['name']] = $v['value'];
    }
    foreach ($treatment14 as $k => $v) {
        $newTreatment14[$v['name']] = $v['value'];
    }
    foreach ($treatment15 as $k => $v) {
        $newTreatment15[$v['name']] = $v['value'];
    }
    foreach ($treatment16 as $k => $v) {
        $newTreatment16[$v['name']] = $v['value'];
    }
    foreach ($treatment17 as $k => $v) {
        $newTreatment17[$v['name']] = $v['value'];
    }
    foreach ($treatment18 as $k => $v) {
        $newTreatment18[$v['name']] = $v['value'];
    }
    foreach ($treatment19 as $k => $v) {
        $newTreatment19[$v['name']] = $v['value'];
    }
    foreach ($treatment20 as $k => $v) {
        $newTreatment20[$v['name']] = $v['value'];
    }

    $patient_id = $newTreatment1['patient_id'];
    $patient['update_time'] = date('Y-m-d H:i:s', time());
    DB::table('patient')->where(['patient_id' => $patient_id])->update($patient);

    $newTreatment1['follow_up_num'] = 1;
    $newTreatment2['follow_up_num'] = 2;
    $newTreatment3['follow_up_num'] = 3;
    $newTreatment4['follow_up_num'] = 4;

    $newTreatment5['follow_up_num'] = 5;
    $newTreatment6['follow_up_num'] = 6;
    $newTreatment7['follow_up_num'] = 7;
    $newTreatment8['follow_up_num'] = 8;
    $newTreatment9['follow_up_num'] = 9;
    $newTreatment10['follow_up_num'] = 10;
    $newTreatment11['follow_up_num'] = 11;
    $newTreatment12['follow_up_num'] = 12;
    $newTreatment13['follow_up_num'] = 13;
    $newTreatment14['follow_up_num'] = 14;
    $newTreatment15['follow_up_num'] = 15;
    $newTreatment16['follow_up_num'] = 16;
    $newTreatment17['follow_up_num'] = 17;
    $newTreatment18['follow_up_num'] = 18;
    $newTreatment19['follow_up_num'] = 19;
    $newTreatment20['follow_up_num'] = 20;
    $result1 = DB::table('follow_up')->insert($newTreatment1);
    $result2 = DB::table('follow_up')->insert($newTreatment2);
    $result3 = DB::table('follow_up')->insert($newTreatment3);
    $result4 = DB::table('follow_up')->insert($newTreatment4);
    $result5 = DB::table('follow_up')->insert($newTreatment5);
    $result6 = DB::table('follow_up')->insert($newTreatment6);
    $result7 = DB::table('follow_up')->insert($newTreatment7);
    $result8 = DB::table('follow_up')->insert($newTreatment8);
    $result9 = DB::table('follow_up')->insert($newTreatment9);
    $result10 = DB::table('follow_up')->insert($newTreatment10);
    $result11 = DB::table('follow_up')->insert($newTreatment11);
    $result12 = DB::table('follow_up')->insert($newTreatment12);
    $result13 = DB::table('follow_up')->insert($newTreatment13);
    $result14 = DB::table('follow_up')->insert($newTreatment14);
    $result15 = DB::table('follow_up')->insert($newTreatment15);
    $result16 = DB::table('follow_up')->insert($newTreatment16);
    $result17 = DB::table('follow_up')->insert($newTreatment17);
    $result18 = DB::table('follow_up')->insert($newTreatment18);
    $result19 = DB::table('follow_up')->insert($newTreatment19);
    $result20 = DB::table('follow_up')->insert($newTreatment20);
    if ($result1) {
        return json_encode(['code' => 1, 'msg' => '添加成功']);
    } else {
        return json_encode(['code' => 2, 'msg' => '添加失败']);
    }
}

这Tm代码有毒啊,反正到今天我已经维护这个项目两周了,越来越感觉自己不适合做开发.....

之前不相信代码能杀人,今天我信了,维护的我头晕眼花,感觉都抑郁了

其实把代码贴出来并没有说贬低别人的意思,只是觉得此人的代码风格我实在无法忍受了,真的难以维护

没有一行注释,从不用循环,全都是复制粘贴,数据库命名一言难尽,数据类型全都是varchar255.......

用的laravel框架,却全部都是面向过程的写法,几乎都是原生

在此还是希望各位同行,不要再写这种代码了,太Tm恶心了,或许受自身水平限制,但是态度总要端正下吧,注释该有还是要有,写代码之前多考虑考虑强复用,低耦合

  • 评论列表
  •  访客
     发布于 2020-01-11 19:29:07  回复该评论
  • 哈哈,看着很整齐,估计是非科班出身人

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

版权所有 | 转载请标明出处