2

I have a static method in my DataLayer GetCompany. I made it because i need a company object to be compared with the property. The question is for the following code should i make another object and assign the returned Company Object to it or just use that in the conditions.

Which is the best way according to performance.

if (property != null && property.CompanyNum > 0)
{
    if (property is PersonalDetail &&
        (Property.GetCompany(property.CompanyNum)).
            CompanyType.ToUpper() != "COI")
    {
        if (property.TaxSubTypeId != 19)
        {
            if (property.CompanyNum == 81 && property.TaxSubTypeId == 11)
            {
                // Tax Sub Type of Compressor & Company Name
                // Midcon Compression LLC 
                SetPersonalNonCOI81Inputs();
            }
        }
    }
}

I have many conditions below so I am just mentioning couple of them just let me know if how can I optimize it.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Nivid Dholakia
  • 5,272
  • 4
  • 30
  • 55
  • There are more serious problems than performance. Measure the performance first and find out where the bottleneck is. – Eranga Sep 01 '11 at 14:54

1 Answers1

1

This sounds like a case of premature optimization. If your GetCompany method is not being called very often, then you have nothing to worry about.

However, from your listing, it appears that the Property.GetCompany call may be going out to a database to retrieve comapny information, so if you are calling this method many times per user request, then it may make sense to cache the return value in the code that is accessing this property.

Another issue that you may have is that your call to SetPersonalNonCOI81Inputs is likely modifying some global state value, since it is being called from your static GetCompany method. This will cause problems in a multi-threaded environment (assuming this is a web application), because you can have concurrent modifications to the shared state value. So, you may want to remove the SetPersonalNonCOI81Inputs method and simply return a new object there.

Community
  • 1
  • 1
Brent M. Spell
  • 2,257
  • 22
  • 14
  • SetNONCOI81... is a seperate layer method and it has to do nothing. But the thing here is i just need a company type to get the valid condition true. that is why i have created the method just to check type of company the static method has nothing to do with no modification nothing. – Nivid Dholakia Sep 01 '11 at 15:09
  • In that case, you should have nothing to worry about. The performance impact of the conditions you are evaluating in this code is negligible. – Brent M. Spell Sep 01 '11 at 15:33