Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > strategys other than subroutine and OO?

Reply
Thread Tools

strategys other than subroutine and OO?

 
 
Ela
Guest
Posts: n/a
 
      03-20-2008
I have 300 lines of codes that are used roughly twice. Some of the lines
have delicate difference in execution (e.g. ~6 variables have to be replaced
and conditional statements are also different) and therefore many arguments
need to be passed into it in order to differentiate the execution parts. OO
development is analogously also difficult. I wonder if there is any good way
to reuse the codes, e.g. some sort of "goto" rather than duplicating these
lines. Whenever there is any change, I find that is really error-prone.


 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      03-20-2008
Ela wrote:
> I have 300 lines of codes that are used roughly twice. Some of the lines
> have delicate difference in execution (e.g. ~6 variables have to be replaced
> and conditional statements are also different) and therefore many arguments
> need to be passed into it in order to differentiate the execution parts. OO
> development is analogously also difficult. I wonder if there is any good way
> to reuse the codes,


There are good ways and there are easy ways. Choose good.

> e.g. some sort of "goto" rather than duplicating these
> lines. Whenever there is any change, I find that is really error-prone.


"For a number of years I have been familiar with the observation that
the quality of programmers is a decreasing function of the density of go
to statements in the programs they produce. More recently I discovered
why the use of the go to statement has such disastrous effects, and I
became convinced that the go to statement should be abolished from all
"higher level" programming languages (i.e. everything except, perhaps,
plain machine code). At that time I did not attach too much importance
to this discovery; I now submit my considerations for publication
because in very recent discussions in which the subject turned up, I
have been urged to do so."

Edager W. Dijkstra. Go To Statement Considered Harmful. 1968.



In nearly 30 years of programming I have completely avoided use of GOTO
in a professional context. All I have seen continues to convince me that
Dijkstra was right on this matter. YMMV.

--
RGB
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-20-2008
"Ela" <(E-Mail Removed)> writes:

> I have 300 lines of codes that are used roughly twice. Some of the lines
> have delicate difference in execution (e.g. ~6 variables have to be replaced
> and conditional statements are also different) and therefore many arguments
> need to be passed into it in order to differentiate the execution parts. OO
> development is analogously also difficult. I wonder if there is any good way
> to reuse the codes, e.g. some sort of "goto" rather than duplicating these
> lines. Whenever there is any change, I find that is really error-prone.


Sounds like using closures/passing functions as arguments could be very
helpful here, but without seeing your code it's impossible to be sure.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      03-20-2008
Ela wrote:
) I have 300 lines of codes that are used roughly twice. Some of the lines
) have delicate difference in execution (e.g. ~6 variables have to be replaced
) and conditional statements are also different) and therefore many arguments
) need to be passed into it in order to differentiate the execution parts. OO
) development is analogously also difficult. I wonder if there is any good way
) to reuse the codes, e.g. some sort of "goto" rather than duplicating these
) lines. Whenever there is any change, I find that is really error-prone.

Maybe you can take the lines that are exactly duplicated and put those
into sub functions. If you define the sub functions inside the main
function they can access the main function's 'my' variables, so you
don't need to pass them as arguments.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Ela
Guest
Posts: n/a
 
      03-20-2008
> Maybe you can take the lines that are exactly duplicated and put those
> into sub functions. If you define the sub functions inside the main
> function they can access the main function's 'my' variables, so you
> don't need to pass them as arguments.
>


From perlintro:
A Perl script or program consists of one or more statements. These
statements are simply written in the script in a straightforward fashion.
There is no need to have a main() function or anything of that kind.

So would you mind giving a very simple example? i guess your suggestion is
very close to the final solution since i never have the concept you suggest.


 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 7:34 am, "Ela" <(E-Mail Removed)> wrote:
> I have 300 lines of codes that are used roughly twice. Some of the lines
> have delicate difference in execution (e.g. ~6 variables have to be replaced
> and conditional statements are also different) and therefore many arguments
> need to be passed into it in order to differentiate the execution parts. OO
> development is analogously also difficult. I wonder if there is any good way
> to reuse the codes, e.g. some sort of "goto" rather than duplicating these
> lines. Whenever there is any change, I find that is really error-prone.


I have a suggestion. Since you didn't post a coding sample, my
suggestion may not be appropros, but maybe it will.

First, you surely don't have a function that is 300 lines long. If you
do, I don't think I can help. You probably have batches of lines that
are duplicated. The first thing I would do is abstract these batches
of lines into separate functions and call them separately, like this:

&batch_a;
&batch_b;
&batch_c;
sub batch_a { #first batch of lines
}
sub batch_b { #second batch of lines
}
sub batch_c { #third batch of lines
}

Second, you can pass different parameters into these functions to
account for the different variables. If you need to set a variable as
a result of some behavior, set the variable by calling a function,
like this:

$var = &setvar;
sub setvar { #code here sets a var which you would return
return $var; }

Third, you can replace your conditional statements with booleans and
set them outside the function call, like this:

my $conditional = 0
$conditional = 1 if &some_function_that_returns_true;
$contitional = 0 unless &some_function_that_returns_true;
if ($conditional) { #code to execute
}

Fourth, if possible, place your common code (our variables and
functions) in a PM and import the backage. This way, you can separate
the interface and the functionality, and this will also allow you to
refactor your code if and when you have the time to do so. You don't
need to create classes and objects to do this.

Fifth, as a strength building exercise, you might attempt to place ALL
your code, every line of it, in user defined functions and execute the
program by the judicious call of these functions. Of course, you would
still need to declare your lexical variables, but you set them by
function calls.

CC
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-20-2008
ccc31807 <(E-Mail Removed)> writes:

> &batch_a;
> &batch_b;
> &batch_c;


Erm, you *do* know what that does, right? In most circumstances you want
to use

batch_a();
batch_b();
batch_c();

instead.

See perlfaq7

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Ela
Guest
Posts: n/a
 
      03-20-2008
> Fourth, if possible, place your common code (our variables and
> functions) in a PM and import the backage. This way, you can separate
> the interface and the functionality, and this will also allow you to
> refactor your code if and when you have the time to do so. You don't
> need to create classes and objects to do this.
>
> Fifth, as a strength building exercise, you might attempt to place ALL
> your code, every line of it, in user defined functions and execute the
> program by the judicious call of these functions. Of course, you would
> still need to declare your lexical variables, but you set them by
> function calls.
>
> CC


I don't quite understand the last 2. Maybe I need to refer to some Advanced
Perl Programming? Anyway I guess your first 3 suggestions should be
sufficient to solve the headache-causing clumsy coding problem. Thank you
and all others' help!


 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 11:10 am, Joost Diepenmaat <(E-Mail Removed)>
> Erm, you *do* know what that does, right?


I know exactly what calling a function using & does. As I understand
it, it uses your programs underscore varibles.

> In most circumstances you want
> to use
>
> batch_a();
> batch_b();
> batch_c();


I'm sorry if what I posted was confusing. Yes, you are correct, and I
certainly did not want to mislead anyone. However, I certainly wasn't
misled when I wrote what I did and didn't think that anyone else would
be, either. In any case, you can call a function clearly and
ambiguously using &. And I just prefer to do so. TIMTOWTDI.

CC
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 11:30 am, "Ela" <(E-Mail Removed)> wrote:
> > Fifth, as a strength building exercise, you might attempt to place ALL
> > your code, every line of it, in user defined functions and execute the
> > program by the judicious call of these functions. Of course, you would
> > still need to declare your lexical variables, but you set them by
> > function calls.


> I don't quite understand the last 2.


1. Please note that I may have misled you by suggesting that you call
your functions using &. While you can certainly do that, ommitting the
& and using the () will probably suit you better, at least until you
get a handle on your personal preference and scripting needs.

2. I strongly suggest you understand the last point. What I mean is
that the body of your script contain only comments, variable
declarations, and function calls. Your functionality is abstracted to
your subroutines. This promotes clarity and modularity, and can help
tremendously with debugging. It separates your logic from your
behavior so you can think about your program logic without considering
implementation. When you write your functions, you don't have to think
about your logic.

I frequently find myself writing the logic in pseudo code as comments,
writing subroutines that implement peices of the logic, and adding the
call to the main script using the pseudo comments as documentation.
This isn't a good way to write big applications, but it is a good way
to develop small to moderate size scripts.

CC
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any other testing providers other than Prometric vnapoli Microsoft Certification 0 11-09-2011 09:42 PM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
Why can't I access variable from other subroutine? Fred Perl Misc 5 02-18-2007 09:26 AM
How to access web.sitemap file from other virtual directories other than the current application root folder? hvajja@gmail.com ASP .Net 0 08-07-2006 08:26 PM
What other produces other than "PC Anywhere"? JnlSeb Computer Information 4 01-08-2004 06:12 PM



Advertisments