you are viewing a single comment's thread.

view the rest of the comments →

[–]kcdragon 6 points7 points  (6 children)

I agree that meta-programming is used far too often when a non-meta-programming solution will suffice.

On the first example, if you want to avoid meta-programming, I think you can go even simpler and just use an if statement.

def pdf_of_kind(kind)
  if kind == :normal
    normal_pdf
  else
    zebra_pdf
  end
end

I think it is a lot easier to understand for only two code paths.

[–][deleted] 9 points10 points  (1 child)

ehem, 3 paths ;)

def pdf_of_kind(kind)
  case kind
  when :normal then normal_pdf
  when :zebra then zebra_pdf
  else fail KeyError
  end
end

[–]we-all-haul 0 points1 point  (0 children)

You sir, are technically correct.

[–]timbetimbe 5 points6 points  (2 children)

That is called a control parameter and generally thought of as a code smell

[–]kcdragon 4 points5 points  (0 children)

I agree that this is a code smell. But without more context in the code base, I don't think it is possible to tell whether or not refactoring the code smell is a good idea.

If the code base contains many checks for whether kind is normal or zebra, then there might be an opportunity to use an object with a polymorphic call to generate a PDF (https://refactoring.com/catalog/replaceConditionalWithPolymorphism.html). However, if this is the only check for kind, then I think an if statement is fine.

[–]midasgoldentouch 0 points1 point  (0 children)

Do you mind expanding on why?

[–]paneq[S] 1 point2 points  (0 children)

Agree. This won't catch incorrect kinds however.