Should a function have only one return statement?

13. August 2015

8 Kommentare

I would say that you can definitely have more than one return statement. Therefore I would like to suggest to:

1.) Minimize the number of returns in each routine. It’s harder to understand a routine if, reading it at the bottom, you’re unaware of the possibility that it returned somewhere above.

2.)Use a return when it enhances readability. In certain routines, once you know the answer, you want to return it to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any cleanup, not returning immediately means that you have to write more code.

Question: Which of the functions shown below would you prefer and why?

Version 1:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        $templateId = $config[$id];
    } else {
        $templateId = 0;
    }

    return $templateId;
}

Version 2:

function getTemplateId(array $config, $id)
{
    if (array_key_exists($id, $config)) {
        return $config[$id];
    }

    return 0;
}

Please write your answer as a comment :-)

¬ geschrieben von gjerokrsteski in PHP Tricks und Tipps

Wann ist ein Unternehmen agil?

21. Mai 2014

1 Kommentar

Agil bedeutet “aktiv und beweglich”. Aber was heißt das für ein Unternehmen? In diesem Artikel werde ich versuchen, diese Frage zu beantworten und zu erklären, wie man ganz einfach herausfinden kann, ob ein Unternehmen agil ist. Den ganzen Beitrag lesen.

¬ geschrieben von gjerokrsteski in PHP Tricks und Tipps, Projektmanagement

PIMF Quick Starters

20. Januar 2014

0 Kommentare

Have you ever wished a PHP framework that perfectly adapts to your projects needs, your programming experience and your customers budget? A thin PHP framewrok with less implementing rools and easy to learn how to use it? PIMF is about to satisfy your demands! Den ganzen Beitrag lesen.

¬ geschrieben von gjerokrsteski in News und Trends, PHP Tricks und Tipps, PIMF

WTF mysqli_stmt::bind_param aliasing violation

16. Januar 2013

1 Kommentar

I was searching for a class which supports multiple calls to mysqli_stmt::bind_param, because I have scenarios where I build huge SQL statements over different functions with variable numbers of parameters. But I didn’t found one. So I have just written up this little piece of code I would like to share with you. And for me it works. In doBindParam() it seems to depend from the PHP version if referenceValues() must be used or if $params can be used directly. Please note that reference is required for PHP 5.3 an higher. If won`t work using type-safe executing, than see here (http://bugs.mysql.com/bug.php?id=48284) for fixing your PHP and MySQL version. In my case I have to use it. Den ganzen Beitrag lesen.

¬ geschrieben von gjerokrsteski in PHP Tricks und Tipps

Powered by Wordpress • Abonniere den RSS Feed