strategys other than subroutine and OO?

J

John Bokma

Big and Blue said:
I have to read my own, and others, code from 15+ years ago.

I can assure you that in a loop a statement of:

goto DONE_THIS:

and a DONE_THIS label at the end of a loop is much easier to follow
than a last, break or continue.

I think that has also to do with what you're used to. with last, next,
etc. the direction is in the keyword. With goto, you have to pick a clear
label. I see no difference between:

last DOING_THIS;

goto DONE_THIS;
 
B

Ben Morrow

Quoth "szr said:
Just a sort of a semi-tangent question, isn't using next and last with
labels (to, say, break out of a parent loop from an inner loop in a set
of nested loops) implicitly using `goto` ?

Of course. The principle difference, for me, is that next/last/redo
always jump to the top (or right out) of a block, so blocks are always
executed straight through up to the point where you jump out. Even
though it's logically the same (and operationally very slightly slower),
I would prefer

begin_stuff();

FOO: {
do_stuff();
redo FOO if condition();
}

end_stuff();

to

begin_stuff();

FOO:
do_stuff();
goto FOO if condition();

end_stuff();

just because the potentially-repeated section gets its own block.

Ben
 
P

Peter J. Holzer

Of course. The principle difference, for me, is that next/last/redo
always jump to the top (or right out) of a block, so blocks are always
executed straight through up to the point where you jump out. Even
though it's logically the same (and operationally very slightly slower),
I would prefer

begin_stuff();

FOO: {
do_stuff();
redo FOO if condition();
}

end_stuff();

to

begin_stuff();

FOO:
do_stuff();
goto FOO if condition();

end_stuff();

In that particular case I'd prefer:

begin_stuff();

do {
do_stuff();
} while (condition());

end_stuff();

but I agree in general. While it's not "structured programming" in the
strict definition (there is more than one way out of a block) it is
"more structured" than spaghetti code. And personally i find

while (<>) {
some_code();
next unless $x;

more_code();
next unless $y;

even_more_code();
next unless $z;

if_all_goes_well();
}

more readable than

while (<>) {
some_code();
if ($x) {
more_code();
if ($y) {
even_more_code();
if ($z) {
if_all_goes_well();
}
}
}
}
 
S

szr

Big and Blue wrote:
[...]
Agreed, but there can be a difference between:

last;
and
got to DONE_THIS;

I believe the comparison for goto vs. next/last/redo is concerned where
the latters use labels; to me that's where they are most similar to
'goto'.

[...]
a) I would never use tabs (which is what are there in the copy I
received) - they are an abomination, since they can mean different
things to different people, and hence any alignment they produce is
fragile

Indeed, especially in a widely used plain-text medium such as UseNet and
even plain-text email. Tabs can easily differ in size depending on the
platform, editor, the font, and the font size, and it gets even messier
if you compare news readers that use a fixed-width font (I think
majority still do) vs. ones that use a proportional font. At least for
UseNet, spaces should always be used instead of raw tabs.

Many readers (like the one I'm using right now), when composing, will
yields a certain about of spaces, predefined in the preferences, when
you hit the TAB key.
b) as for the code, that's why elsif exists:

while (<>) {
some_code();
if ($x) {
more_code();
}
elsif ($y) {
even_more_code();
}
elsif ($z) {
if_all_goes_well();
}
}

Actually it would have to read like:

while (<>) {
some_code();
if ($x) {
more_code();
}
elsif ($x && $y) {
even_more_code();
}
elsif ($x && $y && $z) {
if_all_goes_well();
}
}

in order to equate to:

while (<>) {
some_code();
if ($x) {
more_code();
if ($y) {
even_more_code();
if ($z) {
if_all_goes_well();
}
}
}
}
 
P

Peter J. Holzer

Actually it would have to read like:

while (<>) {
some_code();
if ($x) {
more_code();
}
elsif ($x && $y) {
even_more_code();
}
elsif ($x && $y && $z) {
if_all_goes_well();
}
}

in order to equate to:

while (<>) {
some_code();
if ($x) {
more_code();
if ($y) {
even_more_code();
if ($z) {
if_all_goes_well();
}
}
}
}

Nope. Anyone up for a third try?

hp
 
W

Willem

Peter wrote:
)> Actually it would have to read like:
)>
)> while (<>) {
)> some_code();
)> if ($x) {
)> more_code();
)> }
)> elsif ($x && $y) {
)> even_more_code();
)> }
)> elsif ($x && $y && $z) {
)> if_all_goes_well();
)> }
)> }
)>
)> in order to equate to:
)>
)> while (<>) {
)> some_code();
)> if ($x) {
)> more_code();
)> if ($y) {
)> even_more_code();
)> if ($z) {
)> if_all_goes_well();
)> }
)> }
)> }
)> }
)>
)
) Nope. Anyone up for a third try?
)
) hp

Assuming more_code and even_more_code have no effect on $y and $z:
(Which is an unwarranted assumption, by the way)

while (<>) {
some_code();
if ($x && not $y) {
more_code();
}
elsif ($x && not $z) {
more_code();
even_more_code();
}
elsif ($x) {
more_code();
even_more_code();
if_all_goes_well();
}
}

Which is quite silly, of course.


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
 
S

szr

Willem said:
Peter wrote:
)> Actually it would have to read like:
)>
)> while (<>) {
)> some_code();
)> if ($x) {
)> more_code();
)> }
)> elsif ($x && $y) {
)> even_more_code();
)> }
)> elsif ($x && $y && $z) {
)> if_all_goes_well();
)> }
)> }

See below...

)> in order to equate to:
)>
)> while (<>) {
)> some_code();
)> if ($x) {
)> more_code();
)> if ($y) {
)> even_more_code();
)> if ($z) {
)> if_all_goes_well();
)> }
)> }
)> }
)> }
)>
)
) Nope. Anyone up for a third try?
)
) hp

Assuming more_code and even_more_code have no effect on $y and $z:
(Which is an unwarranted assumption, by the way)

while (<>) {
some_code();
if ($x && not $y) {
more_code();
}
elsif ($x && not $z) {
more_code();
even_more_code();
}
elsif ($x) {
more_code();
even_more_code();
if_all_goes_well();
}
}

Which is quite silly, of course.

Actually I should of written it as:

while (<>) {
some_code();
if ($x) {
more_code();
}
if ($x && $y) {
even_more_code();
}
if ($x && $y && $z) {
if_all_goes_well();
}
}

That way it falls through giving the same effect as the original and is
possibly a bit easier to read.
 
S

sheinrich

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'm not sure why none of the luminaries were coming forward with
closures, but I'd highly recommend reading up on them.
Of course they _are_ subroutines but IMHO they're offering the most
versatile re-use of programm code.

GOTO
perldoc -q closure

Cheers,
Steffen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top