简体   繁体   English

我如何在代码变得凌乱之前处理分支?

[英]How do I handle branches of code before it gets too messy?

So my actual question is, if I have multiple if-elseif cases looking for 1 right match, but all the cases got alot of the same code but some cases are a little diffrent, which way is most efficient? 所以我的实际问题是,如果我有多个if-elseif案例寻找1个正确的匹配,但是所有案例都有很多相同的代码,但有些案例有些不同,哪种方法最有效?

Sorting by roles and repeating code? 按角色排序并重复代码?

$user_role = 2;

if ($user_role === 1) {
  codeMethod1();
  codeMethod2();
} elseif ($user_role === 2) {
  codeMethod1();
  codeMethod2();
  codeMethod3();
} elseif ($user_role === 3) {
  codeMethod2();
  codeMethod3();
}

or, sorting by code/process and make huge if statements? 或者,按代码/流程排序并生成大量if语句?

$user_role = 2;

if ($user_role === 1 || 2) {
  codeMethod1();
} 

codeMethod2();

if ($user_role === 2 || 3) {
  codeMethod3();
}

I realise that for some people this is a dumb question, because they already got it all figured out. 我意识到对于某些人来说这是一个愚蠢的问题,因为他们已经弄明白了。 I'm new to programming and I just want to get it right from the start. 我是编程的新手,我只想从一开始就做好它。 I don't want to ruin my program or ruin it for people who might have to fix my code. 我不想破坏我的程序或不想让可能要修复我的代码的人破坏它。

UPDATE UPDATE

Scenario for question clarification: 用于阐明问题的方案:

When a user enters my site, I identify them as 1 of 5 categories. 当用户进入我的网站时,我将其识别为5个类别中的1个。 Some users will get the same treatment with some variations while others will get wildly diffrent treatments. 一些使用者将获得相同的治疗,但会有一些变化,而另一些使用者则将获得完全不同的治疗。 There is about 20 different methods, where some users will use them all and others will use few. 大约有20种不同的方法,其中一些用户将全部使用它们,而另一些将很少使用。

So, is is better and/or more efficient to list the methods each category needs, even though alot of the code will look the same. 因此,列出许多类别所需的方法会更好和/或更有效,即使很多代码看起来都一样。

Example: 例:

$user_role = getCurrentUserRole();

switch ($user_role) {
  case 1:
    (uses method1() to method10())
    break;

  case 2:
    (uses method5() to method15())
    break;

  case 3:
    (uses method10() to method20())
    break

  case 4:
    (uses method1() to method20())
    break

  case 5:
    method1();
    method4();
    method8();
    method15();
    method20();
}

OR, is it better to list every method and use if statements to see if the $user_role needs it? 或者,最好列出每个方法并使用if语句查看$user_role需要它?

Example: 例:

$user_role = getCurrentUserRole();

switch ($user_role) {
  // Check for method1
  case (1 || 4) {
    method1();
  }

  // Check for method2
  case (1 || 4) {
    method2();
  }

  ... skip ...

  // Check for method5
  case (1 || 2 || 4) {
    method5();
  }

  .. Continue checking role permission for each method ..
}

Please ignore my bad english, and tell me to elaborate if you don't get my question. 请忽略我的英语不好,如果您没有收到我的问题,请告诉我详细说明。

Group functions under your user roles functions and simply call those functions for particular user role. 将功能分组在用户角色功能下,并仅针对特定用户角色调用这些功能。

$user_role = 2;

switch($user_role){
    case 1:
        userRole1(); 
        break;

    case 2:
       userRole2();
       break;

    case 3:
      userRole3();
      break;
}

function userRole1(){
        codeMethod1();
        codeMethod2();
}
function userRole2(){
       codeMethod1();
       codeMethod2();
       codeMethod3();
}
function userRole3(){
      codeMethod2();
      codeMethod3(); 
}

I'd use a roles→features association. 我将使用角色→功能关联。

But in your case it might be more appropriate to associate role numbers to possible functions with a simple map: 但是,在您的情况下,使用简单的映射将角色编号与可能的功能关联起来可能更合适:

$role_funcs = [
    "method1" => [ 1, 2,        ],
    "method2" => [ 1, 2, 3,    5],
    "method3" => [       3,    5],
    "method4" => [ 1,    3, 4,  ],
    // …
    "method9" => [    2, 3,    5],
];

Which gives a compact overview on which function blocks will be run for which user class. 简要概述了将针对哪个用户类运行哪些功能块。 And it's pretty trivial to utilize: 而且利用起来很简单:

foreach ($role_funcs as $callback=>$for_roles) {
    if (in_array($user_role, $for_roles)) {
        $callback();
    }
}

Now that only works of course if you really have them all available as functions. 现在,只有在您确实将所有功能都可用时,它才起作用。 (Otherwise you might even want to aim higher, wrap users and roles into objects, to utilize one of the dispatcher patterns implicitly.) (否则,您甚至可能想要更高的目标,将用户和角色包装到对象中,以隐式地利用其中一种调度程序模式。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM