一个工作了 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',
                    ]
                ];
            }
        }
29395 次点击
所在节点    程序员
298 条回复
afx
2020-09-27 11:39:19 +08:00
@garlics 那是因为大多数人都这么写,所以他们觉得没有问题。
northisland
2020-09-27 11:40:17 +08:00
@Inside 你这个货要是真的天天 review 你 team 的业务代码,你绝对不会表现的这么少见多怪。
gaorenhua
2020-09-27 11:42:04 +08:00
代码木有好坏,功能实现才是王道。
dustin2016
2020-09-27 11:42:26 +08:00
以代码来评价,本身就是小白的行为
1.一天开几次会?
2.天天加班否?
3.需求疯狂变更否?
4.代码前任的前任的前任哪家去高就了?

一个六年的同事,这样的代码已经离开了器,成就道的水平了
ashine
2020-09-27 11:43:15 +08:00
最离谱还是 有 notEmpty() 又有 notZero(),notNull() 这样的封装,看代码才叫混乱,宁愿看多几行代码也不愿去查怎么实现的
KuroNekoFan
2020-09-27 11:43:38 +08:00
反正就是 要啥自行车
要么就是 别人都这么搞
weirdo
2020-09-27 11:44:30 +08:00
又不是不能用 [手动滑稽
TuringGunner
2020-09-27 11:46:57 +08:00
这个显然是因为需求多,没时间重构造成的啊
dustinth
2020-09-27 11:47:00 +08:00
很多老程序员急是因为见得多了那些菜鸟程序员过早优化封装, 结果就是还不如不封装; 写代码跟做其他事情一样讲究的是解决主要矛盾, 在我看来这段代码逻辑简单, 也不是什么核心代码, 楼主纠结于此并说很离谱(这段代码确实写的是不怎么样, 我可能也会顺手改了, 但不会太苛责), 那么在面对复杂的核心问题超过楼主的能力时, 这种凡是要抽象封装的理念只会带来错误和难以维护的设计, 危害会比更少设计来得大.
说到 Code Review, 讲一个笑话: 一个人去 update 一个开源代码, update 几行的时候几个核心程序员讨论了半天才 Approve; 当 update 超过几百行的时候十分钟(run 完 unit test)就接受了.
zoharSoul
2020-09-27 11:47:03 +08:00
这代码逻辑挺清晰的, 是人都能看懂.

比那强行瞎抽象封装的好多了.
matthewzhong
2020-09-27 11:47:30 +08:00
我觉得这个真的要批斗一下才行
kemikemian
2020-09-27 11:48:18 +08:00
楼主是差异工作了 6 年居然这么写,还是工作了 6 年只会这么写
不管你工作了几年,老板改一个需求你就要推倒重写的时候,你是否还会坚持花时间去优化代码,让代码更优雅?
sherlockwhite
2020-09-27 11:51:54 +08:00
@ashine 同感,我司老板封装的方法,里面就一句查询数据库的,我都要看下是什么,我都吐了。。。
jiankaikey
2020-09-27 11:52:02 +08:00
$config['theme'] = 1;
$config['plugin']['PG_SHOPPING_CART'] =1;

$how = 10000000;

$time1 = time();
for($i=0;$i<$how;$i++){
test1($config); //他的代码
}
echo time() - $time1."\n";


$time2 = time();
for($i=0;$i<$how;$i++){
test2($config); //你的代码
}
echo time() - $time2;

别不服气,你的代码慢 10 倍
newmlp
2020-09-27 11:52:33 +08:00
我觉得挺好的,清晰易懂
NoobX
2020-09-27 11:52:52 +08:00
虽然不算优雅,但不算糟糕的代码。变量名清晰,缩进明了,唯一的缺点可能是长了点?
真正糟糕的代码是变量名函数名乱起、看不懂、到处嵌套在一起,改个 bug 可以改半天甚至无从下手维护的代码
dustinth
2020-09-27 11:54:47 +08:00
@jiankaikey 在这里, 效率并不是需要考虑的主要问题.
northisland
2020-09-27 11:55:12 +08:00
隔行如隔山。我确实不懂世界上最好的语言。


看起来像是修改一段 json 。。。你吐槽的同事把 json 内容重复了好多遍。日后改动、扩展要重复好多遍。

但是,我对你的同事表示理解。。。


如果你按照书本来,创建大量几乎同样的对象,并且便于日后改动,学过《设计模式》的话,你应该知道要用原型模式或者工厂模式,
写一个 Prototype 类或者 AbstractFactory 类,所有的条件都对应一个子类。
forgottencoast
2020-09-27 11:59:26 +08:00
@gaorenhua 103#
这就是瞎扯了。
维护成本才是软件开发成本占比最大的部分。
zengming00
2020-09-27 12:06:41 +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