After several weeks read on this forum, I thought it was time for me to make my first post.
I am currently re-reading Code Complete. I think it’s been 15 years since the last time, and I found that I still can’t write the code ;-)
In any case, on page 138 in Code Complete you will find this coding horror example. (I deleted a piece of code)
class Emplyee {
public:
FullName GetName() const;
Address GetAddress() const;
PhoneNumber GetWorkPhone() const;
...
bool IsZipCodeValid( Address address);
...
private:
...
}
What Steve considers bad is that the functions are not related to each other. Or he writes: “There is no logical connection between employees and routines that check zip codes, phone numbers, or job classifications”
Well, I totally agree with him. Maybe something like the example below is better.
class ZipCode
{
public:
bool IsValid() const;
...
}
class Address {
public:
ZipCode GetZipCode() const;
...
}
class Employee {
public:
Address GetAddress() const;
...
}
, zip , - .
employee.GetAddress().GetZipCode().IsValid();
.
, , -, .
class ZipCode
{
public:
bool IsValid();
}
class Address {
public:
ZipCode GetZipCode() const;
bool IsZipCodeValid() {return GetZipCode()->IsValid());
}
class Employee {
public:
FullName GetName() const;
Address GetAddress() const;
bool IsZipCodeValid() {return GetAddress()->IsZipCodeValid());
PhoneNumber GetWorkPhone() const;
}
employee.IsZipCodeValid();
, .
, . - , ?