Solved

Is having multiples properties chained togeather bad coding practice?

Posted on 2013-01-03
7
283 Views
Last Modified: 2013-01-07
I've been designing some classes for my ASP.NET application and a couple of the classes have fields which are of other class types.  When I was coding my save methods, I found that I was chaining quite a few properties togeather simply to get a value, for example in order to access the value for a street address I would have to type Customer.Contact.Address.StreetAddress and that seems rather convoluted so I'm wondering if this is normal or did I design my classes badly which are below.

Thanks

 public abstract class Person
    {
        private string firstName;
        private string lastName;

        public Person(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

Open in new window


The customer class is what is passed to the method that save to the the database.

public class Customer : Person
    {
        private string notes;
        private Contact contact;

        public Customer(string firstName, string lastName, Contact contact) :
            base (firstName, lastName)
        {
            this.contact = contact;
        }

        public Contact Contact
        {
            get { return contact; }
            set { contact = value; }
        }

        public string Notes
        {
            get { return notes; }
            set { notes = value; }
        }
    }

Open in new window


Contact class is used in Customer to store contact info

public class Contact
    {
        private Address address;
        private Company company;
        private List<ContactDetail> contactDetails;

        public Contact(Address address, Company company)
        {
            this.address = address;
            this.company = company;
            this.contactDetails = new List<ContactDetail>();
        }


        public Address Address
        {
            get { return address; }
            set { address = value; }
        }

        public Company Company
        {
            get { return company; }
            set { company = value; }
        }

        public List<ContactDetail> ContactDetails
        {
            get { return contactDetails; }
        }

        public void AddContactDetails(ContactDetail detail)
        {
            contactDetails.Add(detail);
        }

    }

Open in new window


Contact detail is a class that is decalred as a list in Contact to store multiple contact types

public class ContactDetail
    {
        private ContactType contactType;
        private string contactInfo;

        public ContactDetail(ContactType contactType, string contactInfo)
        {
            this.contactType = contactType;
            this.contactInfo = contactInfo;
        }

        public string ContactInfo
        {
            get { return contactInfo; }
            set { contactInfo = value; }
        }
        
        public ContactType ContactType
        {
            get { return contactType; }
            set { contactType = value; }
        }

    }

Open in new window


Address contains basic address info

 public class Address
    {
        public Address(string streetAddess, string cityName, string provinceName, string postalCode)
        {
            this.StreetAddress = streetAddess;
            this.CityName = cityName;
            this.ProvinceName = provinceName;
            this.PostalCode = postalCode;
        }

        public string StreetAddress { get; set; }
        public string CityName { get; set; }
        public string ProvinceName { get; set; }
        public string PostalCode { get; set; }
    }

Open in new window

0
Comment
Question by:obb-taurus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 38742488
Be as granular as you can be... this will lend to easier maintenance and scalability. There are ways around lengthy property traversing.
0
 

Author Comment

by:obb-taurus
ID: 38742497
Can you give an example of what I might do to avoid the lengthy properties in this situation?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38742541
I personally prefer it--so long as the chain isn't too long--for readability, but that's not to say you cannot find ways to achieve the same level of readability by using local variables as well. I think "Customer.Contact.Address.StreetAddress" might be pushing it (length-wise) for my personal taste. I believe MS' recommendation is to use local variables rather than chaining. This can approach can be a bit easier to debug also.

One seemingly ironic thing, however, is that you often see method chaining used heavily in LINQ. It can be difficult, at times, to debug such chaining.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:obb-taurus
ID: 38742578
Kaufmed, when you say to use local variables rather than chaining, do you simply mean something like the following?  

public Address GetAddress()
{
      Address address = Contact.Address;
      return address;
}

Open in new window


Or are you suggesting to create a local primitave variable such as a string and assign the value from Address to the string for example:

public string GetEmail()
{
      string address = Address.Email;
      return address;
}

Open in new window


Is this what you meant?

Thanks
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38742819
Exactly. Although, I think in that trivial example there wouldn't be much benefit. As your methods grow in size, and you are passing objects between multiple method calls within the same caller, then you would get a better benefit from what you are showing. For example:

public void RedactAddresses(Contact c)
{
    Address r = c.Addresses.ResidentialAddress;
    Address m = c.Addresses.MailingAddress;

    r.Street = RedactStreet(r.Street);
    r.City = RedactCity(r.City);
    r.State = RedactState(r.State);

    m.Street = RedactStreet(m.Street);
    m.City = RedactCity(m.City);
    m.State = RedactState(m.State);
}

Open in new window

0
 

Author Comment

by:obb-taurus
ID: 38743540
Just so I have an idea, what would the code for the method RedActStreet(r.street) look like in your example?  Sorry about asking for so much detail but for some reason designing classes from the ground up seems to be a challenge for me and when I ran into this situation it threw me for a loop.

Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38743710
Mind you this is all hypothetical code, but perhaps something along the lines of:

public string RedactStreet(string street)
{
    string result = steet;

    if (street == "123 Main St.")
    {
        result = string.Empty;
    }

    return result;
}

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question