Req: Simple C# Code Analysis

  • Thread starter Thread starter Ivan Simurina
  • Start date Start date
I

Ivan Simurina

Hi

I would appreciate being clarified simple peace of code,


protected void ShowDailyEvents()
{
DateTime d = MyCalendar.SelectedDate;
DataSet dataSet = LoadMyCalendarData();
if (dataSet == null)
{
return;
}

more code... bla bla
}

I dont understand whats the role of "return" here.
There should be some work on the dataset, but theres just "return",
does this just skip the if structure and continues to the rest of the code
in the method or something else?

thanx
 
protected void ShowDailyEvents()
{
DateTime d = MyCalendar.SelectedDate;
DataSet dataSet = LoadMyCalendarData();
if (dataSet == null)
{
return;
}

more code... bla bla
}

I dont understand whats the role of "return" here.
There should be some work on the dataset, but theres just "return",
does this just skip the if structure and continues to the rest of the code
in the method or something else?

Hi Ivan,
this sort of code is usualy used if you want to skip the rest of your
function. In this case if dataSet is not available than there is no use in
executing any further code.
This is equivalent to the following but easier to read (less parentheses,
less indents) IMHO:

protected void ShowDailyEvents()
{
DateTime d = MyCalendar.SelectedDate;
DataSet dataSet = LoadMyCalendarData();
if (dataSet != null)
{
more code... bla bla
}
}


Greetings,
Matti
 
Well, if we're offering opinions Matthias I for one find your alternative
easer to read as it provides a more straightforward linear flow through the
method. However, I would agree though that if the code after the null test
includes additional ifs, indents, etc. then reducing the nesting by one
level with the simple return in the first example becomes more readable.
That's my opinion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top