Loading ...
Sorry, an error occurred while loading the content.

42Re: [extremeperl] Refactorings?

Expand Messages
  • Ged Haywood
    Jul 29, 2002
    • 0 Attachment
      Hi there,

      On Mon, 29 Jul 2002, Rob Nagler wrote:

      > I'm trying to get an idea what people consider useful refactorings.

      Very simple ones.

      > The following refactoring is a very simple one, but I consider it
      > useful to demonstrate how Perl statement modifiers can be used

      I suppose it's my age.
      I HATE Perl's statement modifiers.
      And I REALLY REALLY HATE "unless".

      > I've got a number of other refactorings in my book, but some have said
      > they are too complicated.

      Like I said.

      > Thanks for the feedback.


      > ----------------------------------------------------------------
      > TITLE: Replace Conditional with Modifier
      > SYNOPSIS
      > You have an if statement with a one-line then clause.
      > Use a Perl statement modifier to keep data flow on the left.
      > unless (defined($value)) {
      > return undef;
      > }
      > becomes
      > return undef unless defined($value);

      Notaroundhereitdoesn't! It becomes

      if(! defined($value) ) { return undef; }

      (and I really mean those curly braces:). (Well, actually, it was
      never "return something unless something" in the first place so the
      refactoring would never have been done anyway:).

      My view would be that undef would be an abnormal condition.
      I usually like my variables to be def. If they aren't def,
      then what's the point of creating them in the first place?
      Ergo, one would test for undef but expect the test to fail.
      Ergo, the return statement should be on the right where it
      doesn't get in the way when you're scanning down the left of
      the page to try to figure out WTBHIGO.

      > This refactoring is controversial, if you aren't used to it.

      Er, run that by me again?

      > It's common to use return, last, die, etc. in Perl. Early exits limit
      > the nesting depth.

      I'd have said early exits prevent program failures and may economize
      on processor cycles, but there we are.

      > Modifiers take this one step further by eliminating all nesting, if
      > possible.

      If eliminating nesting makes it harder to read then give me nesting.

      > The roots of programming are based in logic, not in cooking recipes.
      > This refactoring emulates the declarative style of Prolog.

      Argh++. Declarative is OK where it's OK. I feel it's not OK here.

      > For example, this is the wrong way to use this refactoring:
      > $x = 39, delete($z{$y}) if $is_tuesday; # WRONG USAGE

      Obviously. (But now you're being silly. :)

    • Show all 14 messages in this topic