• Post Reply Bookmark Topic Watch Topic
  • New Topic

a coding convention question  RSS feed

 
mark I thomas
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java class. it has many getter/stter methods and some other business logics methods. Shall I put those getter/setter methods in front of business methods or shall I put business methods first ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't really matter. I think that most Java programmers put member variables at the top of the class, then constructors, then getters and setters, and then business methods.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your company has a policy, follow that. if you are doing this for yourself, then you can decide what you want - but stick with it in all your code. Oracle's web site has the "java" standard, but you are not forced to go by their recommendations.

Other programmers might mock you if you don't follow their guidance without a reason, though, so you have been warned.
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I myself always use the official Java Code Conventions. Even here at work, I asked people to follow it and we are doing so. There's also a guide for JavaDoc comments, which is the How to Write Doc Comments for the Javadoc Tool guide. This one is worth reading too.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the Sun/Oracle code conventions don't really address the specific question asked in this thread. The closest it comes is here:
These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

It's open to interpretation whether you this means the getters and setters should be put together or not, and it gives no guidance at all whether they would be before or after other methods. It's up to each individual or group to decide that.
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:It's open to interpretation whether you this means the getters and setters should be put together or not, and it gives no guidance at all whether they would be before or after other methods. It's up to each individual or group to decide that.


What I tried to say is that, as Fred said, if the company has a policy, follow it. These conventions can be used. This way, as Jesper said, it doesn't really matter if a business method comes before or after a getter/setter method.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:The goal is to make reading and understanding the code easier.

Agreed. I usually write getter setter in the end and put business methods right after the definitions of the variables. The getter setter is just become a formality sometime.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wasn't me who said that - it's from the Sun/Oracle code conventions. But I agree with it, as well as with Robert's last post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!