一个工作了 6 年的同事写的代码,不看 git 记录我还以为是实习生写的

2020-09-27 09:58:01 +08:00
 garlics

第一次见到那么离谱的代码,完全没有封装的意思。

        if ($config['theme'] == 1) {
            if ($config['plugin']['PG_SHOPPING_CART']) {
                $tabIndex = [
                    'index' => 0,
                    'collection' => 1,
                    'bestforyou' => 2,
                    'cart' => 3,
                    'mine' => 4
                ];
                $tablist = [
                    [
                        'pagePath' => '/pages/index/index',
                        'text' => '首页',
                        'iconPath' => '/assets/images/home_unactived.png',
                        'selectedIconPath' => '/assets/images/theme1_home_actived.png',
                    ],
                    [
                        "pagePath" => "/pages/collection/collection",
                        "iconPath" => "/assets/images/collection.png",
                        "selectedIconPath" => "/assets/images/collection_actived.png",
                        "text" => "收藏"
                    ],
                    [
                        "pagePath" => "/pages/bestforyou/bestforyou",
                        "iconPath" => "/assets/images/bestforyou_unactived.png",
                        "selectedIconPath" => "/assets/images/bestforyou_actived.png",
                        "text" => "为你优选"
                    ],
                    [
                        'pagePath' => '/pages/cart/cart',
                        'text' => '购物车',
                        'iconPath' => '/assets/images/cart_unactived.png',
                        'selectedIconPath' => '/assets/images/cart_actived.png',
                    ],
                    [
                        'pagePath' => '/pages/mine/mine',
                        'text' => '我的',
                        'iconPath' => '/assets/images/mine_unactived.png',
                        'selectedIconPath' => '/assets/images/mine_actived.png',
                    ]
                ];
            } else {
                $tabIndex = [
                    'index' => 0,
                    'collection' => 1,
                    'bestforyou' => 2,
                    'mine' => 3
                ];
                $tablist = [
                    [
                        'pagePath' => '/pages/index/index',
                        'text' => '首页',
                        'iconPath' => '/assets/images/home_unactived.png',
                        'selectedIconPath' => '/assets/images/theme1_home_actived.png',
                    ],
                    [
                        "pagePath" => "/pages/collection/collection",
                        "iconPath" => "/assets/images/collection.png",
                        "selectedIconPath" => "/assets/images/collection_actived.png",
                        "text" => "收藏"
                    ],
                    [
                        "pagePath" => "/pages/bestforyou/bestforyou",
                        "iconPath" => "/assets/images/bestforyou_unactived.png",
                        "selectedIconPath" => "/assets/images/bestforyou_actived.png",
                        "text" => "为你优选"
                    ],
                    [
                        'pagePath' => '/pages/mine/mine',
                        'text' => '我的',
                        'iconPath' => '/assets/images/mine_unactived.png',
                        'selectedIconPath' => '/assets/images/mine_actived.png',
                    ]
                ];
            }
        } else {
            if ($config['plugin']['PG_SHOPPING_CART']) {
                $tabIndex = [
                    'index' => 0,
                    'cart' => 1,
                    'mine' => 2
                ];
                $tablist = [
                    [
                        'pagePath' => '/pages/index/index',
                        'text' => '首页',
                        'iconPath' => '/assets/images/home_unactived.png',
                        'selectedIconPath' => '/assets/images/home_actived.png',
                    ],
                    [
                        'pagePath' => '/pages/cart/cart',
                        'text' => '购物车',
                        'iconPath' => '/assets/images/cart_unactived.png',
                        'selectedIconPath' => '/assets/images/cart_actived.png',
                    ],
                    [
                        'pagePath' => '/pages/mine/mine',
                        'text' => '我的',
                        'iconPath' => '/assets/images/mine_unactived.png',
                        'selectedIconPath' => '/assets/images/mine_actived.png',
                    ]
                ];
            } else {
                $tabIndex = [
                    'index' => 0,
                    'mine' => 1
                ];
                $tablist = [
                    [
                        'pagePath' => '/pages/index/index',
                        'text' => '首页',
                        'iconPath' => '/assets/images/home_unactived.png',
                        'selectedIconPath' => '/assets/images/home_actived.png',
                    ],
                    [
                        'pagePath' => '/pages/mine/mine',
                        'text' => '我的',
                        'iconPath' => '/assets/images/mine_unactived.png',
                        'selectedIconPath' => '/assets/images/mine_actived.png',
                    ]
                ];
            }
        }
29850 次点击
所在节点    程序员
298 条回复
tcpdump
2020-09-27 09:59:43 +08:00
起码挺工整 ,占楼,看楼下大神指点
fffflyfish
2020-09-27 09:59:50 +08:00
你懂啥,不把 baseline 打低一点怎么优化啊
wizardoz
2020-09-27 10:01:49 +08:00
你以为他是手写的,其实他可能是用模版生成的。
MuscleOf2016
2020-09-27 10:02:19 +08:00
咋优化?提取公共写常量?
tabris17
2020-09-27 10:02:22 +08:00
都写 PHP 了,还封装啥呀,先完成 KPI 才是正道
raincode
2020-09-27 10:03:33 +08:00
起码变量名挺规范 ,占楼,看楼下大神指点
oukichi
2020-09-27 10:03:48 +08:00
你可以优化一下然后贴出来,这样不但提高自己而且帮助别人。毕竟吐槽大家都会,但解决问题的人就少了。
warlock
2020-09-27 10:04:41 +08:00
如果是模板生成 那么应该加上 Code generated by xxxxx. DO NOT EDIT. 避免误会
zhoushushu
2020-09-27 10:05:14 +08:00
我也经常这么写,过度封装,接手的人还得琢磨。
futou
2020-09-27 10:05:17 +08:00
#7 +1,等待优化结果
7654
2020-09-27 10:05:33 +08:00
看不爽有空就给他改了啊
victor
2020-09-27 10:06:36 +08:00
sunznx
2020-09-27 10:06:38 +08:00
@tabris17 这都能黑,你是有病吗
eGlhb2Jhb2Jhbw
2020-09-27 10:07:03 +08:00
有些单引号,有些双引号。有些最后元素带逗号,有些最后元素不带。强迫症就很难受。代码除了长点,问题不是很大,也比较容易阅读(当然与业务有一定关系),感觉没你说的那么严重。
jadehare
2020-09-27 10:07:29 +08:00
这可太工整了,也就重复代码太多,感觉你还是年轻了,没见过嵌套几层的 if
revalue
2020-09-27 10:08:04 +08:00
#7 +2,等待优化结果

表格驱动也不大行
ingdawn
2020-09-27 10:09:03 +08:00
坐等大佬封装方案
haohappy
2020-09-27 10:09:12 +08:00
起码挺工整 ,遇到事逼了
RangerWolf
2020-09-27 10:10:16 +08:00
楼主,把你优化之后的发出来让大家学习学习?
garlics
2020-09-27 10:10:43 +08:00
@fffflyfish
@oukichi
@MuscleOf2016
因为要改加些功能,所以我整理了下这段代码,个人认为看起来逻辑清晰很多,也希望有大神有更好的方法改
```
$map = [
'index' => [
'pagePath' => '/pages/index/index',
'text' => '首页',
'iconPath' => '/assets/images/home_unactived.png',
'selectedIconPath' => '/assets/images/home_actived.png',
],
'cart' => [
'pagePath' => '/pages/cart/cart',
'text' => '购物车',
'iconPath' => '/assets/images/cart_unactived.png',
'selectedIconPath' => '/assets/images/cart_actived.png',
],
'mine' => [
'pagePath' => '/pages/mine/mine',
'text' => '我的',
'iconPath' => '/assets/images/mine_unactived.png',
'selectedIconPath' => '/assets/images/mine_actived.png',
],
'collection' => [
"pagePath" => "/pages/collection/collection",
"iconPath" => "/assets/images/collection.png",
"selectedIconPath" => "/assets/images/collection_actived.png",
"text" => "收藏"
],
'bestforyou' => [
"pagePath" => "/pages/bestforyou/bestforyou",
"iconPath" => "/assets/images/bestforyou_unactived.png",
"selectedIconPath" => "/assets/images/bestforyou_actived.png",
"text" => "为你优选"
],
];
$tab = ['index'];
if ($config['theme'] == 1) {
$tab[] = 'collection';
$tab[] = 'bestforyou';
}
if ($config['plugin']['PG_SHOPPING_CART']) {
$tab[] = 'cart';
}

$tab[] = 'mine';
$tabIndex = array_flip($tab);
$tablist = array_map(function ($v) use ($map) {
return $map[$v];
}, $tab);
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/710808

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX