*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Readability of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Readability of code" Watch "Readability of code" New topic
Author

Readability of code

Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Hi All

I’ve just started on my assignment. I might obsessing over my code a little too much but I was wondering about other peoples view on this are. I’m working on my GUI at the moment and have read that it is good practice to instantiate instance variables where they are declared. In my work I find that I leave the instantiation of components to methods that build the GUI because it looks cleaner and because I find it easier to make sure I do all the set-up of the components required. I know that instantiation on declaration generally results in less NPE but does this not hinder the readability of the code?

Do the examiners take this level of detail into account?

Cheers


Chris
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
My take is that declaration, instantiation and first use should be closely
grouped for singleton attributes. But I've found, especially for GUIs, that
grouping constructs of the same type enhances readability, code quality
and coding efficiency. For example, declaring a list of JButtons, instantiating
them in a list, and so on. Being able to cross check with nearly identical lines
above and below helps spot errors, and CTL-C/CTL-V works better if you can
avoid taking long jouorneys up and down the source code. That said, I then
switch over to set JButton state, button by button. This is kind of a ramble,
but I guess for me, there are tradeoffs to consider.
... Jim ... (P.S. What is NPE?)


BEE MBA PMP SCJP-6
Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Ok, thanks for your take on it. Your view certainly makes me think a little bit more about how I structure my code (something easily over looked when you are under pressure at work). NPE is NullPointerException BTW
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Jim Hoglund wrote:(P.S. What is NPE?)
Short for NullPointerException


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Hi Chris,

I declared a lot of data members in my gui classes, but initialized them just when I needed them. And like Jim indicates you get easy copy/paste code and can spot mistakes a bit quicker. For example:

In my opinion it is more important for readibility to be consistent in the way you code, so your complete code base has the same "look and feel" and not different in each class.

Kind regards,
Roel
Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Thanks Roel, that’s how I would normally do it. It’s good to have some confirmation that this is an acceptable method of practice. Looking at the project for the in the early stages leads me to believe that I’ll be asking a lot more questions. I’ve been surprised at the vagueness of the requirements and questioning myself on every decision made (makes for an interesting project though).

Thanks again


Chris
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Chris Hatton wrote:leads me to believe that I’ll be asking a lot more questions
Not a problem, we are here to help you and try our best to answer your questions and clear your doubts

Chris Hatton wrote:questioning myself on every decision made
Keep on doing that and you'll pass with a nice score. That's what I did and I tried to keep it as simple as possible, so that was in the back of my head with each decision I took.

Kind regards,
Roel

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Readability of code
 
Similar Threads
cant get program loop to work?
how to implement MVC?
final product
java import packages question
Can i put all of my action listeners(i will have hundreds) in a seperate class file and then use it?