Code Complete 2ed, composition and delegation?

It's pay now vs. pay later. You can write the delegation and wrapper functions up front (pay now) and then have less work changing the innards of employee.IsZipCodeValid() later. Or, you can tunnel through to IsZipCodeValid by writing employee.GetAddress().GetZipCode().IsValid(); everywhere you need it in the code, but pay later should you decide to change your class de ;).

This is probably the best answer. Because their is not any elegant solution to the problem. – Arlukin Mar 11 '10 at 21:58.

You are missing the logical connection: class ZipCode { public: bool IsValid(); } class Address { public: ZipCode GetZipCode() const; bool IsAddressValid(); bool IsValid() {return GetZipCode()->IsValid() && IsAddressValid()); } class Employee { public: FullName GetName() const; Address GetAddress() const; bool IsEmployeeValid(); bool IsValid() {return GetAddress()->IseValid() && IsEmployeeValid()); PhoneNumber GetWorkPhone() const; } employee.IsValid().

You still would need to drill down into ZipCode::IsValid() eventually. You call employee.IsValid() and it returns false. OK, why isn't the employee valid?

So you call employee.GetAddress().IsValid(), and it returns false. OK, why isn't the address valid? So you call employee.GetAddress().GetZipCode().IsValid() and you're back at the original problem.

– indiv Mar 11 '10 at 17:35 Plus that you might want to call GetValue() on ZipCode, to output in a dialog box or webpage. That will give you the same problem. – Arlukin Mar 11 '10 at 21:50 @indiv, if you are going to take this example at face value then yes you would need to drill down in that way but you'd probably have a method instead that does this for you and presents back error data/messages for the data entry screen.

If this invalid state is the result of bad data in the app then I think that the answer is to look to clean the data before injecting it into your model. – Lazarus Mar 13 '10 at 9:19 @Arlukin, you'd probably actually pass an Address object to your Controller or Presentation layer and let it pick it apart. In OOD you need to think about the real objects you are presenting and how they interact.

Employee has a Address has a Zipcode. Indirectly, Employee has a Zipcode and you may even have that as a field in your object if and only if you need it but in the general scope of things it's far more likely that Address has a Zipcode is the model you'd go for. At the end of the day it's all down to your model, you might want to read Bruce Eckel's Thinking in C++, url below – Lazarus Mar 13 '10 at 9:28 Bruce Eckel's Thinking in C++ (Vol 1 & 2) FREE mindview.Net/Books/TICPP/ThinkingInCPP2e.

Html – Lazarus Mar 13 '10 at 9:29.

Since there's no logical connection between the Employee class and zip-code validation, you could put the Zip code validation into the Address class where it more logically belongs. Then you can ask the Address class to validate the Zip code for you. Class Address { public: static IsZipValid(ZipCode zip) { return zip.isValid(); } }; Then you do Address::IsZipValid(employee.GetAddress().GetZipCode()); I think this is satisfactory under your constraints of logical association and Law of Demeter.

Probably a question of personal taste, but I think more dots is easier to read. Address::IsZipValid(employee.GetAddress().GetZipCode()); vs employee.GetAddress().GetZipCode().IsZipValid(); – Arlukin Mar 11 '10 at 21:56 @Arlukin: No, I don't believe you gain anything. I was just answering the question as posed.

Demeter imposes no laws on me, so I would implement it as employee.GetAddress().GetZipCode.IsZipValid(). That way feels natural to me. – indiv Mar 11 '10 at 23:08 @Arlukin, this isn't about winning or losing, it's about building a model that makes sense.

If, at the end of the day, one way feels more natural to you than another and you aren't part of a team (if you are then get the team guidance) then go with whatever makes sense to you. I'm sure if you get two programmers into a room you'll get six opinions on how to code a certain solution ;) – Lazarus Mar 13 '10 at 9:33.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions