开发者

Standard for programming 'beautiful' code in Java? [closed]

开发者 https://www.devze.com 2023-02-15 15:42 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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.

So I'm asking you guys, what's your coding standard in Java? Like:

  1. How do you name objects, methods, classes, etc.
  2. If you have more than one object from same class, how do you name the sec开发者_开发百科ond one?
  3. 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?
  4. What is the best trade-off for performance/code beauty, a lot of small methods, or some longer methods?
  5. Feel free to say something more. =)


  1. Mostly following the Java code convention.
  2. 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.
  3. 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
  4. 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.)
  5. Robert Martin's Clean Code is highly recommended!


Just to address one specific point, because it's one I commonly see people doing horrific things with:

If you have more than one object from same class, how do you name the second one?

By their purpose, surely. If you have two different objects of the same class, you must be using them for different purposes, so name it after that purpose. I think all of these examples would be pretty self-explanatory to most readers:

public void copyAddresses(Customer source, Customer destination) {


public void sendMessage(Mailbox sender, Mailbox recipient) {


public void changeContactCompany(User contact, Company from, Company to) {


public void eatWatermelon(Bowl servingBowl, Bowl bowlForSeedSpitting) {

or whatever... you get the idea.


You should start with the official Java Code Conventions.

They will explain why code conventions are needed, different conventions and, what your question seems to be about, naming conventions. They add various examples too.


What is the best trade-off for performance/code beauty, a lot of small methods, or some longer methods?

"Premature optimization is the root of all evil" - Donald Knuth

Remember:

  1. Make it work.
  2. Make it right.
  3. Make it fast.

You should only worry about performance if it is warranted; if the current code is too slow to meet requirements.

In that case you should find the 'hot-spots' and optimize those. Check if performance is good enough. If not, repeat.


Well since most of these are easily googled I will add my own standard Java naming practices:

I usually suffix the name of classes of what they extend or implement. In other words Spring MVC controllers are suffixed with "Controller". This makes it easy in Eclipse to do a Crtl-Shift-R *Controller.

Second if I find I need to aggregate a whole bunch of static methods in a class I usually suffix that class with "Utils". I got this from Apache Commons and has just stuck.

Finally derived methods that do special expensive stuff and are transient I avoid calling them getXXX. The reason is to avoid problems with serializers.


Look here at the official guide

0

精彩评论

暂无评论...
验证码 换一张
取 消