简体   繁体   English

如何简化多个嵌套if else语句的代码

[英]How can I simplify the code for multiple nested if else statements

I am currently using the following code for comparison but as you can see, I am making tonnes of if else statements. 我目前正在使用以下代码进行比较,但正如您所看到的,我正在制作吨if if语句。 Is there a way to simplify the code and make it more efficient? 有没有办法简化代码并使其更有效?

getGenderRef: (grammer=nil) ->
  @gender_ref = ""
  gender = this.get('gender')
  if gender? and gender == 'male'
    if grammer == 'he'
      @gender_ref = 'he'
    else if grammer == 'his'
      @gender_ref == 'his'
    else if grammer == 'him'
      @gender_ref == 'him'
  else if gender? and gender == 'female'
    if grammer == 'he'
      @gender_ref = 'she'
    else if grammer == 'his'
      @gender_ref == 'her'
    else if grammer == 'him'
      @gender_ref == 'her'
  else if gender? or gender == null
    if grammer == 'he'
      @gender_ref = 'he/she'
    else if grammer == 'his'
      @gender_ref == 'his/her'
    else if grammer == 'him'
      @gender_ref == 'him/her'

You could use a map 你可以使用地图

var rules = {
    'female': {
        'he': 'she',
        'his': 'her',
        // ...
    },
    'male': {
        'he': 'he',
        // ...
    },
    'default': {
        'he': 'he/she'
        // ...
    }
};

this.gender_ref = rules[gender ? gender : 'default'][grammer];

It's extensible and can also be dynamically generated from another backend (eg a DB). 它是可扩展的,也可以从另一个后端(例如DB)动态生成。

Edit (by Linus G Thiel) The same in coffeescript: 编辑(由Linus G Thiel撰写与coffeescript相同:

rules =
  female:
    he: 'she'
    his: 'her'
    // ...
  male:
    he: 'he'
    // ...
  default:
    he: 'he/she'
    // ...

@gender_ref = rules[gender or 'default'][grammer]

Consider to use a switch-statement 考虑使用switch语句

switch (gender) {
  case "1":
    alert("");
    break;
  case "2":
    alert("");
    break;
  case "3":
    alert("");
    break;
  case "4":
    alert("");
    break;
  default:
    alert("default");
    break;
}

Use a multi-dimensional hash table. 使用多维哈希表。

grammarByGender = {
    "male": { "he" : "he", "his": "his", "him": "him" },
    "female": {"he" : "she", "his" :"her", "him":"her"},
    "neuter": {"he":"he/she","his":"his/her","him":"him/her"}
}

@gender_ref = grammarByGender[this.get("gender")][grammer]

Use map to cache the possible values. 使用map缓存可能的值。 You can extend it at any point and will be able to simplify your method: 您可以随时扩展它,并且可以简化您的方法:

var genders = {
    "male" : {
       "he": "he",
       "his": "his",
       "him": "him"
   },
   "female" : {
       "he": "she",
       "his": "her",
       "him": "her" 
   },
   "null": {
      "he": "he/she",
      "his": "his/her",
      "him": "him/her"
   }
}

function getGenderRef (grammer) {
  var gender_ref = "",
      availableGenders = genders[this.get('gender')];

  if (availableGenders) {
      gender_ref = availableGenders[grammer];
  }
}

You could reduse the length a tiny bit as follows: 您可以按如下方式缩短长度:

getGenderRef: (grammer=nil) ->
  @gender_ref = ""
  gender = this.get('gender')
  if gender? and gender == 'male'
    if grammer == 'he'
      @gender_ref = 'he'
    else if grammer == 'his'
      @gender_ref == 'his'
    else if grammer == 'him'
      @gender_ref == 'him'
  else if gender? and gender == 'female'
    if grammer == 'he'
      @gender_ref = 'she'
    else if grammer == 'his' or grammer == 'him'
      @gender_ref == 'her'
  else if gender? or gender == null
    if grammer == 'he'
      @gender_ref = 'he/she'
    else if grammer == 'his'
      @gender_ref == 'his/her'
    else if grammer == 'him'
      @gender_ref == 'him/her'

You can't make it a lot shorter than this, because you have alot of different output options. 你不能比它短得多,因为你有很多不同的输出选项。 A swtich / case could be a option, but that would be just as much code. 一个swtich / case可能是一个选项,但这将是同样多的代码。

Another option would be to use a object: 另一种选择是使用一个对象:

references = {
    'male': {
        'he': 'he',
        'his': 'his',
        'him': 'him'
    },
    'female': {
        'he': 'she',
        'his': 'her',
        'him': 'her'
    },
    'null':{
        'he': 'he/she',
        'his': 'his/her',
        'him': 'him/her'
    }
}
//Access:
gender_ref = references[gender ? gender : 'null'][gender_ref];

It would be easy if you go step-by-step. 如果你一步一步走就很容易。 You would learn how to refactor complex conditions that way. 您将学习如何以这种方式重构复杂条件。

First, notice that you have a common check in the first if and its subsequent else if s, that is if gender? 首先,请注意您在第一个if及其后续的else if有一个共同的检查, ifif gender? . You should take that out as a common check and refactor your code as follows: 您应该将其作为常见检查并重构您的代码,如下所示:

  if gender?
    ...
  else # if gender == null
    ...

This forms your main if and else clause. 这构成了主要的if和else子句。 You will nest if and switch statements under these. 您将嵌套if并在这些下面切换语句。 The refactored if/else code is given below: 重构的if / else代码如下:

  if gender?
    if gender == 'male'
      switch grammer
        when 'he' then @gender_ref = 'he'
        when 'his' then @gender_ref = 'his'
        when 'him' then @gender_ref = 'him'
    else if gender == 'female'
      switch grammer
        when 'he' then @gender_ref = 'she'
        when 'his' then @gender_ref = 'her'
        when 'him' then @gender_ref = 'her'
  else # if gender == null
    switch grammer
      when 'he' then @gender_ref = 'he/she'
      when 'his' then @gender_ref = 'his/her'
      when 'him' then @gender_ref = 'him/her'

Switches in Coffeescript can work great for your situation, making the code pretty readable and less complex. Coffeescript中的开关可以很好地适应您的情况,使代码具有可读性和简单性。 You can optimize this further. 您可以进一步优化它。


Here is a simple optimization, where you do the assignment to @gender_ref at the beginning of switch statement (again, taking the common part out): 这是一个简单的优化,您可以在switch语句的开头执行@gender_ref的赋值(再次,将公共部分取出):

  if gender?
    if gender == 'male'
      @gender_ref = switch grammer
        when 'he' then 'he'
        when 'his' then 'his'
        when 'him' then 'him'
    else if gender == 'female'
      @gender_ref = switch grammer
        when 'he' then 'she'
        when 'his' then 'her'
        when 'him' then 'her'
  else # if gender == null
    @gender_ref = switch grammer
      when 'he' then 'he/she'
      when 'his' then 'his/her'
      when 'him' then 'him/her'

You can optimize it even further by moving @gender_ref = assignment to the parent if (it's not working in the else clause though ... I wonder why). 您可以通过将@gender_ref =赋值移动到父级来进一步优化它(如果它不在else子句中工作......我想知道为什么)。

In terms of performance there is nothing wrong with if statements. 在性能方面,if语句没有任何问题。 I can't help but wonder if this isn't just about readibility. 我不禁想知道这不仅仅是关于可读性。

Anyway here is another option still using if statements. 无论如何这里仍然是使用if语句的另一种选择。

@gender_ref = grammer; // male values are the same so use as default

if(  gender == "female" ) {

    if( grammer == 'he' ) @gender_ref = 'she';
    if( grammer == 'his' ) @gender_ref = 'her';
    if( grammer == 'him' ) @gender_ref = 'her';

} else if (  !gender ) { // if not defined

    if( grammer == 'he' ) @gender_ref = 'he/she';
    if( grammer == 'his' ) @gender_ref = 'his/her';
    if( grammer == 'him' ) @gender_ref = 'him/her';
}

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

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