Some suggestions for beginners: before learning Design

Source: Internet
Author: User

This is intended for beginners. Although it is a bit of a boast, I am not afraid to make an axe in front of the class. I am writing about it all from my personal experience .)

Before beginners learn design patterns and other things, if they can lay a good foundation, they will be able to get twice the result with half the effort in the future.

It is not very special for beginners to consider future changes.Code. When a calculator is written, it feels "Good, I will" and immediately throw it to one side.

High LevelProgramThe written code is easy to modify. But it is difficult for beginners to have this opportunity to modify their own programs. What should they do?

In my personal experience, I think a good way to exercise this kind of thinking is to write programs with the same function repeatedly. After the first version is written, summarize it a little and then write it again and add some features.

After several times, you will see that the last write is much more pleasing to the eye than the first write. When you know your code well, no matter what features you want to add, you can immediately think of where to add classes.

If you can write code that is easy to modify, it will be easier to work in the future. With the same requirement, you have to turn around and change things. It's no exaggeration!

HoweverBeginnersThere are few Java classes, let alone the design. Can such a person write such high-level code?

Yes! I will teach you a simple principle:No matter what method, no more than 5 lines. I believe that many people did not see this sentence for the first time, but there are only a few people who really care about it.

What I want to say is that if a beginner can recognize this sentence, it is equivalent to practicing jiuyang's power. The written Code ensures smooth and inactive processes.

You can't do it. The program written by beginners does not have many functions. It is no problem to use a method of no more than five lines.

How to write it? For example, how do I write the main function as a calculator?

   Public Static VoidMain (string [] ARGs ){
Setupuimanager ();//Initialize the swing Style
NewCalculator (). setvisible (True);//Open Form
}

How about two rows. Calculator is the form class of the calculator. This form has many controls, so there must be a lot of code for initialization? No, no:

    Public     Class  Calculator  Extends  Jframe {

Public Calculator (){
Setupframe (); // Initialize the form size and title
Createlayout (); // Initialize Layout
Createmenu (); // Add menu bar
Createtextfield (); // Add text box
Createbuttons (); // Add button
}
}


In this way, by delegating code to other methods, the method can be reduced to five lines.

You may not be able to laugh at this: Is it too mentally retarded? What are the benefits? -- Tell you that the benefits of doing so are:Global and quick positioning.

Take the above Code as an example. If you want to add a menu item in the future, you can see at a glance that only createmenu () is to be modified ()
Method. If a button is missing during running, you can see at a glance that the problem lies in createbuttons ()
Method. On the contrary, if these five methods are mixed together, I promise that you will not want to change the code after reading it. If a problem occurs, it will be a headache...

More importantly,This pre-delegated encoding method also improves your thinking..

For example, what I want to do is divided into several steps. Before writing the code for step a, assign a to an empty method (). As for the content of a (), I will not consider it now. What should I consider now? Now I want to consider what a will do after it completes. This is 30 thousand feet of height.

If a finishes B, then I delegate it to B (). And so on, after all the tasks are assigned, I will consider a (), B ()
What to write in it. At this time, a () has defined step a, set the parameters for what to input and what to return. Write ()
This method does not need to be considered. If ()
It is very complicated. I can delegate several methods to implement it. Every method is the same, until a step is very simple, I can write the final code.

Although the entire logic is complicatedThrough Delegation, I only need to consider a small part of it each time.So it is easy to write. The code written in this way is clear and easy to understand.

This is the so-called "divide and conquer. Therefore, a method does not have more than five rows. The principle is as follows:The content of a method can be understood at a glance. There is a saying in this line: the code is for people, not for machines. It's not nice to say, it's not what people look at, it's definitely not what people write!

You may say, "It's not that serious. You can use the program ". Think about it. If I want to, all the code is written in main (), and the program cannot run as well? Since it can be used, why should I look at the design pattern? -- In the final analysis,The so-called design means to organize the code more easily.Easy-to-understand things are easy to reuse and easy to reuse. Remember this when learning design.

At the beginning, this is my personal experience. If I didn't see more than five lines of advice in my book when I was learning programming, I wouldn't have imagined what I would look like now. So I especially worship this sentence, although I don't know who said it ......



PS1: complex methods can be simplified. If a method "defines variables by light with more than five rows", it can be said that most of these variables actually work locally, and their scope of action is only part of the entire method, this part can be extracted.

PS2: how to simplify the long method. A master wrote a book dedicated to refactoring. The topic of this article is not here,Do not write long methods at the beginning.
Some Java
The entry book does not pay attention to this point, so many people do not believe it, just like in the reply in this article. To sum up my personal experience, it takes much more time to change the code than to write the code. I have changed a lot of codes, including various flavors and packages.
Contains more than 6000 rows of classes and more than 400 rows of methods. So I think this kind of bad habit should be corrected from the very beginning, and it must pass the "no more than five rows"SeeminglyAchieve extreme goals. If you already have a lot of programming experience or are already working, then how do you do it before? Continue and there is no need to listen to me. If you think that I am not a good person, this article appears on the csdn homepage is really a curse, please write an articleArticleYes.

PS3: if you are a beginner and you don't believe it, it doesn't matter. This is the case with experience. If you read a few articles and a few books, you can become familiar with them. We should not mix them.

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.