School brother, ah, you can not write the code so bad? ! - end of the text to send books

Original link: https://item.jd.com/12696168.html

Click on the top [ full-stack developer community ]upper right corner [...][set to star ⭐ ]

640?

Author: Desert Eagle

www.cnblogs.com/buptzym/archive/2013/05/25/3099389.html

Today's cover art: spaghetti (spaghetti code, we all know)


I was a graduate student ordinary Cock wire Kenji upcoming internships and graduate, do data mining project together with a number of research students. In order to prevent lead teacher will not let me lean graduated from then onwards they entered the lab, it "began training design patterns and code-style" at the time of graduation.


Specific approach is to throw them "lying Design Mode" and Jinxu Liang teacher's ".NET 4.0 object-oriented programming Talk." So then do the project, and I have not seen how busy the code they write, until yesterday idle all right, open the SVN see a variety of code they write, it is then collapse ... are listed below:


1. previously named variables a1, a2, a3, much better now, but can not use pinyin?Clustering function even if not written ClusterMethod, but do not call JuleiMethod ah! With inheritance, the new class how they have reacted to improve the characteristics of it, at least can not be called XXXFinal, XXXTest or XXX2, right?


640?wx_fmt=jpeg


I was thinking, what is this GuoDu? over? country? I really do not think there are data mining this stuff Pinyin field.


2. Large class!To what extent it?

Five hundred lines? No, a thousand lines? No, five thousand lines! A MainWindow class which, eloquent five thousand lines of code, 100 variables, more than 50 functions, function'd realized, but the scroll bar codes are fine sight, dare to separate logic ah? "Single responsibility" is really to the fullest: a class to get a teacher's job!


3. You are lazy or diligent?

Several classes of functions similar, differ only in some details, even if not inherit it, do not take the same function copies of four or five times ah. Below is a screenshot of two different classes:


640?wx_fmt=png


4. screenshots above. An object may be inherited tree leaf node, when programming the base class has no attributes you want, but do not need every time as it? If it is a single variable, as the code before the judge and non-empty is not good enough?


If a collection, the collection is not enough to extract the corresponding type via LINQ statement at the start of the code? See so much as, you do not feel bored it?


5. a class there are many parameters, but it can not fail to take all the parameter settings are written to the constructor function ah , you have 10 parameters, it is necessary to write ten parameters of the constructor? The default value can not be made? Other property accessor made is not good enough?


640?wx_fmt=png


6. can not be so fond of singleton and static variables and methods ah? I know you use singleton and static cool, calling everywhere, however, memory recall it? Code extension of it?


640?wx_fmt=png


Brother, how to say this dictionary can also account for a 200M memory, in order to facilitate a singleton you get, you let it be GC recycling Shashi Hou ah?


7. 我要你把一个代码从JAVA移植到C#上,还真是够辛苦,JAVA没有属性访问器,但是C#里面有啊,干嘛还在C#上把所有的属性都加上个 getXXX() 和setXXX()方法。你真勤快!而且还加上了javadoc风格的注释,真是辛苦你了


640?wx_fmt=jpeg


8. 代码注释是用来说明代码的,稍微有些个注释掉的东西还是能理解的,可是,你一个长长的代码文件里,注释比源代码多好几倍,都是以前随便写的测试代码,废弃的代码,舍不得删,可是,后来你再看过吗?你都没看过,这些大段的注释除了碍眼有什么用处?


640?wx_fmt=png


9. 蛋疼的在集合中删除元素!

你学会了foreach, 可爱的迭代器模式,于是和我一样,不喜欢写for循环了。哪天你问我,foreach中remove会报错!我说,当然会报错,于是你在外面new一个集合,把要删的全拷贝进去,然后再来个for循环拷出来,然后。还是不对,最后只能再用for循环了。看得我都要心碎了,咱先不说性能,用LINQ写一个扩展方法RemoveElement不就好了?


///

       /// 对集合实现删除操作

   ///

       ///元素类型

       ///要删除的元素列表

       ///过滤器

       ///删除时执行的委托

       public static void RemoveElements(

           this IList source, Func filter, Action method)

       {

           var indexs = (from d in source where filter(d) select source.IndexOf(d)).ToList();

           indexs.Sort();

           for (var i = indexs.Count - 1; i >= 0; i--)

           {

               if (method != null)

               {

                   method(source[indexs[i]]);

               }

               source.RemoveAt(indexs[i]);

           }

       }


然后,你在实现IList接口上的集合都能删除元素了,并且还能通过一个委托做一些操作,多舒服啊…


10.  你会在一个类的成员里很高兴的定义一个变量,比如下面:

640?wx_fmt=png


一个随机数产生器ran, 然后私有成员还很勤快的写了get,set访问器,下面诡异的arrh,这都不说啥了,关键是下面:


640?wx_fmt=png


这个变量被你在函数中轻易的覆盖了,覆盖了也不管,反正好像程序结果也是正确的,可是以后没这么走运怎么办?


11.  你不懂什么叫写“库”,只知道写代码,代码重用就是代码搬运工,从这边拷贝到那边。真正的库是什么?稳定,高效,好用,扩展性强。 到现在都没写过一个库,老师布置什么任务,新开一个工程,名字命名成XXXTest, 然后一顿狂敲,毕业时候,工作就是那么20多个不同的文件夹,所有代码都是给自己写给自己看,到最后连自己都看不懂了… 那些能够重用的功能,为什么不好好重构一下,以后肯定还能用得着啊!


12. Console.WriteLine(“xxxxxxxx”)

Console.WriteLine(“xxxxxxxx”)

Console.WriteLine(“xxxxxxxx”)


你给写的类库,里面全是这些控制台输出,做Console程序还好,一个大黑框哗哗哗的输出出来,可是,做WPF界面程序和WCF呢?你总不至于输出到VS里那个小小的调试窗口吧!我哪天想让你输出到txt文件,你是不是就傻眼了?  敢不敢用用专门的日志组件,比如log4net!


13.  C风格的C#, 里面全是数组,数组,数组。。。。。。太怨念了。.NET疯了会搞那么多集合和接口进去?全用数组不就得了?


两个集合的对应元素需要做加法,你是这么做的:

640?wx_fmt=png


可是,如果这个代码被执行1000遍,你就要new一千次数组,且不管GC效率如何,这种需求你完全可以返回一个IEnumerable过去,通过类似LINQ的延迟计算进行,也不用生成这么多数组啊啊啊。


13. for循环啊!

640?wx_fmt=png


能不能写个for循环?敢不敢?不仅如此,你都愿意为一块仅仅有细微差别的代码复制十遍,却不敢写一个for循环?


14. 做界面时候的事件引用器…

在界面上,你会在每个按钮,每个菜单双击一下,然后在C#代码中,敲事件处理程序,无非就是一堆变量赋值,一堆初始化等等,若你是做Winform,那我也就勉强不说什么了,可是你做的是WPF,这么紧密的耦合,让以后我改界面或者改逻辑的时候怎么办?敢不敢去学一下绑定和MVVM?


15. There are numerous slots point, for example, you can output hundreds of megabytes in the program you're debugging convenient location txt file, then you have to be uploaded to the unscrupulous to go above SVN, SVN also never used properly, no matter conflict does not conflict directly point to determine, so I can not stand the most is:


// seniors is really a pig  

640?wx_fmt=png

Recommended :" Artificial Intelligence and Health "

640?wx_fmt=png

 
  

Editor's Choice:

"Artificial Intelligence and Health" AI Artificial Intelligence is a medical readers and combat manuals. The book based on cutting-edge, comprehensive review of the application history, applications and prospects of artificial intelligence technology in the healthcare industry; detailed discussion brings AI Artificial Intelligence Medical Medical Industrial Revolution: Precision Medical, intelligent medical, mobile medical, telemedicine, low cost medical, health care will be low risk because the artificial intelligence technology into reality;

Copy Amoy password "€ 6KceYLOraKb €" or read the original to learn more. . .


I think this paper help you? Please share with more people

Watch " full-stack developer community ," starred, full-stack upgrade skills


This number will be public to everyone from time to time send welfare, including sending books, learning resources, please look forward to it!

If you feel the push content is good, it may be a lower right corner looking forward circle of friends or favorites, thanks for the support.


Good article, I look at ❤️

Guess you like

Origin blog.csdn.net/weixin_35681869/article/details/102481073