Shall I specify setter or not?

Here is an initial specification for a simple Address class. This is a simplification as it ignores complications such as apartments in the same building potentially having the same ‘number’,
e.g. 29a, 29b.

 class Address
    {
    private:
    int number;
    string name;
    string postcode;
    public:
    //getters implemented but ommited 
    };

If Address is suppose to served as an utility class (possible future use in other projects by other developers):

//QUESTION
1. For each attribute of Address given in the specification above, state whether it would be appropriate to equip the Address class with a setter method for the corresponding instance variable. Give a brief justification in each case.

Guys this is a question from my assignment so please do not question the way class Address is designed.

Upvotes: 3

Views: 110

Answers (4)

endevour
endevour

Reputation: 708

Should be an immutable class in my pov, with all fields set at construction time, getter for each field.

If you really need to alter the address after construction, think about a setter for the complete set of fields to avoid modification of a single field making the objects data inconsistent. Depends completely on the use of the object.

Upvotes: 0

Eiko
Eiko

Reputation: 25632

I think an address should be immutable, as an address itself cannot change. So if a person changes his address, a new object should be attached.

Not sure what "name" means here, if it is a mis-named street or the name of the person.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

Depends on the source of Address. If, say, you read it from a database, then I wouldn't implement setters as you don't want people changing your database values without the correct permissions. If, however, you read this data in from the user, then you will have to account for the fact that users make typos and adjustments or realize they entered their old address or any of that, and you must provide for the changes.

Upvotes: 8

pauljwilliams
pauljwilliams

Reputation: 19225

To me that should be an immutable class, with all fields set at construction time, and getters for each.

Upvotes: 5

Related Questions