凉鞋:我所理解的框架 【Unity 游戏框架搭建】 (5)

在笔者刚毕业的时候,读了《代码大全》这一本书,其中第 11 章的《变量名的力量》反复读了三遍。这一章的内容对笔者之后养成良好的命名习惯产生很大的影响。书中重点讲了如何命名和编码规范的重要性。

编码规范

笔者很认同做项目要遵循命名规范这个准则的。但是现实是规范的执行过程中会遇到很多阻碍。比如笔者最开始去网上找了一个编码规范文档。一份文档 50 多页,看都觉得很痛苦,只好放弃。最终经过一段时间探索之后得出了一个结论:编码规范只要解决问题就好,其他的尽量确保容易遵守就好。

编码规范解决的是什么问题呢?

减少项目交接时,由于代码风格水土不服所带来的风险。

当某个项目人力不足时,可以减少加人时所带来的人力浪费(可以让一个人花更少的时间去看懂某个项目的代码)。

防止项目过了一段时间一些实现自己都看不懂了。

以上当然要靠一个编码规范是无法完全解决的,除了编码规范之外,还有资源命名规范,项目结构规范等等。
经过多次因为以上原因的加班之后,深有感触。编码规范是非常有必要做的。

为自己制定一个编码规范

在公司,笔者还是最基础的员工,没有什么权利。所以对于定义规范这种事情,在公司想想就好了。不过这并不阻碍笔者为自己制定一个编码规范。于是笔者根据自己的编码习惯,定制了如下的编码规范。

/**************************************************************************** * Copyright (c) 2017 liangxieq * * https://github.com/liangxiegame/QCSharpStyleGuide * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ****************************************************************************/ namespace QFramework.Example { using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; /// <summary> /// 展示编码风格 /// </summary> public class ProgrammingStyle : MonoBehaviour { #region Basic App /// <summary> /// 1.private/protected使用m开头+驼峰式 /// 2.前缀最好展示所属的Component类型比如Button->Btn /// </summary> [SerializeField] Button mBtnEnterMainPage; /// <summary> /// public类型使用首字母大写驼峰式 /// </summary> public int LastIndex = 0; /// <summary> /// public 类型属性也算public类型变量 /// </summary> public int CurSelectIndex { get { return mCurSelectIndex; } } void Start () { mBtnEnterMainPage = transform.Find ("BtnEnterMainPage").GetComponent<Button>(); // GameObject命名 // 临时变量命名采用首字母小写驼峰式 GameObject firstPosGo = transform.Find ("FirstPosGo").gameObject; } /// <summary> /// 方法名一律首字母大写驼峰式 /// </summary> public void Hide() { gameObject.SetActive (false); } #endregion #region Advanced /* * GameObject->Go * Transform->Trans * Button->Btn * * For->4 * To->2 * Dictionary->Dict * Number->Num * Current->Cur */ /// <summary> /// 1.Bg肯定是图片 /// </summary> [SerializeField] Image mBg; /// <summary> /// GameObject->Go /// </summary> [SerializeField] GameObject mDialogGo; /// <summary> /// Transfom->Trans /// </summary> [SerializeField] Transform mScrollViewTrans; /// <summary> /// Index、Num、Count等肯定是int /// </summary> [SerializeField] int mCurSelectIndex; /// <summary> /// RectTransform->RectTrans; /// </summary> [SerializeField] RectTransform mScrollContentRectTrans; /// <summary> /// 1.Pos肯定是Vector3、Vector2 /// 2.Size肯定是Vector2 /// </summary> [SerializeField] Vector3 mCachedPos; [SerializeField] Vector2 mCachedSize; /// <summary> /// 后缀s表示是个数组 /// </summary> [SerializeField] Vector3[] mCachedPositions; /// <summary> /// 1.List后缀 /// 2.4->for 表示所属关系可以表示Dict /// 3.Dict后置 /// </summary> [SerializeField] List<Vector3> mCachedPosList; [SerializeField] Dictionary<string,Vector3> mPos4ChildName; [SerializeField] Dictionary<string,Vector3> mChildPosDict; #endregion } }

规范直接使用代码展示,容易看懂自然而然也就会容易遵循。

破窗效应

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygpyd.html