• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is my class too big?

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic