• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper Code Organization  RSS feed

 
Martin Briones
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know if there's a "best" approach or standard in code organization? I do know that there's Sun's Coding Conventions, but I'm noticing that others are deviating from it. If that's the case, is it still a good practice or would it depend on the purpose of the program?

I'll include a random code, and hopefully will get a feedback on whichever comes first, what's lacking, etc.


Thanks in advance!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different companies and teams have different standards. There isn't "one true way". The important thing is to agree as a team.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Briones wrote:
I would like to know if there's a "best" approach or standard in code organization? I do know that there's Sun's Coding Conventions, but I'm noticing that others are deviating from it. If that's the case, is it still a good practice or would it depend on the purpose of the program?

I'll include a random code, and hopefully will get a feedback on whichever comes first, what's lacking, etc.

Including random code as the basis for getting comments on how to best arrange your code seems like asking people how to best shuffle a deck of cards. I don't know what kind of reasonable feedback you're expecting. Are you asking about arrangement of the members of a class? I don't think many people view that as a very important consideration when it comes to code organization. Obviously, it's good to have some semblance of "order", like you wouldn't put constant fields all over. People usually prefer similar kinds of things grouped together, like static finals all up at the top of a class, followed by class variables, then instance variables, etc.

When I think of organization, I usually think at a higher level, like packages and modules. I think it's more important to pay more attention to organizing code at that level of granularity since most modern IDEs already have enough "intelligence" built in to them to allow you to sort class members with a few keystrokes or clicks of the mouse.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I think about, I actually never really pay much attention to the exact ordering of the members in the classes I write. Since most of my code is written through Test-Driven Development, I'm more focused on functionality than form. When I write some test code that doesn't compile because some production code does not exist, I just use the keyboard shortcut to do a quick fix (CTRL+1 or CMD+1 in Eclipse) and usually accept wherever the IDE decides to put the new field or method in the class. Very seldom do I move methods around after that, especially after the initial checkin. Who has the time to worry too much about the exact ordering of class members anyway? Wouldn't you rather focus your energy on more important things like adding more features, writing more unit tests, or refactoring your code to make it clearer, simpler, and easier to maintain?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to consider: when you're working on a large project with many .java files and more than one developer, enforcing a "standard" in ordering of class members can quickly become an exercise in futility. I can't imagine a whole team of programmers who are so anally retentive that they would chase each other down about a few class members being "out of order". One programmer with the best of intentions in mind going through code and reordering things just because it has a few things out of place can really screw up the rest of the team's ability to track changes in a version control system. Think of the diff command and what kind of havoc reordering of code can wreak on you and your team's ability to use it.

One rule we have on our teams: if you're going to reorder code, have a good reason to do so, reorder only a few things in one commit, always do reordering as a separate commit, and NEVER reorder and make other kinds of changes to the code in the same commit.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed that this was posted to a certification forum. In the context of the certification exam, I doubt that the ordering of class members is something that will come out. If anything, I'd think it's more likely to get a question about standard naming conventions before you'd get a question about class member ordering. I could be wrong but if questions like that actually are included in the exam, I'd be really surprised and frankly, disappointed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!