?
Solved

which line of code is better c# 4

Posted on 2012-03-29
4
Medium Priority
?
271 Views
Last Modified: 2012-03-29
Just a quick question...

I'm wondering which if either of the following lines of code are better and if I should/shouldn't be doing one or the other....

Customer c;
c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));

Open in new window


OR

Customer c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));

Open in new window


Which is "industry standard"/correct way of doing it?
0
Comment
Question by:scm0sml
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37781076
My opinion:  either is fine. I do think, however, that it depends on what the code surrounding it does. What I mean is that if there is some condition being tested for which the logic you posted should only be run if the condition is true, and the logic for that condition has the potential to generate an exception, then you might use the former to prevent executing what you posted until you know for sure that the condition succeeded.

For example:

Customer c;

try
{
    if (someExceptionGeneratingCondition)
    {
        c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));
    }
}
catch (Exception ex)
{
    throw new ApplicaitonExcption("Exception-generating condition generated an exception", ex);
}

Open in new window


In the above, perhaps FindById just happens to be a long-running or expensive method to call. You may not want to initialize "c" until you are sure that you've passed the "someExceptionGeneratingCondition."
0
 

Author Comment

by:scm0sml
ID: 37781083
kaufmed if I went with your example there, should I be declaring c as Customer c = null; or not?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37781089
It's a good indication to the reader coming behind you that you (yourself) understood that you weren't initializing the variable in its declaration, but you don't have to. The only time you must do that (AFAIK) is if you were referring to "c" outside of the body of the if. In that case, the compiler wouldn't know if you understood that the variable wasn't initialized, and it would give you an error stating uninitialized variable.

e.g.

Customer c = null;  // Must do to prevent compiler error on last line

try
{
    if (someExceptionGeneratingCondition)
    {
        c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));
    }
}
catch (Exception ex)
{
    throw new ApplicaitonExcption("Exception-generating condition generated an exception", ex);
}

c.SomeMethod();

Open in new window

0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37781194
Hi scm0sml,

I know you have asked a specific question, but I could not resist.
how about we use int.tryParse and throw exception if it returns false?

Regards,
Chinmay.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 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