aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Is this badly designed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Is this badly designed?" Watch "Is this badly designed?" New topic
Author

Is this badly designed?

k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
Hi
I have a class which represents a user in a system. A user has a number of fields such as
age, name, surname, email. etc.
The class has maybe around 10 fields all which are required. What Is the best way to ensure all these
fields hold a value? I was going to include a constructor which requires values for each field but this means the signature will be expecting 10 arguement. I'm sure i've read somwehere that
methods with long parameter lists are indicative of bad design.
Should I split this class into furhter objects (perhaps store address fields in a seperate object)
Do you guys have a process you go through in order to identify the objects needed and how far to decompose them into further objects?
THanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I would definately be tempted to split the address information out into a separate class.
That would allow you to design an "equals" method so you can ask questions like "do these two people have the same address ?" Also bear in mind that how to split up addresses and what fields to require is a surprisingly hard decision which often changes, so you want to minimise the amount of code you need to change with the requirements for an address.
For a more detailed answer you'll need to list the fields you have so far for us.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Frank Carver:
For a more detailed answer you'll need to list the fields you have so far for us.

And preferably also what you need to do with those fields. Remember, objects should primarily be about behaviour, not about data.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
hi
The fields i have so far are as follows:
private String SNAME;
private String FNAME;
private String addressline1;
private String addressline2;
private int user_id;
private String town;
private String postcode;
private boolean police_checked;
private boolean active;
private boolean subscription_expire;
private String our_email;
private String email;
private Date last_login;
private int views;
private String info;
private String password;
All intend to do with them behaviour wise is either set them or retrieve them.
Also thanks for the tip.

Thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by k Oyedeji:
The fields i have so far are as follows:
private String SNAME;
private String FNAME;
private String addressline1;
private String addressline2;
private int user_id;
private String town;
private String postcode;
private boolean police_checked;
private boolean active;
private boolean subscription_expire;
private String our_email;
private String email;
private Date last_login;
private int views;
private String info;
private String password;

I think there are at least two more classes hiding - probably something along the lines of Address and Account.

All intend to do with them behaviour wise is either set them or retrieve them.

I don't believe you...
The system certainly needs to do something with the data. Typically, you shouldn't use accessor to get the data and do something with it, but tell the object containing the data to do something. See http://c2.com/cgi/wiki?TellDontAsk
For example, your system probably needs to check for the validity of a given password. Instead of getting the correct password from the AccountInfo and comparing it to the input, you should probably have a method "boolean AccountInfo.isValidPassword(String)" or the like.
By encapsulating behaviour and accompanied data in the same class, you reduce coupling between your classes and thereby increase maintainability.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I agree with Ilja about the behaviour issue. I also think that you should reconsider how you represent names and addresses. Ask yourself the following kind of questions:
  • What purpose does splitting names into SNAME and FNAME serve? (we know from the JavaRanch "naming policy" that a lot of names don't fit naturally into this pattern)
  • What happens if someone has an address with more than two lines?
  • What happens if someone has an address with less than two lines? (you told us all fields are mandatory!)
  • What happens if someone has an address which needs something after the "town"? (a county, state, province, or country, for example)
  • What sets the "subscription_expire" flag?, and when?
  • Should it ever be possible to login without setting the "last_login" date ?


  • and so on.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Frank Carver:
    I agree with Ilja about the behaviour issue. I also think that you should reconsider how you represent names and addresses. Ask yourself the following kind of questions:

    Notice, though, that you don't necessarily need to answer all that questions before you write the first line of code. The earlier you start coding, the earlier you will experience how your code *really* wants to be shaped and therefore are able to refactor accordingly.
    I guess there is a new discussion practice emerging: Pair Answering...
    k Oyedeji
    Ranch Hand

    Joined: Jul 07, 2002
    Posts: 96
    Guys
    Thanks for the replies. They've really helped me since i tend to look at objects and what they contain when really i should be looking at the
    behvaiour i need. For example i will need to validate a password but i would have done this outside the object by retreiving the password and then doing a comparison. Finally what resources would you guys recommend for learning more about OO?
    Thanks
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    For me, the book "Refactoring" by Martin Fowler was the eye-opener.
    "Agile Software Development (2nd Edition)" by Robert C. Martin looks really promising, too. The author also has some good articles published at http://www.objectmentor.com/resources/listArticles?key=topic&topic=Design%20Principles
    Wilfried LAURENT
    Ranch Hand

    Joined: Jul 13, 2001
    Posts: 269
    "Agile Software Development"'s author is Alistair Cockburn, not R.C Martin.
    W.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Wilfried LAURENT:
    "Agile Software Development"'s author is Alistair Cockburn, not R.C Martin.
    W.

    I was refering to "Agile Software Development - Principles, Patterns and Practices" by Robert C. Martin.
    Whereas Alistairs book is about agile *teams*, Roberts is mainly about agile *code*. Both are *very* good books, in my not so humble opinion...
    [ November 28, 2002: Message edited by: Ilja Preuss ]
    Kyle Brown
    author
    Ranch Hand

    Joined: Aug 10, 2001
    Posts: 3892
        
        5
    Originally posted by k Oyedeji:
    Guys
    Thanks for the replies. They've really helped me since i tend to look at objects and what they contain when really i should be looking at the
    behvaiour i need. For example i will need to validate a password but i would have done this outside the object by retreiving the password and then doing a comparison. Finally what resources would you guys recommend for learning more about OO?
    Thanks

    Actually, I'd recommend the classic "Designing Object Oriented Software" by Rebecca Wirfs-Brock. It will teach you how to use CRC cards to design your software, which will FORCE you to use a more behavior-oriented approach.
    Kyle


    Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
    See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Is this badly designed?