1

This is my code:

if self.a == "":
    raise MyException("The required parameter 'a' is empty")
if self.b == "":
    raise MyException("The required parameter 'b' is empty")
if self.v == "":
    raise MyException("The required parameter 'v' is empty")
if self.g == "":
    raise MyException("The required parameter 'g' is empty")
if self.quality == "":
    raise MyException("The required parameter 'quality' is empty")
if self.abc == "":
    raise MyException("The required parameter 'abc' is empty")

It's seems like too much repeated pattern. The issue is that I can't find a way to make it simpler and compact.

Any ideas?

Luis
  • 1,305
  • 5
  • 19
  • 46
  • 3
    [getattr](https://stackoverflow.com/a/18538330/7311767) – Stephen Rauch Feb 26 '19 at 13:45
  • 1
    Lazy solution: don't bother doing any checking at all. If your program crashes later because one of the parameters has a bad value, then mission accomplished -- the program halted and the user knows what the problem is. – Kevin Feb 26 '19 at 13:45
  • @StephenRauch How would I create the name of the parameter that is missing? – Luis Feb 26 '19 at 13:50
  • @Kevin My users aren't programmers. The error appear to the UI must be well written so they will know which setting is missing. I can't show them trace of error. – Luis Feb 26 '19 at 13:50

2 Answers2

6

Assuming you still want to validate all arguments, here is a solution using getattr as suggested above:

for attribute in ["a", "b", ...]:
    if getattr(self, attribute) == "":
        raise MyException(f"The required parameter '{attribute}' is empty")
Phydeaux
  • 2,795
  • 3
  • 17
  • 35
1

I wouldn't advocate for getting really fancy trying to eliminate all redundancy. Your code is easy to understand. Doing something clever with getattr() or whatnot to avoid repeating each variable name twice will turn a simple set of if statement into something significantly more complicated.

That said, this is one of the rare times where compacting the code is worth it:

if self.a == "":       raise MyException("The required parameter 'a' is empty")
if self.b == "":       raise MyException("The required parameter 'b' is empty")
if self.v == "":       raise MyException("The required parameter 'v' is empty")
if self.g == "":       raise MyException("The required parameter 'g' is empty")
if self.quality == "": raise MyException("The required parameter 'quality' is empty")
if self.abc == "":     raise MyException("The required parameter 'abc' is empty")

You could extract an exception raising method out of it; particularly valuable if you do this type of validation in many other places.

def requireNonEmpty(value, name):
    if value == "":
        raise MyException("The required parameter '{}' is empty".format(name))

requireNonEmpty(self.a, 'a')
requireNonEmpty(self.b, 'b')
requireNonEmpty(self.v, 'v')
requireNonEmpty(self.g, 'g')
requireNonEmpty(self.quality, 'quality')
requireNonEmpty(self.abc, 'abc')
John Kugelman
  • 349,597
  • 67
  • 533
  • 578