一个工作了 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',
                    ]
                ];
            }
        }
29888 次点击
所在节点    程序员
298 条回复
soulmt
2020-09-27 20:00:28 +08:00
@charlie21 对啊,技术也不要进步了,搞那么多好用的东西干什么,大家都用原始的写法往上堆把,多好。简单易懂,各平台通用,谁都看得懂,一把梭就对了。
geying
2020-09-27 20:07:05 +08:00
又不是不能用.jpg
gravitybox
2020-09-27 21:04:23 +08:00
觉得逻辑不是很复杂,就是写死在代码中的配置看着不太间接。如果是从数据存储加载配置,整个逻辑应该会清晰很多。
levelworm
2020-09-27 21:07:34 +08:00
实习生其实反而可能比较注意这个
abcbuzhiming
2020-09-27 21:47:55 +08:00
作为一个已经写了 10 年的人,我得说,非长期性的,大型项目,动不动把封装搬出来是有毛病。
GiantHard
2020-09-27 22:12:56 +08:00
@a719031256 代码量大的话,长函数也很容易让人梳理着梳理着就找不到回去的路的。其实这个所谓的“短函数更好可以从复杂度来看待”,Sonar Lint 里面有个“认知复杂度”的检查项,长函数往往容易写成认知复杂度很高的样子,再加上函数内部变量在几百行、甚至上千行的代码里面到处被引用,这样后续修改维护起来真的是非常的痛苦。
ooee2016
2020-09-27 22:22:27 +08:00
很多时候就是临时让加,也没那么多时间每个都考虑下怎么优化,咔咔 if else 复制粘贴 5 分钟搞定.
ruandao
2020-09-27 22:34:29 +08:00
其实有时候会接受这样的代码
因为,开发很多都在赶工,有时候,小朋友只懂得套模板,嵌套太多层并不好

看你是愿意牺牲休息时间去教还是愿意直接扔给他套模板
至于招厉害点的,预算就那么多。。。
salmon5
2020-09-27 22:46:04 +08:00
如果是 9106,写的代码不好可以理解;就怕有人 955,代码质量不行。
ajaxfunction
2020-09-27 23:35:50 +08:00
这很明显就是首次写代码,根本没有提到会有新皮肤功能,
某天需要加一个节日换皮肤,于是加了一个 if else
过某个节日又要加一个皮肤 于是再次 else if
直到节日越来越多,if else 皮肤就更多了

至于你说封装的事? 用 php 肯定公司规模肯定不算大,明天活动就上线了,今天还在筹备活动代码,哪有人手和时间优化封装啊?
anerevol
2020-09-27 23:42:13 +08:00
// 不同主题的配置最好分开
$themeAllTabs = [
'default' => [
'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',
]
],
1 => [
'index' => [
'pagePath' => '/pages/index/index',
'text' => '首页',
'iconPath' => '/assets/images/home_unactived.png',
'selectedIconPath' => '/assets/images/theme1_home_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" => "为你优选"
],
'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',
]
]
];

$themeDisplayTabs = [
'default' => [
'index',
'mine'
],
1 => [
'index',
'collection',
'bestforyou',
'mine'
]
];

$themePluginActions = [
'default' => [
'PG_SHOPPING_CART' => function($tabs) {
array_splice( $tabs, 1, 0, 'cart');
return $tabs;
}
],
'1' => [
'PG_SHOPPING_CART' => function($tabs) {
array_splice($tabs, 3, 0, 'cart');
return $tabs;
}
]
];

$theme = $config['theme'] == 1 ? 1 : 'default';
$tabNames = $themeDisplayTabs[$theme];
foreach($config['plugin'] as $plugName => $available) {
$plugAction = $themePluginActions[$theme][$plugName] ?? null;
if ($available && $plugAction != null) {
$tabNames = $plugAction($tabNames);
}
}

$tabIndex = array_flip($tabNames);
$tablist = array_map(function ($tabName) use ($theme, $themeAllTabs) {
return $themeAllTabs[$theme][$tabName];
}, $tabNames);
mht
2020-09-27 23:53:47 +08:00
这代码的确有优化下的余地 太难看了 强迫症很难受啊
falcon05
2020-09-28 00:02:37 +08:00
这种导航 /菜单的设定在 wordpress 里都成了一个内置的功能,灵活性非常强
sorshion
2020-09-28 00:10:51 +08:00
如果只是完成业务,那这个代码没毛病,如果想要自身再精进些,那么就需要改进。
mulog
2020-09-28 03:14:37 +08:00
我本来没仔细看代码 爬楼看评论说呀为了实现功能啊能跑就行啊觉得还挺有道理
回头看了一眼代码,这也实在太过分了。。
594duck
2020-09-28 03:17:10 +08:00
@jackrelative 老哥说的靠谱,奋斗小年轻们都觉得自己天下第一,要吃过两次憋后还不一定学会。觉得是老东西们坑害自己,要跳到创业公司去,再被好好教育下才能认清本质。

就像小年轻觉得最新语言才能解决问题,而殊不知软件工程师系统工程。那帮奋斗小年轻们就喜欢看着诸如
“微软这臭傻逼写出来的代码连我不入,微软的实习生写出来的臭傻逼代码也能发布,连我不如”

问题是人家从系统整体上来看。

我见过很小的一个系统 3 台 vm 搞定的,硬是给你弄 k8s 。还要自建,最后玩出了 8 台 vm 。然后建立一写跳槽跑路。
594duck
2020-09-28 03:22:25 +08:00
顺便我这里哪怕不友好都要说一下

站楼主的都是那些“二次元头像”居多,就和 twitter 上一样奇怪的。
594duck
2020-09-28 03:29:15 +08:00
@reus
@Nicoco

2 位大佬希望你们在日后的日子能被人用“同样的态度”,“积极对待”。

为了能够关注你们日后的日子我已经加了你们的特别好友。


为了能了解两位日后的日子,我会多给两位奋斗着点赞保持你们有足够的 v 币
good1uck
2020-09-28 04:30:59 +08:00
@skinny 您说的是哪类程序员?这里面主要分为两类,一类支持楼主,另一类不支持
catcn
2020-09-28 06:38:44 +08:00
一看,原来是 php 大法。。

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

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

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

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

© 2021 V2EX