• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this badly designed?

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 96
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Ranch Hand
    Posts: 269
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Agile Software Development"'s author is Alistair Cockburn, not R.C Martin.
    W.
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 ]
     
    author
    Posts: 3892
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    That is a really big piece of pie for such a tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic