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

可以说,我确实没在工作中用过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});
        }
    }
}
相关推荐
liliangcsdn12 分钟前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
CssHero15 分钟前
基于vue3完成领域模型架构建设
前端
PanZonghui18 分钟前
用项目说话:我的React博客构建成果与经验复盘
前端·react.js·typescript
言兴22 分钟前
教你如何理解useContext加上useReducer
前端·javascript·面试
sunbyte25 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
前端的日常26 分钟前
网页视频录制新技巧,代码实现超简单!
前端
前端的日常28 分钟前
什么是 TypeScript 中的泛型?请给出一个使用泛型的示例。
前端
今禾29 分钟前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
ccc101832 分钟前
老师问我localhost和127.0.0.1,有什么区别?
前端
我不要放纵35 分钟前
LVS集群搭建
linux·服务器·lvs