Click here to get back home

strategys other than subroutine and OO?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
strategys other than subroutine and OO? Ela 03-20-2008
Posted by Ela on March 20, 2008, 7:34 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.



Posted by RedGrittyBrick on March 20, 2008, 8:07 am
Please log in for more thread options
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

Posted by Abigail on March 20, 2008, 10:40 am
Please log in for more thread options
_
RedGrittyBrick (RedGrittyBrick@SpamWeary.foo) wrote on VCCCXV September
::
:: "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.


Dijkstra also wrote:

"Please don't fall into the trap of believing that I am terribly
dogmatic about [the goto statement]. I have the uncomfortable
feeling that others are making a religion out of it, as if the
conceptual problems of programming could be solved by a simple
trick, by a simple form of coding disciple!"

Edsger W. Dijkstra, personal communication, 1973.
Quoted by Donald E. Knuth, "Structured Programming with the go to
Statement", 1974.


What Knuth and Dijkstra are saying is that it's not so much the use of
"goto" that should be avoided, the real horror is unstructured programming.
But most people only know the title of Dijkstra's paper, and not the
content [1], let alone know what others have to say about it.


I'm not afraid to confess I do use goto. I prefer:

again:
my $result = do_something;
goto again if it_failed;

over

my $result;
my $success = 0;
until ($success) {
$result = do_something;
$success = 1 unless it_failed;
}


[1] This little, innocent, article was intended to explain a phenomenon
I thought most of us knew. Instead, it evoked a violent storm that
raged for more than a decade. This was the last thing I had expected,
and I still don't quite understand the violence of the emotions,
which have blurred the vision of many: when my "Notes on Structured
Programming" appeared a few years later, many believed that that
was primarily about avoiding of eliminating GOTO statements.
I have also been blamed for its title, which became a template
for many titles of the form "X considered harmful" (among them even
"Dijkstra considered harmful"), but this blame is misplaced. I
had submitted my article under the name "A case against the GOTO
statement", but thee editor I mailed it to, Dr. Niklaus Wirth,
wanted to speed up its publication, which he could do by turning
it into a Letter-to-the-Editor; in the process he gave it the title
that became a template.

Edsger W. Dijkstra about "Go To Statements Considered Harmful",
October 1993, in Phillip Laplante (ed): "Great Papers in Computer
Science", 1996.


Abigail
--
perl -wle'print"Κυστ αξοτθες Πεςμ Θαγλες"^"\x80"x24'

Posted by RedGrittyBrick on March 20, 2008, 12:12 pm
Please log in for more thread options
Abigail wrote:
> _
> RedGrittyBrick wrote:
> ::
<snip>
> ::
> :: 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.
>
> Dijkstra also wrote:
>
> "Please don't fall into the trap of believing that I am terribly
> dogmatic about [the goto statement]. I have the uncomfortable
> feeling that others are making a religion out of it, as if the
> conceptual problems of programming could be solved by a simple
> trick, by a simple form of coding disciple!"
>
> Edsger W. Dijkstra, personal communication, 1973.
> Quoted by Donald E. Knuth, "Structured Programming with the go to
> Statement", 1974.
>
> What Knuth and Dijkstra are saying is that it's not so much the use of
> "goto" that should be avoided, the real horror is unstructured programming.
> But most people only know the title of Dijkstra's paper, and not the
> content [1], let alone know what others have to say about it.

Guilty as charged. Perhaps I am unlucky in that almost all the examples
of GOTO I have had to wrestle with, have been in examples of "spaghetti
programming". It is certainly possible to write spaghetti code without
using GOTO, but use of GOTO seems to encourage this trend in many
programmers.

>
>
> I'm not afraid to confess I do use goto. I prefer:
>
> again:
> my $result = do_something;
> goto again if it_failed;
>
> over
>
> my $result;
> my $success = 0;
> until ($success) {
> $result = do_something;
> $success = 1 unless it_failed;
> }

OK. I don't prefer the former, but I see your point.

However, in this case do_something is three hundred lines, I hope you'd
consider putting it into a subroutine do_something() - or perhaps
re-factoring into several subroutines. The OP is looking for an
"alternative strategy" that doesn't involve subroutines. The OP appears
to be hoping that using GOTO will avoid the perceived difficulty of
reorganising code into well-formed subroutines or objects and methods.

I infer the OP prefers

...
<stuff>
...
<perhaps hundreds of lines of intervening code>
...
<similar stuff>
...

to be transformed into something like

...
my second_time = 0;
stuff:
<stuff probably containing many IFs / GOTOs for dissimilar bits>
if (second_time) goto continuation
...
<perhaps hundreds of lines of intervening code>
...
second_time = 1;
goto stuff:
continuation:
...

instead of

...
stuff("foo", ...);
...
<perhaps hundreds of lines of intervening code>
...
stuff("bar", ...);
...

sub stuff {
<various stuff>
}

I still feel that re-factoring unwieldy and repetitive code into
subroutines (or objects and methods) is a more useful skill to learn
than how to sprinkle GOTO statements into that unwieldy code.

--
RGB

Posted by Frank Seitz on March 20, 2008, 2:38 pm
Please log in for more thread options
RedGrittyBrick wrote:
> Abigail wrote:
>>
>>What Knuth and Dijkstra are saying is that it's not so much the use of
>>"goto" that should be avoided, the real horror is unstructured programming.
>>But most people only know the title of Dijkstra's paper, and not the
>>content [1], let alone know what others have to say about it.
>
> Guilty as charged. Perhaps I am unlucky in that almost all the examples
> of GOTO I have had to wrestle with, have been in examples of "spaghetti
> programming". It is certainly possible to write spaghetti code without
> using GOTO, but use of GOTO seems to encourage this trend in many
> programmers.

Who the hell uses goto today???

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen fόr Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Similar ThreadsPosted
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

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap