You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cloudstack.apache.org by Alex Hitchins <al...@alexhitchins.com> on 2014/04/11 17:54:12 UTC

Coding Standards Questions

All,

 

As I've been looking through the code, I've seen a fair number of places
where return statements are called within if statements and the like. I've
always found that having one place to return is easier to debug and follow
the code flow.

 

Are there any guidelines on this? Or is it not a major concern?

 

Thanks,

 

Alex

 

 

 

Alex Hitchins

------------------------------ 

E: alex@alexhitchins.com

W: alexhitchins.com

M: 07788 423 969

T: 01892 523 587

 


RE: Coding Standards Questions

Posted by Ian Southam <IS...@schubergphilis.com>.
Hi,

It is all a question of good computer science.  A return can easily become a sneaky goto so, it does makes sense to avoid dotting them all over methods because it can (and often does) indicate that the code is poorly structured.

That said, use common sense.  Making code less readable or unnecessarily complex makes it more difficult to maintain and that, is also bad from a computer science POV.

>From Stephen's example

public int getNumberOfWidgets(Foo input) {
    if (input == null)
      return -1;

    int ret;
    // 30 lines of computation
    return ret;
}

It is also possible to do somethinglike

public int getNumberOfWidgets(Foo input) {
    if (input == null)
      return -1;
  else
     return doMainProcessing(...)
}

Int doMainProcessing() {
    int ret;
    // 30 lines of computation
    return ret;
}

This can avoid the need for everything being nested in an (ugly) if not null condition.

It is also probably a good idea to consider whether it is reasonable that input is null.  Avoiding exceptions is a good thing, returning junk that does more damage than an exception is a bad thing (tm).

--
Grts!
Ian

-----Original Message-----
From: Gabor Apati-Nagy [mailto:gabor.apati-nagy@citrix.com] 
Sent: maandag 14 april 2014 12:59
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

To my mind having multiple return statements might be unclear sometimes, but personally I do prefer this option. In this case we can easily make sure that the return value is not going to be changed. If we know the result, let's return it asap. If we reach a return statement anywhere, we can be sure that the right value is returned there is no need to double check further code in the method (except when exception can be raised by the return itself).

If we had only one return at the end of the method, it almost always adds more complexity (needs if/else blocks, maybe nested ones..., breaks for loops) The compiler checks for code paths that do not return before reaching the end of the method, this check is almost useless if we have one return at the end of the method.

Regards,
Gabor


-----Original Message-----
From: Stephen Turner [mailto:Stephen.Turner@citrix.com] 
Sent: 14 April 2014 10:35
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

I agree that pulling the return value out into a variable and returning it at the end can be clearer, but I wouldn't want to make an absolute rule about it. Sometimes returning early can reduce the number of nested if/else statements and increase clarity. For example, I would rather see:

public int getNumberOfWidgets(Foo input) {
    if (input == null)
      return -1;

    int ret;
    // 30 lines of computation
    return ret;
}

than put the bulk of the function in an else block. But maybe others disagree?

-- 
Stephen Turner


-----Original Message-----
From: Alex Hitchins [mailto:alex@alexhitchins.com] 
Sent: 11 April 2014 21:45
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

Daan,

Are you referring to keeping line lengths up to 80 characters? Sorry - tired eyes.

My thoughts were more that in a function there should only be one "return"
statement rather than many, all nested in layers of if/else statements.


Alex Hitchins | 07788 423 969 | 01892 523 587
---------------------------------------------

-----Original Message-----
From: Daan Hoogland [mailto:daan.hoogland@gmail.com]
Sent: 11 April 2014 18:30
To: dev
Subject: Re: Coding Standards Questions

H Alex,

I agree with you that would be nicer if your function fits in a screen.
Another coding convention we should adhere to. As it is I think it not so much 'not a major concern' as too much to ask for.
Feel free to refactor and submit patches;)

Daan

On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
wrote:
> All,
>
>
>
> As I've been looking through the code, I've seen a fair number of 
> places where return statements are called within if statements and the 
> like. I've always found that having one place to return is easier to 
> debug and follow the code flow.
>
>
>
> Are there any guidelines on this? Or is it not a major concern?
>
>
>
> Thanks,
>
>
>
> Alex
>
>
>
>
>
>
>
> Alex Hitchins
>
> ------------------------------
>
> E: alex@alexhitchins.com
>
> W: alexhitchins.com
>
> M: 07788 423 969
>
> T: 01892 523 587
>
>
>



--
Daan


RE: Coding Standards Questions

Posted by Gabor Apati-Nagy <ga...@citrix.com>.
To my mind having multiple return statements might be unclear sometimes, but personally I do prefer this option. In this case we can easily make sure that the return value is not going to be changed. If we know the result, let's return it asap. If we reach a return statement anywhere, we can be sure that the right value is returned there is no need to double check further code in the method (except when exception can be raised by the return itself).

If we had only one return at the end of the method, it almost always adds more complexity (needs if/else blocks, maybe nested ones..., breaks for loops) The compiler checks for code paths that do not return before reaching the end of the method, this check is almost useless if we have one return at the end of the method.

Regards,
Gabor


-----Original Message-----
From: Stephen Turner [mailto:Stephen.Turner@citrix.com] 
Sent: 14 April 2014 10:35
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

I agree that pulling the return value out into a variable and returning it at the end can be clearer, but I wouldn't want to make an absolute rule about it. Sometimes returning early can reduce the number of nested if/else statements and increase clarity. For example, I would rather see:

public int getNumberOfWidgets(Foo input) {
    if (input == null)
      return -1;

    int ret;
    // 30 lines of computation
    return ret;
}

than put the bulk of the function in an else block. But maybe others disagree?

-- 
Stephen Turner


-----Original Message-----
From: Alex Hitchins [mailto:alex@alexhitchins.com] 
Sent: 11 April 2014 21:45
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

Daan,

Are you referring to keeping line lengths up to 80 characters? Sorry - tired eyes.

My thoughts were more that in a function there should only be one "return"
statement rather than many, all nested in layers of if/else statements.


Alex Hitchins | 07788 423 969 | 01892 523 587
---------------------------------------------

-----Original Message-----
From: Daan Hoogland [mailto:daan.hoogland@gmail.com]
Sent: 11 April 2014 18:30
To: dev
Subject: Re: Coding Standards Questions

H Alex,

I agree with you that would be nicer if your function fits in a screen.
Another coding convention we should adhere to. As it is I think it not so much 'not a major concern' as too much to ask for.
Feel free to refactor and submit patches;)

Daan

On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
wrote:
> All,
>
>
>
> As I've been looking through the code, I've seen a fair number of 
> places where return statements are called within if statements and the 
> like. I've always found that having one place to return is easier to 
> debug and follow the code flow.
>
>
>
> Are there any guidelines on this? Or is it not a major concern?
>
>
>
> Thanks,
>
>
>
> Alex
>
>
>
>
>
>
>
> Alex Hitchins
>
> ------------------------------
>
> E: alex@alexhitchins.com
>
> W: alexhitchins.com
>
> M: 07788 423 969
>
> T: 01892 523 587
>
>
>



--
Daan


Re: Coding Standards Questions

Posted by Rajani Karuturi <Ra...@citrix.com>.
I agree with Stephen. IDEs are smart and can highlight all the exits of a function.

Functions which are more than a page long are more of an issue than the multiple return statements. It is very difficult to follow such code. 

~Rajani



On 14-Apr-2014, at 3:05 pm, Stephen Turner <St...@citrix.com> wrote:

> I agree that pulling the return value out into a variable and returning it at the end can be clearer, but I wouldn't want to make an absolute rule about it. Sometimes returning early can reduce the number of nested if/else statements and increase clarity. For example, I would rather see:
> 
> public int getNumberOfWidgets(Foo input) {
>    if (input == null)
>      return -1;
> 
>    int ret;
>    // 30 lines of computation
>    return ret;
> }
> 
> than put the bulk of the function in an else block. But maybe others disagree?
> 
> -- 
> Stephen Turner
> 
> 
> -----Original Message-----
> From: Alex Hitchins [mailto:alex@alexhitchins.com] 
> Sent: 11 April 2014 21:45
> To: dev@cloudstack.apache.org
> Subject: RE: Coding Standards Questions
> 
> Daan,
> 
> Are you referring to keeping line lengths up to 80 characters? Sorry - tired eyes.
> 
> My thoughts were more that in a function there should only be one "return"
> statement rather than many, all nested in layers of if/else statements.
> 
> 
> Alex Hitchins | 07788 423 969 | 01892 523 587
> ---------------------------------------------
> 
> -----Original Message-----
> From: Daan Hoogland [mailto:daan.hoogland@gmail.com]
> Sent: 11 April 2014 18:30
> To: dev
> Subject: Re: Coding Standards Questions
> 
> H Alex,
> 
> I agree with you that would be nicer if your function fits in a screen.
> Another coding convention we should adhere to. As it is I think it not so much 'not a major concern' as too much to ask for.
> Feel free to refactor and submit patches;)
> 
> Daan
> 
> On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
> wrote:
>> All,
>> 
>> 
>> 
>> As I've been looking through the code, I've seen a fair number of 
>> places where return statements are called within if statements and the 
>> like. I've always found that having one place to return is easier to 
>> debug and follow the code flow.
>> 
>> 
>> 
>> Are there any guidelines on this? Or is it not a major concern?
>> 
>> 
>> 
>> Thanks,
>> 
>> 
>> 
>> Alex
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Alex Hitchins
>> 
>> ------------------------------
>> 
>> E: alex@alexhitchins.com
>> 
>> W: alexhitchins.com
>> 
>> M: 07788 423 969
>> 
>> T: 01892 523 587
>> 
>> 
>> 
> 
> 
> 
> --
> Daan
> 


RE: Coding Standards Questions

Posted by Stephen Turner <St...@citrix.com>.
I agree that pulling the return value out into a variable and returning it at the end can be clearer, but I wouldn't want to make an absolute rule about it. Sometimes returning early can reduce the number of nested if/else statements and increase clarity. For example, I would rather see:

public int getNumberOfWidgets(Foo input) {
    if (input == null)
      return -1;

    int ret;
    // 30 lines of computation
    return ret;
}

than put the bulk of the function in an else block. But maybe others disagree?

-- 
Stephen Turner


-----Original Message-----
From: Alex Hitchins [mailto:alex@alexhitchins.com] 
Sent: 11 April 2014 21:45
To: dev@cloudstack.apache.org
Subject: RE: Coding Standards Questions

Daan,

Are you referring to keeping line lengths up to 80 characters? Sorry - tired eyes.

My thoughts were more that in a function there should only be one "return"
statement rather than many, all nested in layers of if/else statements.


Alex Hitchins | 07788 423 969 | 01892 523 587
---------------------------------------------

-----Original Message-----
From: Daan Hoogland [mailto:daan.hoogland@gmail.com]
Sent: 11 April 2014 18:30
To: dev
Subject: Re: Coding Standards Questions

H Alex,

I agree with you that would be nicer if your function fits in a screen.
Another coding convention we should adhere to. As it is I think it not so much 'not a major concern' as too much to ask for.
Feel free to refactor and submit patches;)

Daan

On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
wrote:
> All,
>
>
>
> As I've been looking through the code, I've seen a fair number of 
> places where return statements are called within if statements and the 
> like. I've always found that having one place to return is easier to 
> debug and follow the code flow.
>
>
>
> Are there any guidelines on this? Or is it not a major concern?
>
>
>
> Thanks,
>
>
>
> Alex
>
>
>
>
>
>
>
> Alex Hitchins
>
> ------------------------------
>
> E: alex@alexhitchins.com
>
> W: alexhitchins.com
>
> M: 07788 423 969
>
> T: 01892 523 587
>
>
>



--
Daan


Re: Coding Standards Questions

Posted by Daan Hoogland <da...@gmail.com>.
no I don't mean linelength, more no of lines. In a function of several
100s of lines you want to return asap. and you don't want a method of
100s of lines

On Fri, Apr 11, 2014 at 2:44 PM, Alex Hitchins <al...@alexhitchins.com> wrote:
> Daan,
>
> Are you referring to keeping line lengths up to 80 characters? Sorry - tired
> eyes.
>
> My thoughts were more that in a function there should only be one "return"
> statement rather than many, all nested in layers of if/else statements.
>
>
> Alex Hitchins | 07788 423 969 | 01892 523 587
> ---------------------------------------------
>
> -----Original Message-----
> From: Daan Hoogland [mailto:daan.hoogland@gmail.com]
> Sent: 11 April 2014 18:30
> To: dev
> Subject: Re: Coding Standards Questions
>
> H Alex,
>
> I agree with you that would be nicer if your function fits in a screen.
> Another coding convention we should adhere to. As it is I think it not so
> much 'not a major concern' as too much to ask for.
> Feel free to refactor and submit patches;)
>
> Daan
>
> On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
> wrote:
>> All,
>>
>>
>>
>> As I've been looking through the code, I've seen a fair number of
>> places where return statements are called within if statements and the
>> like. I've always found that having one place to return is easier to
>> debug and follow the code flow.
>>
>>
>>
>> Are there any guidelines on this? Or is it not a major concern?
>>
>>
>>
>> Thanks,
>>
>>
>>
>> Alex
>>
>>
>>
>>
>>
>>
>>
>> Alex Hitchins
>>
>> ------------------------------
>>
>> E: alex@alexhitchins.com
>>
>> W: alexhitchins.com
>>
>> M: 07788 423 969
>>
>> T: 01892 523 587
>>
>>
>>
>
>
>
> --
> Daan
>



-- 
Daan

Re: Coding Standards Questions

Posted by Koushik Das <ko...@citrix.com>.
On 12-Apr-2014, at 2:14 AM, Alex Hitchins <al...@alexhitchins.com> wrote:

> Daan,
> 
> Are you referring to keeping line lengths up to 80 characters? Sorry - tired
> eyes.
> 
> My thoughts were more that in a function there should only be one "return"
> statement rather than many, all nested in layers of if/else statements.

+1.

> 
> 
> Alex Hitchins | 07788 423 969 | 01892 523 587
> ---------------------------------------------
> 
> -----Original Message-----
> From: Daan Hoogland [mailto:daan.hoogland@gmail.com] 
> Sent: 11 April 2014 18:30
> To: dev
> Subject: Re: Coding Standards Questions
> 
> H Alex,
> 
> I agree with you that would be nicer if your function fits in a screen.
> Another coding convention we should adhere to. As it is I think it not so
> much 'not a major concern' as too much to ask for.
> Feel free to refactor and submit patches;)
> 
> Daan
> 
> On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
> wrote:
>> All,
>> 
>> 
>> 
>> As I've been looking through the code, I've seen a fair number of 
>> places where return statements are called within if statements and the 
>> like. I've always found that having one place to return is easier to 
>> debug and follow the code flow.
>> 
>> 
>> 
>> Are there any guidelines on this? Or is it not a major concern?
>> 
>> 
>> 
>> Thanks,
>> 
>> 
>> 
>> Alex
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Alex Hitchins
>> 
>> ------------------------------
>> 
>> E: alex@alexhitchins.com
>> 
>> W: alexhitchins.com
>> 
>> M: 07788 423 969
>> 
>> T: 01892 523 587
>> 
>> 
>> 
> 
> 
> 
> --
> Daan
> 


RE: Coding Standards Questions

Posted by Alex Hitchins <al...@alexhitchins.com>.
Daan,

Are you referring to keeping line lengths up to 80 characters? Sorry - tired
eyes.

My thoughts were more that in a function there should only be one "return"
statement rather than many, all nested in layers of if/else statements.


Alex Hitchins | 07788 423 969 | 01892 523 587
---------------------------------------------

-----Original Message-----
From: Daan Hoogland [mailto:daan.hoogland@gmail.com] 
Sent: 11 April 2014 18:30
To: dev
Subject: Re: Coding Standards Questions

H Alex,

I agree with you that would be nicer if your function fits in a screen.
Another coding convention we should adhere to. As it is I think it not so
much 'not a major concern' as too much to ask for.
Feel free to refactor and submit patches;)

Daan

On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com>
wrote:
> All,
>
>
>
> As I've been looking through the code, I've seen a fair number of 
> places where return statements are called within if statements and the 
> like. I've always found that having one place to return is easier to 
> debug and follow the code flow.
>
>
>
> Are there any guidelines on this? Or is it not a major concern?
>
>
>
> Thanks,
>
>
>
> Alex
>
>
>
>
>
>
>
> Alex Hitchins
>
> ------------------------------
>
> E: alex@alexhitchins.com
>
> W: alexhitchins.com
>
> M: 07788 423 969
>
> T: 01892 523 587
>
>
>



--
Daan


Re: Coding Standards Questions

Posted by Daan Hoogland <da...@gmail.com>.
H Alex,

I agree with you that would be nicer if your function fits in a
screen. Another coding convention we should adhere to. As it is I
think it not so much 'not a major concern' as too much to ask for.
Feel free to refactor and submit patches;)

Daan

On Fri, Apr 11, 2014 at 9:54 AM, Alex Hitchins <al...@alexhitchins.com> wrote:
> All,
>
>
>
> As I've been looking through the code, I've seen a fair number of places
> where return statements are called within if statements and the like. I've
> always found that having one place to return is easier to debug and follow
> the code flow.
>
>
>
> Are there any guidelines on this? Or is it not a major concern?
>
>
>
> Thanks,
>
>
>
> Alex
>
>
>
>
>
>
>
> Alex Hitchins
>
> ------------------------------
>
> E: alex@alexhitchins.com
>
> W: alexhitchins.com
>
> M: 07788 423 969
>
> T: 01892 523 587
>
>
>



-- 
Daan