How to write a Java program!

xiaoxiao2021-03-06  20

The minimum of keeping the class, a class with a large number of methods always has some methods that are not here, because this huge object is too much. Martin Fowler in his Refactoring book (see the link in the reference), this is called the Foreign Method code. If you have an object with 100 methods, you should think about whether this object should be dismantled into multiple objects. Categories usually have a big way in college. Java code is one of them. The minimum method of keeping the method is the same as the small class, and the reason is similar. Many experienced OO programmers have troubles to Java language, it provides a lot of OO capabilities, but they don't teach them how to do oo. In other words, it gave them a lot of ropes to bundle themselves (although at least C gave them more). One common place to see this is the main () method is far from the class, or a single-called DOIT () method. This is not meant because you can put all the code in a single method in the class. The Java language has more syntaxic sugar than many other OO languages, so a certain rumor is necessary, but don't pass the degree. Think about these long-term methods. It is very difficult to scroll through the three-screen code to understand the work. What does this method do? You need to bubble a big cafe for a few hours to study. A small, even tiny method is a code block that is easy to understand. The runtime efficiency is not the reason for a small method, readability is the real goal. This will make the code easier to maintain and make it easier to change when you need to add a function. Each method is limited to the execution. Adding a helper method to the method to add a helper method to make the code more readable: if (myadult.getwallet (). ISempty ()) {do something} The iSempty () method on the ISempty () method on the ARRAYLIS is helpful, but our IF Boolean conditions in the statement should be based on the following to the next method called HasMoney (): public boolean hasmoney () {return! Getwallet (). ISempty ();} Then our IF statement is more like English: IF (myadult.hasmoney ()) {Do Something} The number of keeps the number of the minimum of XP About the simple design of the guidelines is to complete a goal with as few classes as possible. If you need another class, although it is added. Add this class if another class will make your code simple or simplify your intent. But there is no reason just to have a class. Of course, usually have fewer items in the early stage of the project, but generally constitute more class more easier. If you have a class with a lot of methods, then analyze it, see if there is another object to fall into it, waiting for it. If you have, create a new object. On almost all Java projects I have experienced, no one is afraid to create classes, but we always try to reduce the number of classes without reducing the intent clarity. Keep the quantity of your comments, each computer scientific program, every programming book and many of the programmers I know, you have to write a comment to the code. In some cases, the annotation is helpful. In many cases, the annotation makes the code maintenance more difficult. Think about what you must do when you change the code. Is there a comment? If so, you'd better change your comment, otherwise it will expire, even over time, you will no longer describe the code at all. Effectiveness, almost double the maintenance time. My experience is: If the code is too difficult to read and understand, I need to comment, I need to make it clear enough, so I don't need to comment.

The code may be too long, or too much thing. If so, I simplify it. The code may be too concealed. If so, I add a helper method to make it clear. In fact, in the three years of Java programming with other members of the same team, the annotations I have written cannot be numbered. Keep the code clear! If you need a system or a panoramic description of a particular component, you will write a short annotation. The annotation of Luo Wei is generally more difficult to maintain, usually not a small, a good way to express the intention, and will soon expire. Don't excessively dependent on the comment at all. Using a consistent style coding style is actually an inevitable and acceptable thing in your environment. I don't even know which style can be called "typical". This is usually a personal taste problem. For example, the following is some code that makes me see that it will be uncomfortable:

Public void mymethod () {if (this.a == this.b) {statements} Why is this harassing me? Because I personally biased the coding style against the code line that I thought I thought unnecessary. The Java compiler thinks the following code is exactly the same as the code just, and I decrease a few lines: public void mymethod () {if (this.a == this.b) statements} There is no "pair" or "wrong" The division is just a shorter than the other. So what happens when I have a person who likes the first form? We have negotiated, picking out one, and then insisting on this. The only absolute style rule is consistent. If everyone on a project uses a different style, then the reading code will become difficult. Pick a style and don't change. Avoid Switch Some Java programmers have a soft spot for the Switch statement. I used to think that they are very good, but later I realized that a Switch is actually a few IFs, and usually means that the conditional logic appears in multiple places in the code. This is the code repeat, it should be taboo. why? Because there is more than many of the same code makes the code is more difficult to change. If I have the same switch in three, I want to change the processing of a case, I have to change the code three. Now, if you can reconstruct the code with a single Switch statement, what will it? well! I don't believe what is the disadvantage of using it. In some cases, it is clearer than nested IF. But if you see it in many places, there is a problem that should be solved. An easy way to prevent this problem is to avoid Switch unless it is the best tool for this work. Effective, it is very best. It is public for PUBLIC to put the most controversial suggestions in the final. Do a deep breath. I believe that you will object to let all methods are public. Example variables should be protected. Of course, many professional programmers will be afraid of this idea, because if anything is public, anyone can change it, and may change in unauthorized ways. In anything, you have to rely on programmers discipline to ensure that people do not access things that should not be accessed when they should not access. But in the programming life, few things are more frustrated than those who want to access an invisible variable or method. If you want to restrict access to things that others should not access, you are in an ignorant. This is a dangerous assumption most of the time. This frustrated feeling often occurs when using other people's code. You will see a method just doing the work you want to do, but it is not public accessible. Sometimes this has a good reason and makes the restricted accessibility meaningful. However, sometimes the only reason that does not public, is that people who write code don't want to visit this code, or they want to visit this code, because ... "there is no good reason. Many times, people use private because it is available. Do not do this. Making the method is public, the variable is protected until you have a good reason to restrict access. Follow Fowler Now you know how to create excellent Java code and how to keep it excellent. About this theme, the best books in the industry are the Refactoring, which is Martin Fowler (see the link in the reference). It is even very interesting. Refactoring means changing the design of existing code without changing the code results.

转载请注明原文地址:https://www.9cbs.com/read-39635.html

New Post(0)