Can code be written so badly ?!

Source: Internet
Author: User

I am an intern and graduate student from the second year of study, and I am working on a data mining project with a few students. In order to prevent the teacher from discontinuing me from graduation, the teacher "started the design model andCodeStyle training ". The specific approach is to throw them to the "big talk Design Model" and the ". Net 4.0 object-oriented programming" of Jin xuliang ". So the project went on. I was also very busy and did not read the code they wrote until yesterday I was idle. I opened SVN and looked at the code they wrote, and then it crashed... List as follows:

1. I used to name the variables A1, A2, and A3. Now it is much better, but can I use Chinese pinyin instead? The clustering function is not called juleimethod even if it does not write clustermethod! With inheritance, how can we improve the new class? At least xxxfinal, xxxtest, or xxx2?

I was wondering, what is this guodu? Excessive? Country? I really cannot think of this pinyin stuff in the data mining field.

2. super large class! To what extent? Five hundred rows? No, the number of rows is one thousand? No, it's five thousand rows! A mainwindow class contains five thousand lines of code, 100 variables, and more than 50 functions. The function is implemented, but the code scroll bar is invisible, dare not separate the logic? The "single responsibility" is a perfect solution: One Class handles the task of a teacher!

3. Are you lazy or diligent? The functions of several classes are similar. Only some details are different. Even if you do not inherit them, do not copy the same function four or five times. The following are two different classes:

4. Same as above. An object may be a leaf node in the inheritance tree. During programming, this base class does not have the attributes you want, but does not need to be as every time? If it is a single variable, just as before the code and judge whether it is not empty?

If it is a collection, it is not good to extract the corresponding types from the collection through the LINQ statement at the beginning of the Code? Are you bored with seeing so many?

5. There are many parameters in a class, but can you not write all the parameter settings into the constructor? If you have 10 parameters, you need to write the constructor with 10 parameters? Can it be set to the default value? What about other property accessors?

6. Don't be so fond of Singleton mode and static variables and methods? I know that you use singleton and static instances, which can be called everywhere. But what about memory recycling? What about code scalability?

Eldest Brother, how can I use this dictionary to occupy MB of memory? You have done it for convenience of a single case. When will you let it be recycled by GC?

7. I want you to port a code from Java to C #. It's really hard enough. Java has no attribute accessors, but C # does, why are all attributes added with the getxxx () and setxxx () Methods on C. You are so diligent! It's really hard for you to add a javadoc-style comment.

 

8. Code comments are used to describe the code. Some comments are understandable. However, in a long code fileSource codeSeveral times more, they are all test code that was previously written casually. Discarded Code cannot be deleted. But have you read it again? You have never seen it. What are the meanings of the comments in these sections except the eye-catching ones?

9. delete an element in the collection!

You learned foreach, the cute iterator mode, so like me, you don't like to write a for loop. If you ask me one day, an error will be reported when removing in foreach! I said, of course, an error will be reported, so you should copy all the items to be deleted in the new collection, and then copy them out in a for loop, and then... Or not. In the end, we can only use the for loop. It seems that I am heartbroken. Let's not talk about performance first. Isn't it good to write an extension method removeelement with LINQ?

   ///          ///  Delete A set  ///          ///  Element type  ///  List of elements to be deleted  ///  Filter  ///  Delegate executed during Deletion          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]);} 

Then, you can delete all the elements in the set that implements the ilist interface, and perform some operations through a delegate. How comfortable is it...

10. You will happily define a variable in a class member, such as the following:

 

A random number generator ran, and then the private members wrote the get, set accessors, and the following weird arrh. The key is as follows:

This variable is easily overwritten by the function.ProgramThe results are correct, but what should I do if I am not so lucky in the future?

11. You don't know what it means to write a "Database". You only know how to write code. code reuse is the code Porter. Copy the code from here to there. What is the real library? Stable, efficient, easy to use, and highly scalable. I haven't written a library yet. The teacher assigned any tasks, started a new project, named it xxxtest, and then gave me a crazy knock. When I graduated, work is more than 20 different folders, all the code is for yourself to read, and finally can not even understand themselves... Why are there some reusable functions that can be used in the future!

12. Console. writeline ("XXXXXXXX ")

Console. writeline ("XXXXXXXX ")

Console. writeline ("XXXXXXXX ")

The class library you wrote contains all these console outputs. It is okay to make a console program, and a large black box is shown. But what about the WPF interface program and WCF? You will not always output the small debugging window in! On which day do you Want to output TXT files? Dare not use special log components, such as log4net!

13. C-style C #, which contains arrays, arrays, arrays ...... Too much resentment .. Will net be involved in so many integration and interfaces? Why not use all arrays?

The corresponding elements of the two sets need to be added. You do this:

However, if the code is executed 1000 times, you need to create a new one thousand times group, regardless of the GC efficiency, you can completely return an ienumerable, through latency computing like LINQ, you don't need to generate so many arrays ....

13. For loop!

Can I write a for loop? Dare not? What's more, are you willing to copy a for-loop for a code with only a few nuances for ten times?

14. Event reference during interface creation...

On the interface, you double-click each button and menu, and then in the C # code, hitting the event handler is nothing more than a bunch of variable assignment and initialization, if you are using winform, I will barely say nothing, but you are using WPF. This tight coupling allows me to change the interface or logic in the future. What should I do? Dare to learn about binding and mvvm?

15. there are still countless slots. For example, you will output a few hundred megabytes of TXT files in the program location for your convenience for debugging, and then all of them will be uploaded to SVN improperly, SVN has never been used correctly, and no matter whether there is a conflict or not, it can be determined directly. What makes me most intolerable is:

Hum! Tomorrow's meeting will clean you up!

 

If students see thisArticleDon't hate me, or report to my mentor. I still want to graduate normally...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.