http://www.perlmonks.org?node_id=636138


in reply to keywords versus variables

It makes great sense that a program with fewer values to consider would have fewer bugs. Are you talking about two different programs where one has fewer variables by its nature, or are you talking about removing variables consciously from existing programs?

The complicated data structures you mention I've always found handy for decreasing the overall number of variables, BTW. Instead of having a lot of related but disconnected variables that need to be passed around, I can have one data structure that describes a bunch of details about the current topic. I can then delete, alter, pass, and return the one data structure.

Let's look at a typical first-year programming class assignment. I think most of us have done a very simple payroll program. Just the function call illustrates the point:

figure_pay ( $name, $emp_id, $rate, $hours, $gets_overtime, $hours_without_ot, $ot_multi );
vs.
figure_pay ( \%employee );
So all other things being equal, I'd say you're definitely on to something. I think perhaps it's balanced against other issues, though, and shouldn't necessarily be a singular goal.

Replies are listed 'Best First'.
Re^2: keywords versus variables
by rvosa (Curate) on Aug 30, 2007 at 18:00 UTC
    I think perhaps it's balanced against other issues, though, and shouldn't necessarily be a singular goal.
    Oh, definitely. It's a bit of a silly "metric", of course. My point about the complex data structures was that, if you have to do
    $blah->{something}->{and_deeper}->[12]
    you'll be pretty bug prone and so you might consider refactoring into something like
    $deeper->item(12)
    ...which is why I put method calls on one lower level of egregiousness :)
      Good point. Even if you are doing the former on some level, hiding it behind the latter for most code should still help cut down on bugs. Doing the complex one in one place and the simple one in lots of other places could be quite a maintenance win.
Re^2: keywords versus variables
by GrandFather (Saint) on Aug 30, 2007 at 21:36 UTC

    perhaps even better:

    $employee->figure_pay ();

    or:

    $employer->figure_pay ($employee);

    DWIM is Perl's answer to Gödel
      definitely even better:
      $employee->figure_raise();
      or:
      $employer->figure_really_sweet_bonus($employee);
      Especially if you're a company like ADC that runs payrolls for a living.