Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #7499

Refactoring strategy

From Victor Porton <porton@narod.ru>
Newsgroups comp.programming
Subject Refactoring strategy
Date 2017-03-08 21:34 +0200
Organization Aioe.org NNTP Server
Message-ID <o9pmcs$lvf$1@gioia.aioe.org> (permalink)

Show all headers | View raw


I was assigned to refactor several similar functions, namely extract common 
code fragments for no repetition.

The main problem is to pass long lists of variables and references to 
variables into the "fragment" functions.

I propose the following as a solution:

Replace all (or most of) local variables with members of a record/structure 
(actually hash in Perl) members. Let call this structure $Values.

Then I pass a reference to the structure into fragment functions instead of 
passing all variables separately.

But actually I also make a copy of $Params hash (which contains most 
function parameters). In the old code local variables were created from this 
hash like:

my $Site = $Params->{Site} || '';

(For these who don't know Perl, it means to put the value of {Site} 
parameter from the hash $Params into $Site variable, using empty string 
instead of null (`undef` in Perl) value.)

To overcome this, I first make a copy of $Params:

my %ParamsCopy = %$Params;

and then modify it inplace:

$ParamsCopy{Site} ||= '';

Any comments are welcome.

Particularly, I question: Is it worth to merge $ParamsCopy and $Values into 
one structure instead of two?

-- 
Victor Porton - http://portonvictor.org

Back to comp.programming | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Refactoring strategy Victor Porton <porton@narod.ru> - 2017-03-08 21:34 +0200
  Re: Refactoring strategy Julio Di Egidio <julio@diegidio.name> - 2017-03-08 12:04 -0800

csiph-web