写了十几年程序,今天才第一天知道什么是屎山代码

可以说,我确实没在工作中用过Javascript,因为我从未见过如此"厚颜无耻"的代码

我曾经也是学过2~3年,还是JQuery的年代,但应该确实没在实战中用过,否则我怎么会不记得写过这些屎山代码的???

(当年,第一个吃到螃蟹的,赚到第一桶金的是DICUZ,PHP的;真还不是写JAVASCRIPT的)

一个看上去平平无奇,像模像样的类Js代码

代码1

cs 复制代码
            recipeSection.Add(recipeRow);
            //在上面套用 preset??:自己写的方法:VisualElementPresets.CreateStandardCategoryEditor(),作用是再套了个 MemberGroupControl()
            //就是用category 绘制
            ve.Add(new Label("Categories(强行套用Category 的[Inspector])").CenterContents());
            ve.Add(groupControl);//group == category ,就是用 categoriesCtl 做绘制。。。。。
            ve.Add(recipeSection);   
            return ve; 

结果某一天。。。。。

要是,你只是想在Unity VisualElement UI 添加一个按钮 Button

代码1-1

cs 复制代码
            recipeSection.Add(recipeRow);
            //在上面套用 preset??:自己写的方法:VisualElementPresets.CreateStandardCategoryEditor(),作用是再套了个 MemberGroupControl()
            //就是用category 绘制
            ve.Add(new Label("Categories(强行套用Category 的[Inspector])").CenterContents());
            ve.Add(groupControl);//group == category ,就是用 categoriesCtl 做绘制。。。。。
            ve.Add(recipeSection);
            //在这"再添加一个操作按钮吧,测试GridView"));
            var testButton = new Button(() => {
                //这是一个构造函数??(不是,TMD这是一个clickEvent,不是构造函数。。。。)
                // 下面的{text = "xxxxx"},这个描述语言,TMD才是构造函数,就问你惊喜不惊喜,意外不意外???
                // 下面这么写C# 构造函数,也是有不少的。。。。。
                ///
                ///
                //可以插入一万行代码,事实上 Init的代码就不会少。。。。
            }){ text = "测试GridView(ItemObjectEditor)" };//这里加的 text 描述。。。。
            ve.Add(testButton);
            return ve;

如果我什么都不说,一般人会不会看到这样的代码就给我扣帽子:"这样的代码,大牛写的真好啊,就是不同于常人,非常飘逸,肯定另有深意,简直的"

写JavaScript的都是什么人?面对这样的屎山代码,这些年,你们都是怎么过 的??

我好像曾经在2~3个团队,明令禁止这么写代码

而必须规范地写成如下:

但其实再多地外部规范,远不及代码本身底层规则 ,很快这些规范就不会被执行,也多此一举立规范,和立FLAG 是差不多的逻辑,根本不要立FLAG

"//--------------------"

当然,我的文章都写很多废话的

看,这又是一句废话

就问你,烦不烦

"//----------------------/"

"//----------------------// 又是一行废话,备注 //"

代码2

cs 复制代码
            recipeSection.Add(recipeRow);
            //在上面套用 preset??:自己写的方法:VisualElementPresets.CreateStandardCategoryEditor(),作用是再套了个 MemberGroupControl()
            //就是用category 绘制
            ve.Add(new Label("Categories(强行套用Category 的[Inspector])").CenterContents());
            ve.Add(groupControl);//group == category ,就是用 categoriesCtl 做绘制。。。。。
            ve.Add(recipeSection);
            //在这"再添加一个操作按钮吧,测试GridView"));
            var testButton = new Button(){ text = "测试GridView(ItemObjectEditor)" };//这里加的 text 描述。。。。
            testButton.addClick(OnClick);//
                                         //写成EventWarpper(testButton).click += XXXX 都好
                                         //写多少行都行,都比较好
                                         //就是一行就是一个功能
                                         //是一行就行,重点不在于行数太多,而是每一行需要意义明确
                                         //千万不要学Json 无意义的括号这种 {  | =》 | });         
            ve.Add(testButton);
            return ve;


//无论是调试,还是规范,还是可阅读性,还是编辑器friendly
//都比Javascript 要好
//而 Javascript 为什么越看越像屎山代码????
//这个世界上,其实没那么屎山代码
// 30%的JavaScript 容易产生屎山代码,而30%在比例用Js,这个比例就很恐怖了
//当然我不靠Js赚钱,所以怎么吐槽都行。。。。。
function OnClick(eve)
{

}

//------------------ /

最终改成这样,是不是舒服很多???

代码2-final

cs 复制代码
            //。。。。。。。。。。

            recipeSection.Add(recipeRow);
            //在这"再添加一个操作按钮吧,测试GridView"));
            var testButton = new Button(testButtonClick);
            testButton.text = "测试GridView(ItemObjectEditor)";//这里加的 text 描述。。。。

            //在上面套用 preset??:自己写的方法:VisualElementPresets.CreateStandardCategoryEditor(),作用是再套了个 MemberGroupControl()
            //就是用category 绘制
            ve.Add(new Label("Categories(强行套用Category 的[Inspector])").CenterContents());
            ve.Add(groupControl);//group == category ,就是用 categoriesCtl 做绘制。。。。。
            ve.Add(recipeSection);
            //在这"再添加一个操作按钮 baabababa"
            ve.Add(testButton);
            return ve;
        }
        void testButtonClick()
        { 
            
        }

一个乱套,而崩坏的世界:

代码3

cs 复制代码
using System;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace Polyperfect.Crafting.Edit
{
    public class OpenForEditManipulator : Clickable
    {
        /// <summary>
        /// ObjectEditWindow.cs 点击item事件;会创建一个 window
        /// </summary>
        /// <param name="getObject"></param>
        /// <param name="clickCount"></param>
        /// <param name="ve">这个 ve 会乱动,所以需要 refreshAction()兜底刷新</param>
        /// <param name="refreshAction"></param>
        public OpenForEditManipulator(System.Func<Object> getObject, int clickCount,VisualElement ve, Action<VisualElement,Object> refreshAction = null)
            : base(() =>ObjectEditWindow.CreateForObject(getObject(), ve,refreshAction))//又是一个强行闭包
            
        {
//            UnityEngine.Debug.LogError("fffff click??");//一个点击,触发了15次。。。一个compile刷新 ,又执行了45次。。。。。
            //一共触发了60次。。。。
            //一共触发了60次。。。。
            //一共触发了60次。。。。
            //一共触发了60次。。。。.......
            //一共触发了60次。。。。.......
            //一共触发了60次。。。。..............
            //而且是多次触发。。。。
            //而且是多次触发。。。。
            //而且是多次触发。。。。
            //这是 一个构造函数,你敢想像
            //这是 一个构造函数,你敢想像
            //这是 一个构造函数,你敢想像
            activators.Clear();
            activators.Add(new ManipulatorActivationFilter {button = MouseButton.LeftMouse, clickCount = clickCount});
        }
    }
}
相关推荐
忘了ʷºᵇₐ3 小时前
在IDEA 2024.1版本中如何打开Remote Host及连接linux
linux·运维·服务器
freewlt6 小时前
前端性能优化实战:从 Lighthouse 分数到用户体验的全面升级
前端·性能优化·ux
0xDevNull6 小时前
Java反射机制深度解析:从原理到实战
java·开发语言·后端
小小亮016 小时前
Next.js基础
开发语言·前端·javascript
华洛6 小时前
我用AI做了一个48秒的真人精品漫剧,不难也不贵
前端·javascript·后端
华科易迅6 小时前
MybatisPlus增删改查操作
android·java·数据库
standovon7 小时前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
Novlan17 小时前
我把 Claude Code 里的隐藏彩蛋提取出来了——零依赖的 ASCII 虚拟宠物系统
前端
IAUTOMOBILE7 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi7 小时前
PostgreSQL版本选择
java