aspose file tools*
The moose likes Beginning Java and the fly likes Is my class too big? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is my class too big? " Watch "Is my class too big? " New topic
Author

Is my class too big?

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Hi All,

New here, I think I the Head First book mentioned something about this forum and I thought I'd drop by.

I'm working on a personal project and I have some doubts about the way my biggest class looks like. Since the project will make extensive use of XML, I have XML reader and writer methods inside the class and I'm wondering if that's the way to go for OO design or am I paranoid? I just keep wondering whether I should have a separate general purpose XML reader and writer classes, which I could probably reuse in the future. The class has ~450 lines of code at the moment, without the getter and setter methods.

I'd like my code to be really clean but without getting into analysis by paralysis mode.

I'm still learning, and while reading I jot down some questions about things I don't fully understand. Perhaps some of you could clear them up for me:

1. I've seen some code with full imports, like this

instead of


I'm assuming this is performance-related, but being explicit on the imports makes the import section quite long. Does it make sense? How much of a performance hit are we talking about? What's good practice?

2. Are there any resources you've personally used and would recommend for good Java coding practices? I've read that 80% of coding is maintenance, and I'd like to make sure that my code really is easy to maintain. Any suggestions here would be greatly appreciated.

There are a lot of other questions I have, but I'll try to figure most of them out on my own as it's probably the best way to learn.


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It is hard to judge if a class is too long based just on line count. what you need to be sure of is that it does just one thing. From the Sound of it that is not the case with your class.

When using XML it is not unusual to make a generic XML parser, a data holder which just holds values, and a class which translates from what the parser gives you into the data holder.

Since your class sounds like a data holder (it has getter and setter methods) you should consider making one Or more XML helper classes for everything that isn't data holding.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Also, about the imports: it really isn't about efficiency. The explicit imports are used because they are explicit.

There are two cases where it becomes important.

1) You Or Someone else tries to read the code later d sees a class and wonders where it came From. with the explicit imports it is clear, with the wildcard it is not.

2) You try to use a class name that is present in 2 or more imported packages. A classic example is List, which can be found in both java.awt and java.util. If you imported java.awt.* and java.util.* you would end up with a Collision. Or if you imported java.awt.* and not java.util.* you could get Compiler errors you might not understand.

Neither of those two issues is very Compelling, so which you choose usually ends up coming from personal preference or your projects/company's Coding conventions.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Steve Luke wrote:Since your class sounds like a data holder (it has getter and setter methods) you should consider making one Or more XML helper classes for everything that isn't data holding.


That's exactly what I wanted to know, thank you.

Steve Luke wrote:Neither of those two issues is very Compelling, so which you choose usually ends up coming from personal preference or your projects/company's Coding conventions.


Thank you Steve. I saw a bunch of explicit imports when looking at some of IBM's DeveloperWorks tutorials. So how do you do it? ;-)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I do explicit imports: mainly because that is the company convention and my IDE is setup that way. The IDE lets me collapse the imports so they don't get in my way.

Before my current job my preference was to use explicit imports for the first 3 Classes from a package. IF I used more than 3 Classes from one package I would switch to the wildcard.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

So I suppose it's just a matter of personal preference or company policy. I thought it was performance-related. Which IDE do you use if you don't mind my asking? I'll probably start using an IDE soon, but I'm holding off for a while until I get really comfortable with Java.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Paul Mrozik wrote:So I suppose it's just a matter of personal preference or company policy. I thought it was performance-related. Which IDE do you use if you don't mind my asking? I'll probably start using an IDE soon, but I'm holding off for a while until I get really comfortable with Java.

That's definitely a good idea. Personally, I use Eclipse; but most of the major ones offer similar tools.

As to your original question: one thing I would say is that there are gazillions of 3rd party classes and packages around for XML processing. If I were you, I'd make sure that you're not writing code that is re-inventing the wheel.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Paul Mrozik wrote:
I'd like my code to be really clean but without getting into analysis by paralysis mode.


If you want to learn how to write clean code, then get the book "Clean Code" by Robert Martin. Good companion books for this are "Refactoring" by Martin Fowler and "Growing Object-Oriented Software, Guided by Tests" by Freeman and Pryce and books on automated unit testing.


Junilu - [How to Ask Questions] [How to Answer Questions]
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Winston Gutkowski wrote:
That's definitely a good idea. Personally, I use Eclipse; but most of the major ones offer similar tools.

As to your original question: one thing I would say is that there are gazillions of 3rd party classes and packages around for XML processing. If I were you, I'd make sure that you're not writing code that is re-inventing the wheel.



I guess the IDE isn't that much of a big deal, it wouldn't take too much time to get used to a new one, but then you're always getting out of your comfort zone.

I am definitely reinventing the wheel, and it might even be a jagged one. I need as much experience coding as I can get though because I I'd like to find a job as a programmer so until I'm really comfortable I'll probably be reinventing the wheel for a bit.

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Junilu Lacar wrote:

If you want to learn how to write clean code, then get the book "Clean Code" by Robert Martin. Good companion books for this are "Refactoring" by Martin Fowler and "Growing Object-Oriented Software, Guided by Tests" by Freeman and Pryce and books on automated unit testing.


Awesome. I've just added them to my list. I'm especially interested in Clean Code. Thanks Junilu, much appreciated!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is my class too big?