Prev: Evetlog
Next: debugger class
From: csharper on 6 May 2010 11:50 I have been wondering about this for a while. Suppose I have a class Employee which has defined the following properties: EmployeeId FirstName MiddleName LastName .... ... Suppose now in another class, I need to use the employeeId in a method GetContactInfo. Now, should I pass an Employee object to GetContactInfo like below? // Version 1. public IEnumerable<ContactInfo> GetContactInfo(Employee employee) { // The implementation goes here. } Or should I simply pass the EmployeeId int like below? // Version 2. public IEnumerable<ContactInfo> GetContactInfo(int employeeId) { // The implementation goes here. } Both will work, but Q1: From an OOP perspective, which is more OOP-like? Q2: Which version has better performance? Version 1? Version 2? No difference? Thank you.
From: Family Tree Mike on 6 May 2010 13:12 On 5/6/2010 11:50 AM, csharper wrote: > I have been wondering about this for a while. Suppose I have a class > Employee which has defined the following properties: > > EmployeeId > FirstName > MiddleName > LastName > ... ... > > Suppose now in another class, I need to use the employeeId in a method > GetContactInfo. > > Now, should I pass an Employee object to GetContactInfo like below? > > // Version 1. > public IEnumerable<ContactInfo> GetContactInfo(Employee employee) > { > // The implementation goes here. > } > > Or should I simply pass the EmployeeId int like below? > > // Version 2. > public IEnumerable<ContactInfo> GetContactInfo(int employeeId) > { > // The implementation goes here. > } > > Both will work, but > > Q1: From an OOP perspective, which is more OOP-like? > Q2: Which version has better performance? Version 1? Version 2? No > difference? > > Thank you. It would appear that version one is better performance wise, because in version two you will need to find the employee information for the id, hence requiring a search that version one does not need. Version one is more OOP-like in that if employee and customer had contact info, you could make the method accept the base of both. -- Mike
From: Mr. Arnold on 6 May 2010 13:27 csharper wrote: > I have been wondering about this for a while. Suppose I have a class > Employee which has defined the following properties: > > EmployeeId > FirstName > MiddleName > LastName > ... ... > > Suppose now in another class, I need to use the employeeId in a method > GetContactInfo. > > Now, should I pass an Employee object to GetContactInfo like below? > > // Version 1. > public IEnumerable<ContactInfo> GetContactInfo(Employee employee) > { > // The implementation goes here. > } > > Or should I simply pass the EmployeeId int like below? > > // Version 2. > public IEnumerable<ContactInfo> GetContactInfo(int employeeId) > { > // The implementation goes here. > } > > Both will work, but > > Q1: From an OOP perspective, which is more OOP-like? > Q2: Which version has better performance? Version 1? Version 2? No > difference? > > Thank you. You just pass the ID. You would pass the object if you needed to pass the object to do more things with the object's properties or methods within the method you have passed the object. Speed considerations would be applied to you sending the object or a single primitive type over the wire using WCF between a WCF client and service as an example. Why send the whole object, if you're only working with a single primitive type property within the object, which takes time to transmit the object?
From: csharper on 6 May 2010 13:47 On May 6, 1:27 pm, "Mr. Arnold" <Arn...(a)Arnold.com> wrote: > csharper wrote: > > I have been wondering about this for a while. Suppose I have a class > > Employee which has defined the following properties: > > > EmployeeId > > FirstName > > MiddleName > > LastName > > ... ... > > > Suppose now in another class, I need to use the employeeId in a method > > GetContactInfo. > > > Now, should I pass an Employee object to GetContactInfo like below? > > > // Version 1. > > public IEnumerable<ContactInfo> GetContactInfo(Employee employee) > > { > > // The implementation goes here. > > } > > > Or should I simply pass the EmployeeId int like below? > > > // Version 2. > > public IEnumerable<ContactInfo> GetContactInfo(int employeeId) > > { > > // The implementation goes here. > > } > > > Both will work, but > > > Q1: From an OOP perspective, which is more OOP-like? > > Q2: Which version has better performance? Version 1? Version 2? No > > difference? > > > Thank you. > > You just pass the ID. You would pass the object if you needed to pass > the object to do more things with the object's properties or methods > within the method you have passed the object. > > Speed considerations would be applied to you sending the object or a > single primitive type over the wire using WCF between a WCF client and > service as an example. Why send the whole object, if you're only working > with a single primitive type property within the object, which takes > time to transmit the object? Let's say, in a standalone application, no network transmission is involved. Are you suggesting that in this situation then there is no difference btwn version 1 and version 2 in terms of performance? Also, your opinion about whether we should pass an Employee object or an employeeId int is opposite to that of Family Tree Mike, any comments? Thanks.
From: csharper on 6 May 2010 13:52
On May 6, 1:12 pm, Family Tree Mike <FamilyTreeM...(a)ThisOldHouse.com> wrote: > On 5/6/2010 11:50 AM, csharper wrote: > > > > > > > I have been wondering about this for a while. Suppose I have a class > > Employee which has defined the following properties: > > > EmployeeId > > FirstName > > MiddleName > > LastName > > ... ... > > > Suppose now in another class, I need to use the employeeId in a method > > GetContactInfo. > > > Now, should I pass an Employee object to GetContactInfo like below? > > > // Version 1. > > public IEnumerable<ContactInfo> GetContactInfo(Employee employee) > > { > > // The implementation goes here. > > } > > > Or should I simply pass the EmployeeId int like below? > > > // Version 2. > > public IEnumerable<ContactInfo> GetContactInfo(int employeeId) > > { > > // The implementation goes here. > > } > > > Both will work, but > > > Q1: From an OOP perspective, which is more OOP-like? > > Q2: Which version has better performance? Version 1? Version 2? No > > difference? > > > Thank you. > > It would appear that version one is better performance wise, because in > version two you will need to find the employee information for the id, > hence requiring a search that version one does not need. > > Version one is more OOP-like in that if employee and customer had > contact info, you could make the method accept the base of both. > > -- > Mike Thanks for sharing. I understand that you say passing in the id would require a search for that int, but passing in an Employee object would also require a search for the EmployeeId integer inside the GetContactInfo method. So, I don't see this as a valid argument with regard to application level performance. |