4 added 10 characters in body
source | link

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to give a name or make clear what the conditional is intended to detect (and encapsulate how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to name make clear what the conditional is intended to detect (and encapsulate how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to give a name or make clear what the conditional is intended to detect (and encapsulate how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}
3 added 17 characters in body
source | link

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to name make clear what the conditional is intended to detect (notand encapsulate how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to make clear what the conditional is intended to detect (not how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to name make clear what the conditional is intended to detect (and encapsulate how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}
2 added 12 characters in body
source | link

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to make clear what the conditional is intended to detect (not how it detects it):

public int getMoneyByPersons(){
  if(isSingleEmployeeisSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleEmployeeisSingleDepartmentHead() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to make clear what the conditional is intended to detect (not how it detects it):

public int getMoneyByPersons(){
  if(isSingleEmployee()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleEmployee() {
   return persons.size() == 1;
}

I have seen a fair amount of code like in the (modified) OP in DB retrievals. The query returns a list, but the business rules say there can be only one element. And then, of course, something changed for 'just this one case' to a list with more than one item. (Yeah, I said a fair amount of times. It's almost like they ... nm)

So rather than then create a constant, I would (in clean code method) create a method to make clear what the conditional is intended to detect (not how it detects it):

public int getMoneyByPersons(){
  if(isSingleDepartmentHead()){ 
    // TODO - return money for one person
  } else {
    // TODO - calculate and return money for people.
  }
}

public boolean isSingleDepartmentHead() {
   return persons.size() == 1;
}
1
source | link