【问题标题】:How can these if/else blocks be improved? [closed]如何改进这些 if/else 块? [关闭]
【发布时间】:2013-04-20 11:15:09
【问题描述】:

我认为我的代码逻辑有错误,因为有两个连续的 elseif 块具有几乎相同的代码:

//first elseif
elseif (!$socialUser && empty($siteUserId)) {
        //first time user
        $secretWord = $this->RandomString->getRand(7);
        $data = array('User'=> array(
                  'username' => $this->userData['username'],
                  'password' => $this->Auth->password($secretWord),
                  'email' => $this->userData['email'],
                  'name' => $this->userData['name']               
              ));
        $siteUserId = $this->_addSiteUser($data);
        if ($siteUserId){
          $data = array('SocialUser' => array(
            'title' => 'facebook',
            'identifier' => $this->FB_userId,
            'user_id' => $siteUserId
        ));
          if ($this->_addSocialUser($data)){
            $this->Auth->login($siteUserId);
            $l = $this->Session->read('Auth.redirect');
        if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');       
        $this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
        $this->Session->delete('Auth.redirect');        
        $this->controller->redirect($l);
          }
          else{
            $this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
         // $this->controller->redirect($this->Auth->loginAction);
          $this->logout();
          $this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));

          }
        }

    }
//second elseif
    elseif($socialUser && empty($siteUserId)){
      $secretWord = $this->RandomString->getRand(7);
        $data = array('User'=> array(
                  'username' => $this->userData['username'],
                  'password' => $this->Auth->password($secretWord),
                  'email' => $this->userData['email'],
                  'name' => $this->userData['name']               
              ));
        $siteUserId = $this->_addSiteUser($data);
        if ($siteUserId){
//HERE IS ONLY THE DIFFERENCE
          $data = $socialUser;
          $data['SocialUser']['user_id'] = $siteUserId;
//DIFFERENCE END HERE
          if ($this->_addSocialUser($data)){
            $this->Auth->login($siteUserId);
            $l = $this->Session->read('Auth.redirect');
        if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');       
        $this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
        $this->Session->delete('Auth.redirect');        
        $this->controller->redirect($l);
          }
          else{
            $this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
         // $this->controller->redirect($this->Auth->loginAction);
          $this->logout();
          $this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));

          }
        }

    }

我认为代码运行良好,但在两个连续的 elseif 块之间复制和粘贴代码块我感觉不太好?有什么想法可以改进此代码吗?还是没事?

【问题讨论】:

  • 请至少正确缩进代码,这非常难以阅读。
  • @deceze 它是从 netbeans IDE 格式的代码复制和粘贴。最重要的是基本思想本身。即两个连续的elseif几乎相同的代码!
  • 我不在乎它来自哪里,它很难阅读,因此很难给出任何具体的建议。如果您希望人们分析您的代码中的错误或建议,请不要让他们不必要地爆炸。
  • 是的,这很难读。我通常不会对此投反对票,但如果有人可能会帮助你说“请缩进代码”,如果我需要帮助,我会这样做。

标签: php algorithm logic


【解决方案1】:

嵌套你的ifs

...
elseif (empty($siteUserId)) {
    // common code
    if ($socialUser) {
        // social-user specific code
    } else {
        // non-social-user specific code
    }
    // more common code
}
...

【讨论】:

    【解决方案2】:

    你应该创建另一个嵌套的if-else

    elseif (empty($siteUserId)) {
        ...
        if ($socialUser) {
            ...
        } else {
            ...
        }
        ...
    }
    

    这样,您可以将empty($siteUserId)true 中常见的代码分开,并根据$socialUser 的布尔值区分其余代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2021-11-28
      • 1970-01-01
      • 2013-02-15
      • 2017-09-05
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多