对于这些嵌套的if/elseif语句,有什么更优雅的解决方案? [英] What is a more elegant solution to these nested if/elseif statements?

查看:196
本文介绍了对于这些嵌套的if/elseif语句,有什么更优雅的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,其中包含具有用户个人资料的用户.概要文件中的许多字段都是可选的.

I'm building a website that contains users with user profiles. Many of the fields in the profile are optional.

有很多用户生成的内容的机会,因此我需要在网站的许多不同位置(评论,帖子等)显示该内容的作者.在用户的个人资料中,他能够(可选)填写他的名字",姓氏"和显示名称".

There is an opportunity for a lot of user-generated content, and so I need to display the author of this content in many different locations of the site (comments, posts, etc.). In the user's profile, he is able to (optionally) fill out his "first name", his "last name", and a "display name".

为了显示作者,我编写了一个帮助程序方法,该方法通过提供的这些字段数组进行查找,并按照优先顺序返回最适合用户的名称:

To display the author, I wrote a helper method that looks through a provided array of these fields and returns the most appropriate name for the user, in this order of preference:

  1. 如果用户填写了display_name,则会显示该内容.
  2. 如果用户填写了first_namelast_name,但没有填写display_name,则它将同时显示两个名称
  3. 如果用户仅填写first_name,它将显示first_name.
  4. 如果用户仅填写last_name,它将显示last_name.
  5. 如果其他所有方法均失败,则将显示用户ID,即user123
  6. 如果不存在任何数组键,或者参数为NULL,则名称将显示为NULL
  1. If the user filled out display_name, this will be displayed.
  2. If the user filled out first_name and last_name, but no display_name, it will display both names
  3. If the user only filled out first_name, it will display first_name.
  4. If the user only filled out last_name, it will display last_name.
  5. If all else fails, a user id will be displayed i.e. user123
  6. If none of the array keys are present, or the parameter is NULL, the name will display as NULL

该方法效果很好,但是很丑陋.必须有一种方法可以用嵌套的if/else语句替代它来美化它.

The method works great, but it's ugly. There must be a way to beautify this with an alternative to nested if/else statements.

public function nameify($names = NULL) {
    $name = '';
    if (!empty($names)) {
        if (!empty($names['display_name'])) {
            $name = $names['display_name'];
        } elseif (!empty($names['first_name'])) {
            $name = $names['first_name'];
            if (!empty($names['last_name'])) {
                $name .= ' ' . $names['last_name'];
            }
        } elseif (!empty($names['last_name'])) {
            $name = $names['last_name'];
        }

        if (empty($name) && !empty($names['id'])) {
            $name = 'user' . $names['id'];
        } else {
            $name = 'NULL';
        }
    } else {
        $name = 'NULL';
    }
    return $name;
}

推荐答案

public function nameify($names = NULL) {
    if ($names) {
        if (!empty($names['display_name'])) {
            return $names['display_name'];
        }
        if (!empty($names['first_name'])) {
            $name = $names['first_name'];
        } 
        if (!empty($names['last_name'])) {
            $name .= ' ' . $names['last_name'];
        }
        if (empty($name) && !empty($names['id'])) {
            $name = 'user' . $names['id'];
        }
    }
    return $name ? ltrim($name) : 'NULL';
}

首先设置默认值,如果没有其他匹配项,则返回默认值.然后,既然我们一直想要返回显示名称,就这样做.

Set the default first, and return that if nothing else matches. Then since we always want to return the display name if we have it do just that.

进行调整以防止返回"NULL"

Tweak to prevent returning "NULL "

这篇关于对于这些嵌套的if/elseif语句,有什么更优雅的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆