[owncloud-devel] Defensive programming

Bernhard Posselt dev at bernhard-posselt.com
Wed Feb 4 12:20:13 GMT 2015


Take a look at this stackoverflow answer :)
http://stackoverflow.com/questions/77127/when-to-throw-an-exception

It should outline perfectly well when to throw exceptions

On 02/04/2015 01:14 PM, Vincent Petry wrote:
> Hello,
>
> After debugging several bugs, I recently found that many pieces of the
> core code is assuming that a function call will always return the
> expected value.
>
> There were many scenarios where a given bug was causing \OCP::getUser()
> to return null but the code would carry on working with that null value,
> and cause side-effects:
> https://github.com/owncloud/core/blob/stable7/apps/files_encryption/hooks/hooks.php#L438
>
> I suggest that from now on we try and code more defensively.
> This means that we want to check for correct values more often.
> For example when the current user is needed, check for "null" afterwards
> and throw an exception / log a warning instead of letting the code continue.
>
> Here is another example: https://github.com/owncloud/core/issues/13862
> And here when `getRelativePath()` fails, it is not a normal situation,
> we need to throw an exception instead of continuing:
> https://github.com/owncloud/core/blob/master/lib/private/files/view.php#L792
>
> What do you think ?
>
> I have raised https://github.com/owncloud/core/issues/13885 with a list
> of cases that can be fixed. If you know of other places in the code that
> can be improved that way, please add them in the checklist (and/or
> submit a PR to fix them)
>
> Thanks,
>
> Vincent
>
>
>
> _______________________________________________
> Devel mailing list
> Devel at owncloud.org
> http://mailman.owncloud.org/mailman/listinfo/devel



More information about the Devel mailing list