|
Posted by ccc31807 on March 20, 2008, 10:55 am
Please log in for more thread options
> 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
|
|
Posted by Joost Diepenmaat on March 20, 2008, 11:10 am
Please log in for more thread options
> &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/
|
|
Posted by ccc31807 on March 20, 2008, 11:41 am
Please log in for more thread options > 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
|
|
Posted by Ela on March 20, 2008, 11:30 am
Please log in for more thread options > 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!
|
|
Posted by ccc31807 on March 20, 2008, 11:55 am
Please log in for more thread options > > 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
|
| Similar Threads | Posted | | help me pass argument to the subroutine and then return the value from that subroutine to another. | October 14, 2006, 1:35 am |
| A subroutine for gcd | July 17, 2006, 9:38 am |
| subroutine | July 20, 2006, 5:36 pm |
| "Undefined subroutine" | November 21, 2004, 8:48 pm |
| How to tell if a subroutine arg is a constant | February 24, 2005, 9:13 am |
| Subroutine Function | March 3, 2005, 5:14 pm |
| subroutine explanation | April 22, 2005, 8:40 am |
| problam in subroutine? | December 5, 2005, 1:40 pm |
| calling subroutine | January 3, 2006, 6:35 pm |
| subroutine doesn't seem to run after calling it. | May 9, 2006, 4:04 pm |
|