10-year-old programmer to 10:03 in the developer's suggestion

There are many Java programmers code of conduct or best practices to be followed. This article outlines 10 of the Code or the precepts of most every developer should follow, if they are not followed, will lead to disastrous consequences.10-year-old programmer to 10:03 in the developer's suggestion

1. add comments (Add comments to your code) is the code - everyone knows this, but not everyone will do so. How many times have you "forget" add a comment? Indeed, the comment does not add any functionality to your application function. But how many times seen two weeks ago to write the code, you can not remember what it is? You are very fortunate that the code does not write your own comments, and your mind will have residual impression. Unfortunately, most of the time, the code is written by someone else, and that person is likely to have left the company. There is a saying that good: "come and go, and mutual benefit", so programmers should understand each other (and your own), add comments to your code.
2. Do the simple things complicated (Do not complicate things) -. I've done it before, I believe you, too. Developers tend to use complex solution to a simple problem. We are one of only five user system introduced in EJB, implement a framework for the application does not require a frame, using the properties file, object-oriented solutions, the use of threads, and these do not need. Why do that? Some people may not know there is a better solution, but others may do it on purpose to learn new knowledge, or just for fun. For those who do not know a better solution, to listen to experienced programmers recommendations. For those who for purely personal purposes will complicate the design, I suggest you a little more professional.
3. Remember - "the better" is not always the case (Keep in Mind - "Less is more" is not always better) -. Efficient code is a good thing, but in many cases, not the number of lines of code less the higher the efficiency. See the following "simple" example:
if(newStatusCode.equals("SD") && (sellOffDate == null || todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null && todayDate.compareTo(lastUsedDate)>0)) ||(newStatusCode.equals("OBS") && (OBSDate == null || todayDate.compareTo(OBSDate)<0))){ newStatusCode = "NYP"; }

What if this condition is noted how difficult? And imagine, people who did not write the code to follow Article 1 - add comments to the code.
If the conditions are broken down into two if statements are not easier to understand it? Now let's look at the modified code:
! IF (newStatusCode.equals ( "SD") && (sellOffDate == null || todayDate.compareTo (sellOffDate) <0 || (LastUsedDate = null && todayDate.compareTo (LastUsedDate) > 0))) {newStatusCode = "NYP";} else if (newStatusCode.equals ( "OBS") && (OBSDate == null || todayDate.compareTo (OBSDate) <0)) {newStatusCode = "NYP";}

So readability is not better? Indeed, we wrote repeat statements; indeed, if we write one and two braces; but the code does more readable and easier to understand!
10-year-old programmer to 10:03 in the developer's suggestion

4. Do not "hard-coded" (No hard coding please) - . Due to time constraints, developers always forgotten or deliberately ignored this one. Yet another possibility is to follow this commandment, we would not be trapped in a "time-critical" dilemma. Define a static final variable, add a line of code, but how long will it take? For example:
public class A {public static String S_CONSTANT_ABC = Final "the ABC"; public Boolean methodA (String sParam1) {IF (A.S_CONSTANT_ABC.equalsIgnoreCase (sParam1)) {return to true;} return to false;}}

现在,每次需要比较字符串“ABC”与某个变量的时候,我们只要引用 A.S_CONSTANT_ABC 即可,而不必记住它本身是什么。对这个常量的修改也非常方便,改一个地方即可,而不必在全部代码中查找。
5.不要发明你自己的框架(Do not invent your own frameworks). – 不夸张地讲,已经有几千个框架存在了,大多数还是开源的。很多框架都是极完美的解决方案,并已被用到成千的系统中。我们只要关注最新的流行的框架,至少表面上要熟悉一下。一个最成功的、也是被广泛使用的例子是Struts框架,这个开源的web框架是建立web系统的极佳选择,不要试图构造你自己的Struts版本,会累死的。但你必须记住第2条(译注:原文是“第3条”,显然不对)戒律 —— 不要把简单事情复杂化。如果你要开发的系统只有3个界面,就不要用Struts. 对于这样一个系统,没有足够的需要被“控制”的东西(译注:Struts将界面做MVC划分,C即controller,所以作者说there isn’t much “controlling” required)。
6.对Print行或字符串说不(Say no to Print lines and String Concatenations). – 我知道为了调试方便,程序员喜欢到处用System.out.println ,然后对自己说过一会就删掉。但我们常常忘记删掉这些行或不愿删掉,我们用System.out.println 做测试,为什么测完后还要去改代码?这很可能导致误删一行我们需要的代码。不要低估System.out.println 的危害,看下面代码:
public class BadCode { public static void calculationWithPrint(){ double someValue = 0D; for (int i = 0; i < 10000; i++) { System.out.println(someValue = someValue + i); } } public static void calculationWithOutPrint(){ double someValue = 0D; for (int i = 0; i < 10000; i++) { someValue = someValue + i; } } public static void main(String [] n) { BadCode.calculationWithPrint(); BadCode.calculationWithOutPrint(); } }

下面表格可以看出,calculationWithOutPrint() 方法执行时间是0.001204 s. 作为对比,calculationWithPrint() 方法居然需要令人难以置信的10.52 s来执行!10-year-old programmer to 10:03 in the developer's suggestion

(若你想知道怎么做一个这样的表,请阅读另一篇文章”Java Profiling with WSAD” Java Profiling with WSAD )
为了避免CPU浪费,最好的办法是引入一个包装的方法,如下:
public class BadCode { public static final int DEBUG_MODE = 1; public static final int PRODUCTION_MODE = 2; public static void calculationWithPrint(int logMode){ double someValue = 0D; for (int i = 0; i < 10000; i++) { someValue = someValue + i; myPrintMethod(logMode, someValue); } } public static void myPrintMethod(int logMode, double value) { if (logMode > BadCode.DEBUG_MODE) { return; } System.out.println(value); } public static void main(String [] n) { BadCode.calculationWithPrint(BadCode.PRODUCTION_MODE); } }

字符串(String)连接是另一种CPU浪费方式,看下面的例子:
public static void concatenateStrings(String startingString) { for (int i = 0; i < 20; i++) { startingString = startingString + startingString; } } public static void concatenateStringsUsingStringBuffer( String startingString) { StringBuffer sb = new StringBuffer(); sb.append(startingString); for (int i = 0; i < 20; i++) { sb.append(sb.toString()); } }

从下面表格可以看出使用 StringBuffer只要花 0.01 s 而使用String 连接需要0.08 s,选择哪种应该很明显了。10-year-old programmer to 10:03 in the developer's suggestion

7.注意图形用户界面(Pay attention to the GUI). – 无论听上去多荒谬,但有一点我注意过多次了:图形用户界面(GUI)对于商业用户而言与程序功能及执行效率一样重要。GUI对于应用程序的成功至关重要。 IT管理者(译注:这里应该是指程序开发方的IT management)常常忽略GUI的重要性,很多公司为了省钱而不雇佣Web设计人员,而这些设计人员有足够的经验来设计“用户友好”的应用软件。 Java程序员不得不依赖他们有限的HMTL知识。我见过非常多对“计算机友好”而非对“用户友好”的应用程序,同时精通软件开发和用户界面开发的开发者非常少见。 如果你是一位不幸被指派做界面开发的Java程序员,你要遵循下面3条规则:
不要重新发明轮子。去看那些类似应用系统的界面。
首先建立一个原型。这一步非常关键。客户喜欢提前看到他们要用的东西。同样你可以得到他们的反馈,而不是你辛辛苦苦做出来一个客户不喜欢的东西。
试戴用户的帽子。换句话说,站在用户的角度查看需求。譬如,一个统计的界面可以分页,也可以不分页。作为开发者,很可能会忽略分页,因为这会减少很多麻烦;而站在客户角度,这就不是一个好的方案,因为数据可能多达几百行。

10-year-old programmer to 10:03 in the developer's suggestion

8. requirements document prepared in advance (Always Prepare Document Requirements) -. Every business needs are recorded in the document. This may be achieved in a fairy tale, but in reality difficult to achieve. No matter how urgent the time, no matter how looming deadline, you must ensure that the business needs to be recorded. (Annotation: This obviously contrary to the concept of agile development, we have to think independently, non-discrimination)
9. unit testing, unit testing, unit testing (Unit-test Unit-test Unit -test..) - I am not prepared to discuss. how unit testing details, I just want to say that this must be done. This is the most basic rules of the program, in particular can not be ignored. If you co-workers can create your code a test plan, that's fine; if not, it would have to do it yourself. When doing unit testing program, follow the following principles:
to write unit tests before coding
Annotation retention unit tests
for any "interesting" public methods to do unit testing ( "interesting" refers to the addition as the most common getter / setter this getter / setter methods outside the class methods, but contains its own content)
10. Remember: quality, not quantity (Remember - quality, not quantity) - too late (unless necessary) not to be in. I know that sometimes because of product issues, deadlines or other unexpected events, can not work on time. But the manager will not be too late for you to be grateful for general questions or reward you; they will have for the quality of work and thank you. If you follow the principles listed above, you will write more robust, less bug in the program. This is what you should do.
Conclusion
This article summarizes the 10 most codes of Java programmers should pay attention to. It is not enough just to know, but also to follow them. These codes allow us to hope to do more professional programmer.
Not everyone can become a master, but do not work hard, even if there is another high talent, but also a idiot!

Welcome to share with everyone, like the point of a praise yo remember the article, thanks for the support!

Guess you like

Origin blog.51cto.com/14442094/2425787
Recommended