Java中编程“美丽”代码的标准? [英] Standard for programming 'beautiful' code in Java?
本文介绍了Java中编程“美丽”代码的标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在阅读一些关于Java编码标准的书。
我总是喜欢美丽和干净的代码。
I'm reading some books about coding standard in Java. I always loved beautiful and clean code.
但有一些事情困扰我。例如,方法名称应以小写字母开头,如果有第二个单词,则应以大写字符开头。但变量的标准是一样的。我认为这有点混乱。
But there are some things that bother me. For example, a method name should start with a lowercase word, and if it has a second word, it should be start with a uppercase character. But the standard for variables is the same thing. I think this is a little confusing.
所以我问你们,你的编码标准在Java?
Like:
So I'm asking you guys, what's your coding standard in Java? Like:
- 如何命名对象,方法,类,等 >
- 如果你有同一个类的多个对象,你如何命名第二个?
- 如果你在一个方法的参数中有一个对象,你在这个方法中有同一个类的另一个对象,你怎么命名它们两个?
- 性能/代码美的最好的权衡,很多小方法,或一些较长的方法?
- 随便说些什么。 =)
- How do you name objects, methods, classes, etc.
- If you have more than one object from same class, how do you name the second one?
- If you have one object in the argument of a method and you have another object from the same class inside this method, how you do name both of them?
- What is the best trade-off for performance/code beauty, a lot of small methods, or some longer methods?
- Feel free to say something more. =)
推荐答案
- http://www.oracle.com/technetwork/java/codeconv-138413.html\"> Java代码约定。
- 我尽量不要考虑什么样的类是一个对象。如果我有五个不同的字符串,每个变量的名称应该描述变量代表什么信息/内容,而不是一个字符串。
- 我发现提出变量的变化只是因为它既存在一个方法参数和一个类变量。我大多使用与此语法相同的名称
this.theVariable = theVariable
- 方法应尽可能短:和尽可能少的嵌套级别(例如,最多一个if语句,而不是ifs里面的ifs等)。
- Robert Martin的清洁代码。
- Mostly following the Java code convention.
- I try to not make it matter what kind of class an object is. If I for instance have five different strings, the name of each variable should describe what information/content the variable represents, and not that is is a string.
- I find it often silly to try coming up with variations of a variable just because it exists both as a method argument and a class variable. I mostly use the same name with this syntax
this.theVariable = theVariable
- A method should be as short as possible: as few lines as possible, and as few nested levels as possible (i.e. max one if-statement, and not ifs inside ifs etc.)
- Robert Martin's Clean Code is highly recommended!
这篇关于Java中编程“美丽”代码的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文