Req: Simple C# Code Analysis

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Simurina

    #1

    Req: Simple C# Code Analysis


    Hi

    I would appreciate being clarified simple peace of code,


    protected void ShowDailyEvents ()
    {
    DateTime d = MyCalendar.Sele ctedDate;
    DataSet dataSet = LoadMyCalendarD ata();
    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


    --
    np: (Winamp is not active ;-)


  • Matthias Klöpper

    #2
    Re: Simple C# Code Analysis

    > protected void ShowDailyEvents ()[color=blue]
    > {
    > DateTime d = MyCalendar.Sele ctedDate;
    > DataSet dataSet = LoadMyCalendarD ata();
    > 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?[/color]

    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.Sele ctedDate;
    DataSet dataSet = LoadMyCalendarD ata();
    if (dataSet != null)
    {
    more code... bla bla
    }
    }


    Greetings,
    Matti


    Comment

    • Daniel Billingsley

      #3
      Re: Simple C# Code Analysis

      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.

      "Matthias Klöpper" <mkloepper@ba-com.net> wrote in message
      news:uUpq1molDH A.2080@TK2MSFTN GP10.phx.gbl...[color=blue][color=green]
      > > protected void ShowDailyEvents ()
      > > {
      > > DateTime d = MyCalendar.Sele ctedDate;
      > > DataSet dataSet = LoadMyCalendarD ata();
      > > 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[/color][/color]
      "return",[color=blue][color=green]
      > > does this just skip the if structure and continues to the rest of the[/color][/color]
      code[color=blue][color=green]
      > > in the method or something else?[/color]
      >
      > 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.Sele ctedDate;
      > DataSet dataSet = LoadMyCalendarD ata();
      > if (dataSet != null)
      > {
      > more code... bla bla
      > }
      > }
      >
      >
      > Greetings,
      > Matti
      >
      >[/color]


      Comment

      Working...