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


Groups > comp.programming > #7499

Refactoring strategy

Path csiph.com!news.redatomik.org!aioe.org!.POSTED!not-for-mail
From Victor Porton <porton@narod.ru>
Newsgroups comp.programming
Subject Refactoring strategy
Date Wed, 08 Mar 2017 21:34:53 +0200
Organization Aioe.org NNTP Server
Lines 39
Message-ID <o9pmcs$lvf$1@gioia.aioe.org> (permalink)
NNTP-Posting-Host QjDAkIYsx7RpawTRsEUcEg.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Complaints-To abuse@aioe.org
User-Agent KNode/4.14.10
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.programming:7499

Show key headers only | 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