Re: [RFC PATCH] gpiolib: Refactor gpio_export

From: Grant Likely
Date: Fri Oct 19 2012 - 18:43:51 EST


On Fri, Oct 19, 2012 at 11:14 PM, Ryan Mallon <rmallon@xxxxxxxxx> wrote:
> On 19/10/12 21:07, Linus Walleij wrote:
>
>> On Wed, Oct 17, 2012 at 1:41 AM, Ryan Mallon <rmallon@xxxxxxxxx> wrote:
>>
>>> The gpio_export function uses nested if statements and the status
>>> variable to handle the failure cases. This makes the function logic
>>> difficult to follow. Refactor the code to abort immediately on failure
>>> using goto. This makes the code slightly longer, but significantly
>>> reduces the nesting and number of split lines and makes the code easier
>>> to read.
>>>
>>> Signed-off-by: Ryan Mallon
>>
>> Very good initiative!
>>
>>> +++ b/drivers/gpio/gpiolib.c
>>> @@ -702,68 +702,74 @@ int gpio_export(unsigned gpio, bool direction_may_change)
>>> {
>>> unsigned long flags;
>>> struct gpio_desc *desc;
>>> - int status = -EINVAL;
>>> + int status;
>>> const char *ioname = NULL;
>>> + struct device *dev;
>>>
>>> /* can't export until sysfs is available ... */
>>> if (!gpio_class.p) {
>>> pr_debug("%s: called too early!\n", __func__);
>>> - return -ENOENT;
>>> + status = -ENOENT;
>>> + goto fail;
>>
>> Why bother with all the goto:s here since there are no resources
>> to clean up? Just pr_debug() and return -ENOENT; is good enough.
>>
>> I don't quite see the point.
>
> I did it this way just so that there would be a single exit point.
> I don't mind either way, so I'll update the ones without any
> clean up to simply return.

Single exit points are overrated. The code is shorter and easier to
read to return immediately if there isn't any unwinding to do.

g.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/