Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

2 extends

Thread view: 
Irlan agous - 09 Jun 2005 12:38 GMT
Hello i want to extend from 2 classes lik

public class PatientImpl extends DataModel ,UnicastRemoteObject implements
PatientInterface throws RemoteExeption;

But this gives an error message, dfoes anyone knows how to solve this?

Thanks
Andrew Thompson - 09 Jun 2005 13:07 GMT
> Hello i want to extend from 2 classes ...
..
> But this gives an error message, dfoes anyone knows how to solve this?

Try reading the group, rather than simply posting questions to it.
Here is a link to one of the answers given for the very previous
message thread to this group!
<http://groups.google.com.au/group/comp.lang.java.help/msg/755d22bee4b041ae?hl=en>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Bryce - 09 Jun 2005 14:25 GMT
>Hello i want to extend from 2 classes lik
>
>public class PatientImpl extends DataModel ,UnicastRemoteObject implements
>PatientInterface throws RemoteExeption;
>
>But this gives an error message, dfoes anyone knows how to solve this?

No multiple inheritence in Java.

--
now with more cowbell
Thomas G. Marshall - 10 Jun 2005 23:43 GMT
Bryce coughed up:

>> Hello i want to extend from 2 classes lik
>>
[quoted text clipped - 5 lines]
>
> No multiple inheritence in Java.

{cough, cough}....except for interfaces.

Signature

Everythinginlifeisrealative.Apingpongballseemssmalluntilsomeoneramsitupyournose.

Tor Iver Wilhelmsen - 11 Jun 2005 07:00 GMT
> {cough, cough}....except for interfaces.

Or through inner classes and using methods instead of casting.

public class MyClass {

       private MyOtherType myOtherTypePart = new MyOtherType() {
               public void overriddenMethod() {
                       // Should access members of MyClass
                       // otherwise it's pointless
               }
       }

       public MyOtherType asMyOtherType() {
               return myOtherTypePart;
       }

}
Bryce - 20 Jun 2005 18:19 GMT
>Bryce coughed up:
>>
[quoted text clipped - 9 lines]
>
>{cough, cough}....except for interfaces.

Well, That's not technically inheritence...

Java classes inherit other classes
Java classes implement interfaces.

But I get the point, and duly noted

--
now with more cowbell
Thomas G. Marshall - 20 Jun 2005 20:49 GMT
Bryce coughed up:

>> Bryce coughed up:
>>>
[quoted text clipped - 16 lines]
>
> But I get the point, and duly noted

Are you sure?  Take a look at the following:

   interface A { void a(); }
   interface B { void b(); }
   interface C extends A,B { void c(); }

C is a valid interface declaration.  It requires a(), b(), and c().

Signature

"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."

Bryce - 21 Jun 2005 16:31 GMT
>>>> No multiple inheritence in Java.
>>>
[quoted text clipped - 14 lines]
>
>C is a valid interface declaration.  It requires a(), b(), and c().

Gotcha. I was assuming we were extending classes, not interfaces.

--
now with more cowbell
Hal Rosser - 11 Jun 2005 05:31 GMT
Pehaps you may consider  creating an instance variable of the 'other' class.
This approach is preferred in many cases.  Composition is a good alternative
to multiple inheritance, and is preferred by some authors of the subject.
HTH

> Hello i want to extend from 2 classes lik
>
[quoted text clipped - 4 lines]
>
> Thanks
The Wogster - 11 Jun 2005 15:34 GMT
> Hello i want to extend from 2 classes lik
>
> public class PatientImpl extends DataModel ,UnicastRemoteObject implements
> PatientInterface throws RemoteExeption;
>
> But this gives an error message, dfoes anyone knows how to solve this?

There are a few tricks, an Interface is one, an instance of the second
class is another, you could potentially combine both or create a new
class that Interfaces to both, as a child of neither.

However needing multiple inheritance usually means that something in the
initial class design is broken.  Often it's from trying to create a
jack-of-all-classes combining a bunch of what should be unrelated stuff
into a single class.

It's like the goto, it's legal in C, but the only times I have seen it
used, were because someone had programmed themselves into a corner.
Once I rewrote a few lines and got rid of it, the other is still there
AFAIK, because the code was so fragile, nobody was stupid enough to want
to monkey with it, even the lead programmer would not touch it.

W
Thomas G. Marshall - 12 Jun 2005 02:30 GMT
The Wogster coughed up:
>> Hello i want to extend from 2 classes lik
>>
[quoted text clipped - 12 lines]
> a jack-of-all-classes combining a bunch of what should be unrelated
> stuff into a single class.

I agree with this.

> It's like the goto, it's legal in C, but the only times I have seen it
> used, were because someone had programmed themselves into a corner.

{sounds of a brake screech, collision, and sound of wobbling hubcap...}

If by "programmed themselves into a corner" you mean something akin to
"couldn't find another way to solve the problem then to use goto", then your
experience is far different from mine in the last 20 years.

While there certainly have been cases where a goto was used when someone
could not design properly, the vast majority of the times I've seen goto
actually used were to /increase/ the clarity and maintainability of the
code.  Not that they simply got stuck.

The cases I'm thinking of almost always involved a function where the exit
of the function required several things to happen.  The goto was in these
cases a much clearer alternative to otherwise *horrible* contrivances.  In
these cases, to say that this was done only because the author missed a
better non-goto way to design it is to imply that using goto is /always/ to
be avoided, and is simply incorrect.

> Once I rewrote a few lines and got rid of it, the other is still there
> AFAIK, because the code was so fragile, nobody was stupid enough to
> want to monkey with it, even the lead programmer would not touch it.
>
> W

Signature

"Gentlemen, you can't fight in here! This is the War Room!"

The Wogster - 12 Jun 2005 03:18 GMT
> The Wogster coughed up:
>
[quoted text clipped - 25 lines]
> "couldn't find another way to solve the problem then to use goto", then your
> experience is far different from mine in the last 20 years.

I'm just relating to my experiences, in C and C like languages (C++ and
Java) the goto is usually a result of needing a quick solution to a bad
code design.  Last time I saw a goto that was justified it was in
Commodore Basic and that was over 20 years ago.  I've written a lot of
code since then, and never found a need to design something that needed
a goto.

> While there certainly have been cases where a goto was used when someone
> could not design properly, the vast majority of the times I've seen goto
> actually used were to /increase/ the clarity and maintainability of the
> code.  Not that they simply got stuck.

In my experience, needing gotos to clarify and make code maintainable,
means the code wasn't designed properly in the first place.  Procedural
program design theories don't require it, and OOP should have made it
illegal (IMNSHO).  When you have structures like if-elseif-else and case
statements, there are usually better ways of doing it.

> The cases I'm thinking of almost always involved a function where the exit
> of the function required several things to happen.  The goto was in these
> cases a much clearer alternative to otherwise *horrible* contrivances.  In
> these cases, to say that this was done only because the author missed a
> better non-goto way to design it is to imply that using goto is /always/ to
> be avoided, and is simply incorrect.

With languages that have richly designed looping and redirection
constructs, you shouldn't need the goto.  I didn't say it was always to
be avoided, just that when you feel the need to use goto (except maybe
in Gee Whiz Basic), it's usually a pretty good indicator that the code
design is lacking.

W
Thomas G. Marshall - 12 Jun 2005 05:32 GMT
The Wogster coughed up:

>> While there certainly have been cases where a goto was used when
>> someone could not design properly, the vast majority of the times
[quoted text clipped - 3 lines]
> In my experience, needing gotos to clarify and make code maintainable,
> means the code wasn't designed properly in the first place.

This is a common notion from students.  It should /not/ be a notion among
seasoned engineers.

> Procedural program design theories don't require it, and OOP should
> have made it illegal (IMNSHO).

It has nothing whatsoever to do with either discipline specifically, so such
a statement doesn't accomplish much.  {shrug}

> When you have structures like
> if-elseif-else and case statements, there are usually better ways of
> doing it.

"usually".  This doesn't mean that the goto doesn't have significant value.

>> The cases I'm thinking of almost always involved a function where
>> the exit of the function required several things to happen.  The
[quoted text clipped - 9 lines]
> maybe in Gee Whiz Basic), it's usually a pretty good indicator that
> the code design is lacking.

No, this last statement is very much saying that it is "always to be
avoided".  There is no "need", other than a "need" to produce the clearest
possible code, and to use anything in your arsenal to accomplish that.

I can tell you why you'd want to tell a student such a thing.  I've taught
computer programming before in different venues; you tend to want to present
the goto in a dim light (if not ban it outright) with beginners because it
is often the very /first/ thing that a beginner considers.  It's probably
because, to a beginner, control-flow within a program is more easily thought
of in terms of the branching instruction rather than a conditional/looping
construct as a whole.

However, if the mere presence of a goto is enough to make you think that the
design is lacking, then you've simply not allowed yourself to use it
properly, and as a result it's conceivable that some of your design may have
been morphed out of shape in an effort to avoid it.  As long as it is used
with forethought and in a downward motion, the goto can be a fine way to
improve readability and maintainability, parTICULARLY if it builds a clear
section within a function for cleanup.  Demonizing the usage of goto
accomplishes nothing.  In fact, out of hand demonization of goto is
something you'll most often observe among junior engineers.

Signature

Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....

The Wogster - 12 Jun 2005 15:22 GMT
> The Wogster coughed up:
>
[quoted text clipped - 8 lines]
> This is a common notion from students.  It should /not/ be a notion among
> seasoned engineers.

I've been working in this stuff for nearly a quarter century, and in
that time, I have seen a whole bunch of languages (FORTRAN, COBOL, HYPO,
JCL, BASIC, VisualBasic (a BASIC that wants to be Pascal when it grows
up :), Pascal, Assembler (I bet few people here have actually seen
370/Assembler, worked with 6502 and Z80 Assemblers myself), C, C++ and
Currently studying Java - which is why I am here.

>>Procedural program design theories don't require it, and OOP should
>>have made it illegal (IMNSHO).
>
> It has nothing whatsoever to do with either discipline specifically, so such
> a statement doesn't accomplish much.  {shrug}

I maintain that when you "need" goto, it's usually in a function or
method that is trying to do too many things, and that breaking it down
into smaller pieces will actually lead to a cleaner, more maintainable
construct that doesn't need the goto.

>>When you have structures like
>>if-elseif-else and case statements, there are usually better ways of
>>doing it.
>
> "usually".  This doesn't mean that the goto doesn't have significant value.

>>>The cases I'm thinking of almost always involved a function where
>>>the exit of the function required several things to happen.  The
[quoted text clipped - 3 lines]
>>>to imply that using goto is /always/ to be avoided, and is simply
>>>incorrect.

"The exit of the function required several things to happen", this
sounds like a case of a function trying to do too much, and breaking it
down into separate functions, to do each thing, might work better
without needing goto everywhere.  Functions (and to an even larger
extent classes) should be treated as a black box that does something,
and does it well.  This makes it much easier to write a library or
package of reusable code.  Reusable code is worth 10 times what new code
is, since it's already written, tested and debugged.

>>With languages that have richly designed looping and redirection
>>constructs, you shouldn't need the goto.  I didn't say it was always
[quoted text clipped - 5 lines]
> avoided".  There is no "need", other than a "need" to produce the clearest
> possible code, and to use anything in your arsenal to accomplish that.

I'll grant you the second part of that statement, you need the clearest
possible code, but I have yet to see code with gotos sprinkled
throughout, that didn't need a lot of comments to explain what it was
trying to do.  I also have yet to see such code that had those comments
in it, unless some poor sod (me more then once), had to take the time to
figure it out, and added the comments later on.  I have seen where the
whole mess was commented out, because someone rewrote it, in a clearer,
more consise, manner.  More then once, I have been the someone who
rewrote it.

> I can tell you why you'd want to tell a student such a thing.  I've taught
> computer programming before in different venues; you tend to want to present
[quoted text clipped - 3 lines]
> of in terms of the branching instruction rather than a conditional/looping
> construct as a whole.

Goto is often presented in a dim light, because it's easy to overuse it,
 I once saw a 700 line program (Commodore BASIC) that contained around
400 gotos, it was cheaper and faster to rewrite the whole program, then
to figure out, what in Hades the original writer was trying to do, or
what he had been smoking when he wrote it.

W

> However, if the mere presence of a goto is enough to make you think that the
> design is lacking, then you've simply not allowed yourself to use it
[quoted text clipped - 5 lines]
> accomplishes nothing.  In fact, out of hand demonization of goto is
> something you'll most often observe among junior engineers.
Patricia Shanahan - 12 Jun 2005 21:27 GMT
...

> I'll grant you the second part of that statement, you need the clearest
> possible code, but I have yet to see code with gotos sprinkled
> throughout, that didn't need a lot of comments to explain what it was
> trying to do.

Do you feel the same way about code that has, say, one goto per hundred
thousand lines of code?

Patricia
Thomas G. Marshall - 12 Jun 2005 22:01 GMT
Patricia Shanahan coughed up:
> ...
>>
[quoted text clipped - 5 lines]
> Do you feel the same way about code that has, say, one goto per
> hundred thousand lines of code?

I am attempting to determine where his line is drawn as well.  Had I read
this post first, I would have responded here instead.

Signature

"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."

The Wogster - 12 Jun 2005 22:51 GMT
> ...
>
[quoted text clipped - 5 lines]
> Do you feel the same way about code that has, say, one goto per hundred
> thousand lines of code?

It depends on why the goto is there in the first place.  Commonly goto's
are used by two groups of people:

1) Students who don't know better, and usually end up with code that has
more in common with pasta then with well designed code.  It's usually a
horrible mess, that someone else ends up rewriting, because it's faster
to do that, then to try and figure out the code.

2) Old timers who learned the goto back in the days of COBOL, FORTRAN
AND BASIC, where it was often the only option, and can't get their
brains around the newer concepts, in more modern languages.  These are
often the guys who did the flowchart after the code was done.  They also
often write code that has the minimum number of comments allowed by
employer specifications.

W
Patricia Shanahan - 13 Jun 2005 02:24 GMT
>> ...
>>
[quoted text clipped - 8 lines]
> It depends on why the goto is there in the first place.  Commonly goto's
> are used by two groups of people:

"Why the goto is there" is indeed the really important question. For
about the last 30 years I've used a consistent set of rules for coding
goto. Before that, I was young, wild, too clever for my own good, didn't
know any better, and produced some spaghetti code.

1. Goto should only be used to emulate a well-organized control
structure, not spaghetti.

2. The emulated control structure must be a significantly better
solution to the current problem than any higher level structure
supported in the language.

Using those rules, I write a lot of gotos in assembly language, but the
code can still be indented to show nested loop and if-then-else
organization. On the other hand, I don't miss goto in Java because it
has all the control structures I've needed. In between, I've used goto
in C, to emulate a finally clause or a multi-level break.

Whether I'll ever use goto in a language depends on the set of control
structures the language supports in procedures, orthogonal to whether it
is O-O or not.

Patricia
Thomas G. Marshall - 12 Jun 2005 21:59 GMT
The Wogster coughed up:
>> The Wogster coughed up:

...[rip]...

[regarding goto]...
>>> Procedural program design theories don't require it, and OOP should
>>> have made it illegal (IMNSHO).
[quoted text clipped - 6 lines]
> into smaller pieces will actually lead to a cleaner, more maintainable
> construct that doesn't need the goto.

A very common use for goto is an exit out of a nested loop in a language
without multilevel or labeled breaks.  Would you accept or not accept that
as a perfectly reasonable usage of goto?

It is still not at all clear where you wish to draw the line, particularly
with the statement you made:

       "The Wogster":
       Procedural program design theories don't
       require it, and OOP should have made it
       illegal (IMNSHO).

Here's what's wrong with it:

1. That procedural or any other program design theories don't require
something is certainly of no issue.  Once you have a while() construct, you
do not need the for(;;) loop, and once you have if/else, you do not need the
switch.  Would you hence argue against the for() and switch?  It's a
pointless statement.

2. The concepts of OO and procedural programming are entirely orthogonal to
whether or not a language contains a goto.  It is applicable throughout both
paradigms, and has no specific impact to either.

Again, the /out-of-hand/ demonizing of goto is thus far without merit.
Demonizing the misuse of goto is certainly with merit, as is demonizing the
misuse of anything, but that is an entirely different exercise than what you
seem to be attempting.

Now don't get me wrong here: I /prefer/ the restricted goto within java
(downward only "labeled" or "named" break) to the unrestricted versions
found in C/C++, because I like the way the code looks as a result.  But when
you have a language without such a downward goto, then using an unrestricted
goto in a careful downward fashion is perfectly fine, and railing against
its usage is outright silly.

Signature

"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."

The Wogster - 12 Jun 2005 23:03 GMT
> The Wogster coughed up:
>
[quoted text clipped - 18 lines]
> without multilevel or labeled breaks.  Would you accept or not accept that
> as a perfectly reasonable usage of goto?

That would be acceptable, however this is a Java group, and Java does
have those capabilities, does it not?  Besides that the building of a
nested loop that needs you to bail from the very inside, all the way to
the outside, is often a design that is far too complicated in the first
place.

> It is still not at all clear where you wish to draw the line, particularly
> with the statement you made:
[quoted text clipped - 27 lines]
> goto in a careful downward fashion is perfectly fine, and railing against
> its usage is outright silly.

Whatever go ahead, I don't really care if you want to write lots of
gotos, just hope I never have to maintain that code.....

W
Thomas G. Marshall - 13 Jun 2005 03:52 GMT
The Wogster coughed up:
>> The Wogster coughed up:
>>
[quoted text clipped - 58 lines]
> Whatever go ahead, I don't really care if you want to write lots of
> gotos, just hope I never have to maintain that code.....

I never said I wanted to write "lots of goto's".  And if I put them in
there, then it would be to improve maintainability.

Signature

With knowledge comes sorrow.

The Wogster - 13 Jun 2005 14:00 GMT
> The Wogster coughed up:
>
[quoted text clipped - 63 lines]
> I never said I wanted to write "lots of goto's".  And if I put them in
> there, then it would be to improve maintainability.

As I put in another message, I have written a lot of code, and worked
with considerably more, still haven't found an instance where goto made
things more maintainable.  Every goto I have seen, since my days in
assembler, was as a result of someone trying to make sense of code that
was poorly (if at all) designed.

We may need to agree to disagree on this one, though, were arguing in
circles now....

W
Thomas G. Marshall - 13 Jun 2005 20:39 GMT
The Wogster coughed up:
>> The Wogster coughed up:

...[rip]...

>>> Whatever go ahead, I don't really care if you want to write lots of
>>> gotos, just hope I never have to maintain that code.....
[quoted text clipped - 10 lines]
> We may need to agree to disagree on this one, though, were arguing in
> circles now....

Probably a good idea, and you should understand that I do not wish to be
contentious nor offend.  But I still am struggling to understand your
position.  IYO, let's forget the "what I've seen is", and let's concentrate
on "what bothers me", if it's ok with you, to establish your stake in the
sand.

Take a look at the following in a goto-enabled language:

       for (int z = 0; z < 10; z++)
           for (int y = 0; y < 10; y++)
               for (int x = 0; x < 10; x++)
                   if (array[z][y][x] == somethingNeeded)
                       goto hop_out;
   hop_out:
       (continue on)

Is /this/ something that bothers your sensibilities?

Now let's look at this.  Note, this is purposefully not ideal---I'm
searching for lesser-quality scenarios.  And note that the "status" is
computed and maintained for proper return---I'm limiting the detail here on
purpose as well:

       Send UDP/IP initial contact datagram...
       If (abortNak)
           goto finish_up;

       (UDP transactions here)
       if (errorCondition)        // or whatever
           goto finish_up;

       (UDP transactions here)
       if (errorCondition or abortNak)    // or whatever
           goto finish_up;

       (UDP transactions)
       (etc)

   finish_up:
       Notify database;
       Send UDP trailing datagram...
       return status;

Are you at odds here as well?

Signature

I've seen this a few times--Don't make this mistake:

Dwight: "This thing is wildly available."
Smedly: "Did you mean wildly, or /widely/ ?"
Dwight: "Both!", said while nodding emphatically.

Dwight was exposed to have made a grammatical
error and tries to cover it up by thinking
fast.  This is so painfully obvious that he
only succeeds in looking worse.

The Wogster - 14 Jun 2005 15:03 GMT
> The Wogster coughed up:
>
[quoted text clipped - 34 lines]
>
> Is /this/ something that bothers your sensibilities?

Yes, but more that it's a horrible construct, then that it contains a
goto.

Here is another option, that does the same thing, but is, IMNSHO clearer;

bool onlyFirst = true; // only find first somethingNeeded

for (int z = 0; z < 10 && onlyFirst == true; z++)
 for (int y = 0; y < 10 && onlyFirst == true; y++)
  for (int x = 0; x < 10 && onlyFirst == true; x++)
   if (array[z][y][x] == somethingNeeded)
   {
    onlyFirst == false;
    // do other stuff
   }

Where the goto becomes a problem, is when do other stuff is 30 lines, so
that the goto is a couple of screens later, and someone not realizing
that the for gets usurped later on, adds code between the "for (int z"
and the "for (int y" that counts on z getting to 10.  Of course later
when the code doesn't work the programmer who did it, is unavailable,
and you spend a nice, warm sunny Sunday afternoon, trying to figure out
what happened, instead of tossing a ball around with your kids, before
putting some steaks on the barbie....

My version indicates that there are other conditions, and that z may not
get to 10, and then the programmer would go looking to find out what
happens to onlyFirst.

> Now let's look at this.  Note, this is purposefully not ideal---I'm
> searching for lesser-quality scenarios.  And note that the "status" is
[quoted text clipped - 22 lines]
>
> Are you at odds here as well?

That is possibly the worst piece of code I have ever seen, and I have
seen some awful stuff in my years.  It's an off the cuff example, so
I'll grant you that, how about this version though:

// send initial datagram

while(getData()); //  sets abortNak and errorCondition
{
    if(!abortNak && !errorCondition)
    {
        // UDP transactions etc
    }
    // Notify database;
}

// Send Trailing datagram
// return status

Here is another version of just the if statement:

if(abortNak)
{
   // we could do nothing here.
}
elseif (errorCondition)
{
   // we could optionally add logging code here
}
else
{
   // UDP transactions
}

// Notify database;
// Send Trailing datagram
// return status

W
Thomas G. Marshall - 14 Jun 2005 19:11 GMT
The Wogster coughed up:

...[rip]...

>> Take a look at the following in a goto-enabled language:
>>
[quoted text clipped - 23 lines]
> // do other stuff
>    }

Ok, good.  now there is a stake in the sand.  That you have an issue with
the UDP example of mine is fine for the moment, because I was purposefully
establishing something [far] less ideal.  I supplied two examples of varying
complexity and worth and you've precisely placed your position on this.
Thank you for that, now we're /somewhere/.

I think that the nested loop you provide is not only slower (which in this
example is hardly an issue), but a harder to maintain obfuscated mess.  Back
in my C days, had one of my guys attempted this, he would have received a
quick conversation from me.

You've taken 3 loops, each of which contained only a single conditional for
the engineer to parse through, and added to each one an additional
conditional that must be parsed and evaluated /by the engineer/ in order to
make sure that any change made to the body of the loop doesn't explode the
loop itself around his ears.

Your following statement of:

       The Wogster:
       Where the goto becomes a problem, is when do
       other stuff is 30 lines, so that the goto is a couple
       of screens later, and someone not realizing that
       the for gets usurped later on, adds code between
       the "for (int z"and the "for (int y" that counts on z
       getting to 10.

is incredibly broken logic.  If the "do other stuff" were 30 lines, then
you'd *PARTICULARLY* not want the loop you describe.  It would require the
engineer to wade through 30 lines of code to make sure that the onlyFisrt
boolean was not altered within it.  Furthermore, any change to those 30
lines 4 weeks later by another engineer holds the possibility of breaking
the loop itself even if he's very senior, but hopped on caffeine at 3 am.
You would have dragged the logic of the loop itself into 30 additional
lines, when the loop should have instead merely been just that: a nested
loop containing just the logic needed to form the loop.

I believe that your not-so humble opinion (as we all have from time to time)
is /predicated/ on the notion that goto is bad.  Predicated and assumed.
From there you drew further conclusions, rather than weigh the issue at hand
and discover what was truly clearest.

...[rip]...

Signature

"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides

The Wogster - 15 Jun 2005 03:32 GMT
> The Wogster coughed up:
>
[quoted text clipped - 38 lines]
> in my C days, had one of my guys attempted this, he would have received a
> quick conversation from me.

See below

> You've taken 3 loops, each of which contained only a single conditional for
> the engineer to parse through, and added to each one an additional
[quoted text clipped - 21 lines]
> lines, when the loop should have instead merely been just that: a nested
> loop containing just the logic needed to form the loop.

With the goto, the engineer needs to look for something, they may not
even realize exists.  My solution, indicates that the loop has TWO
conditions, which it does, so does yours, but it may not be clear that
there are two conditions, the for only has one condition, even though
the loop has two.  That can be dangerous, when the engineer is debugging
code at 3am, knowing that the application takes 6 hours, and needs to be
finish running by 7.

The flag, indicates that there IS two conditions, so a quick search will
find the second condition.

I would like to continue, but don't have more time to spend on this.....

W

> I believe that your not-so humble opinion (as we all have from time to time)
> is /predicated/ on the notion that goto is bad.  Predicated and assumed.
> From there you drew further conclusions, rather than weigh the issue at hand
> and discover what was truly clearest.
>
> ...[rip]...
Thomas G. Marshall - 15 Jun 2005 04:52 GMT
The Wogster coughed up:
>> The Wogster coughed up:
>>
[quoted text clipped - 78 lines]
> The flag, indicates that there IS two conditions, so a quick search
> will find the second condition.

Now you're grasping at straws.

In my case, each nested for() has a single conditional making 3 visible to
the engineer.  Add to this a conditional test within the 3 layer loop for
exit.  There is nothing hidden here at all.

In your case, each for() has 2 conditionals making 6.  Add to this the same
conditional test I have in the middle, but place the code within the body of
the loop where it is possible to truly muck up the looping logic.

> I would like to continue, but don't have more time to spend on
> this.....

I too am getting tired.  It's not that we disagree that is so tiring, but I
just cannot even get /close/ to understanding your logic in this regard, try
as I might to spell the examples out.

You don't think so of course, but I still suspect that if you really thought
it through honestly that you'd realize that much of your knee-jerk dismissal
of the goto is only based upon itself and not on evidence.  That you loathed
it first, and then looked for and found the reasons why.

But in any case, we're certainly going nowhere fast.

Thanks for the discussion.

...[rip]...

Signature

Framsticks.  3D Artificial Life evolution.  You can see the creatures
that evolve and how they interact, hunt, swim, etc. (Unaffiliated with
me). http://www.frams.alife.pl/

The Wogster - 15 Jun 2005 13:22 GMT
> The Wogster coughed up:
>
[quoted text clipped - 90 lines]
> conditional test I have in the middle, but place the code within the body of
> the loop where it is possible to truly muck up the looping logic.

Actually there are just as many conditions with your loop structure,
however it's ONLY clear if you either document it (unlikely), or the
code is a small snippet like in the example.  However if there is a lot
of code, there is nothing in the code to connect the goto to the loop,
so here is where it starts looking more difficult:

for (int z = 0; z < 10; z++)
   for (int y = 0; y < 10; y++)
      for (int x = 0; x < 10; x++)
         if (array[z][y][x] == somethingNeeded)
         {
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
// more lines of code
            goto hop_out;
            }
hop_out:

Yes, this code is legal (C and C++ at least), even though it's confusing
, because you may not remember (a year later) that the goto is connected
to any of the loops.  The flag version, may contain more conditionals,
however you know that the loop is connected to it, even though the
number of comments is the same.  In other words the flag documents
itself.  You know that another condition is connected to the loop.

You know as well as I do, that most C programmers would rather have
themselves boiled in oil, have all of their fingernails pulled out, and
be keel-hauled in acid then to voluntarily write the combination /* or
// any more often then required by employer standards.

Another possible way to write this is this way:

bool onlyFirst = true;
for (int z = 0; z < 10; z++)
{
   for (int y = 0; y < 10; y++)
   {
      for (int x = 0; x < 10; x++)
      {
         if (array[z][y][x] == somethingNeeded && onlyFirst )
         {
            onlyFirst = false;
            // do stuff
         }
      } // for (int x
   } // for (int y
} // for (int z

In this case, you simply let the loops "deadhead" to completion, knowing
that the innermost condition will not trigger a second time, because the
condition can not be met the subsequent times.  Of course you probably
don't like this either, it wastes some time running the loops, but in a
day when most processors are 1GHz and higher, that doesn't really matter
much.

However it still documents itself, and you can't get tripped up, by a
loop that does not complete.  Even though the braces are not needed, I
usually put them in, along with the decorations, only because it makes
the code far easier to read, especially a year later when you can't
remember what you did, and the } is over a screen length away from the
related for.

W

>>I would like to continue, but don't have more time to spend on
>>this.....
[quoted text clipped - 11 lines]
>
> Thanks for the discussion.
Thomas G. Marshall - 15 Jun 2005 17:54 GMT
The Wogster coughed up:

...[rip]...

>> In my case, each nested for() has a single conditional making 3
>> visible to the engineer.  Add to this a conditional test within the
[quoted text clipped - 6 lines]
>
> Actually there are just as many conditions

Nope.  Only if you count the compound conditional as a single entity.

I'm regarding

  for (int x = 0; x < 10; x++)

as easier to read than checking your *two* items, as you put it here:

   for (int x = 0; x < 10 && onlyFirst==true; x++)

> with your loop structure,
> however it's ONLY clear if you either document it (unlikely), or the
[quoted text clipped - 34 lines]
> // more lines of code
> // more lines of code

You forgot 3 :)
// more lines of code
// more lines of code
// more lines of code

>             goto hop_out;
>             }
> hop_out:

This is hardly what I was advocating!  *If* we're going to spell out the 30
lines of code, then this is what it *should* look like.

   for (int z = 0; z < 10; z++)
       for (int y = 0; y < 10; y++)
           for (int x = 0; x < 10; x++)
               if (array[z][y][x] == somethingNeeded)
                   goto found;

   //Not found handling here.

found:
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code
   > // more lines of code

In this way,  the *entirety* of the loop is visible, and it is accomplishing
precisely one thing: the search.  What happens as a *result* of *finding*
the first item is handled afterward, as it should be.

Keep those 30 lines the heck out of the loop, so that the tired engineer at
3am can see it easily and not @#$% up the boolean you added to each of the 3
nested checks.

And Keep those 30 lines the heck out of the loop because they are not being
done more than once anyway, as the bodies of loops are most often seen to
do.

...[rip]...

Signature

"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."

The Wogster - 15 Jun 2005 19:27 GMT
> The Wogster coughed up:
>
[quoted text clipped - 123 lines]
> done more than once anyway, as the bodies of loops are most often seen to
> do.

So what we really end up with is something like this:

 for (int z = 0; z < 10; z++)
    for (int y = 0; y < 10; y++)
       for (int x = 0; x < 10; x++)
          if (array[z][y][x] == somethingNeeded)
             goto found;

     //Not found handling here.
 goto skipfound:
 found:
      // more lines of code
.
. <skip>
.
      // more lines of code
skipfound:
     // more code that gets executed regardless.

So with 6 lines of logic (not including the labels, and other code) we
have 2 goto statements, your coming up a batch of spaghetti here....  I
prefer noodles, with a nice alfredo sauce, and some chicken mixed in, on
a plate, not on my screen.

Worse still, 3 years from now, when they need the if to iterate over
each occurance of something needed, and not just the first one, you need
to do a rewrite, or add even more gotos.....  Where as the switching of
the flag can simply be commented out, which is why I came up with my
second version, something you never did comment on.....

Oh well, I need to go....

W
Thomas G. Marshall - 16 Jun 2005 02:19 GMT
The Wogster coughed up:
>> The Wogster coughed up:
>>
[quoted text clipped - 142 lines]
> skipfound:
>      // more code that gets executed regardless.

Interesting technique: you've taken my example and added a goto to it in
order to make it look like it's overusing goto.  This is a different example
where you have three pertinent sections:

   found after searching and executed
   non found after searching and executed
   executed either way after searching

which has little to do with my example of two sections, something you still
didn't like and even opted for that extra flag check in each for() level and
placing the code executed once the thing was found /into/ the middle of the
loop, which as I pointed out opens you up for maintainability problems.

If you absolutely *must* honor all three possibilities in the same function,
then I would probably /then/ use a boolean for use /after/ the goto, but not
within the loop conditionals for obfuscation, and I certainly wouldn't have
the main logic appear within the loop like you favor. (again, this is an
arbitrary goto-enabled language):

   boolean found = false;

   for (int z = 0; z < 10; z++)
       for (int y = 0; y < 10; y++)
           for (int x = 0; x < 10; x++)
               if (array[z][y][x] == somethingNeeded)
               {
                   found = true;
                   goto hopped_out;
               }

hopped_out:

   if (found)
   {
       foundcode;
       foundcode;
       foundcode;
   }
   else // (!found)
   {
       notfoundcode;
       notfoundcode;
       notfoundcode;
   }

   bothcode;
   bothcode;
   bothcode;

...[rip]...

> Worse still, 3 years from now, when they need the if to iterate over
> each occurance of something needed, and not just the first one, you
> need to do a rewrite, or add even more gotos.....  Where as the
> switching of the flag can simply be commented out, which is why I
> came up with my second version, something you never did comment
> on.....

I'm specifically commenting on things as close to one at a time as I can for
focus.  This thread is out of hand as it is.  In fact, too far so.  If you
insist, then here is your example:

       bool onlyFirst = true;
       for (int z = 0; z < 10; z++)
       {
           for (int y = 0; y < 10; y++)
           {
              for (int x = 0; x < 10; x++)
              {
                 if (array[z][y][x] == somethingNeeded && onlyFirst )
                 {
                    onlyFirst = false;
                    // do stuff
                 }
              } // for (int x
           } // for (int y
       } // for (int z

You certainly can't be serious!  Making loops run to completion
unnecessarily?  What if the array is enlarged significantly?  What if this
were done in multiple threads on top of all that?

There is such a thing as over-optimization, but terminating a loop when the
looping is no longer needed is certainly not one of them!

We really are going nowhere.

...[rip]...

Signature

Sometimes life just sucks and then you live.

The Wogster - 16 Jun 2005 03:04 GMT
> We really are going nowhere.

Agreed, thread ended.

W
Virgil Green - 16 Jun 2005 19:07 GMT
>> We really are going nowhere.
>
> Agreed, thread ended.
>
> W

34 messages in the thread (now 35...). Who wins the pool?

Signature

Virgil

The Wogster - 16 Jun 2005 19:30 GMT
>>>We really are going nowhere.
>>
[quoted text clipped - 3 lines]
>
> 34 messages in the thread (now 35...). Who wins the pool?

Having done additional thinking on this, the thread may have, to a large
degree, proved my point, when you absolutely need a goto, then your
design needs to be revisited.

Everyone who studies C, C++, Java, Basic or any other language that
contains a for statement, knows it's intended for fixed-length loops, so
perhaps the ideal is to use a different looping construct, such as a
while or do-while, loop-until or whatever else the language offers.

Then again looking for a specific value in a 3 dimensional array, should
probably be taking into account the other dimensions.  Most searches on
multi-dimensional arrays, are only searching within one of the
dimensions, not all of the dimensions.  Efficiency is not the name of
the game when the value your looking for in a 1000 x 1000 x 1000
dimensioned array is array[999][999][999]....

W
Thomas G. Marshall - 17 Jun 2005 22:19 GMT
The Wogster coughed up:

>>>> We really are going nowhere.
>>>
[quoted text clipped - 6 lines]
> Having done additional thinking on this, the thread may have, to a
> large degree, proved my point

No it didn't, and not to any degree.  And attempting to revive this thread
as an attempt to claim false victory and throw in additional points is
hardly an intelligent idea.

Signature

Having a dog that is a purebred does not qualify it for breeding.  Dogs
need to have several generations of clearances for various illnesses
before being bred.  If you are breeding dogs without taking care as to
the genetic quality of the dog (again, being purebred is *not* enough),
you are what is known as a "backyard breeder" and are part of the
problem.  Most of the congenital problems of present day dogs are
traceable directly to backyard breeding.  Spay or neuter your pet
responsibly, and don't just think that you're somehow the exception and
can breed a dog without taking the care described.

Dale King - 13 Jun 2005 02:40 GMT
>> The Wogster coughed up:
>>
>>> It's like the goto, it's legal in C, but the only times I have seen it
>>> used, were because someone had programmed themselves into a corner.

In C there were some cases where it was necessary/mad the code clearer.
Most of those cases in Java have other constructs that eliminate the
need for an arbitrary goto. Exceptions, labelled break and continue
eliminate most of the cases for goto from C. But since C doesn't have
those you are forced to use goto or over complicate the code.

> With languages that have richly designed looping and redirection
> constructs, you shouldn't need the goto.  I didn't say it was always to
> be avoided, just that when you feel the need to use goto (except maybe
> in Gee Whiz Basic), it's usually a pretty good indicator that the code
> design is lacking.

That design as you said could be the design of the language and not
necessarily the design of the program. Or are you one of those anti-goto
that also disagrees with goto-like constructs like break, continue, and
exceptions?
Signature

 Dale King

Thomas G. Marshall - 13 Jun 2005 03:57 GMT
Dale King coughed up:

>>> The Wogster coughed up:
>>>
[quoted text clipped - 18 lines]
> anti-goto that also disagrees with goto-like constructs like break,
> continue, and exceptions?

There are a sizeable number of people that would accept all of goto's
abilities and foibles, so long as it were not called "goto".

;)

Signature

With knowledge comes sorrow.

The Wogster - 13 Jun 2005 13:43 GMT
>>> The Wogster coughed up:
>>>
[quoted text clipped - 17 lines]
> that also disagrees with goto-like constructs like break, continue, and
> exceptions?

Uh no, because those leave control with the language.  Most people who
defend the goto so vehemently,are, with modern languages like Java,
simply are using it to emulate something else available within the language.

Let's see, in the last 10 years, I have written about 1,000,000 lines of
C code, the number of actual goto's in there, none.  During that time, I
viewed probably 40,000,000 more lines, which contained maybe 20 gotos.
Shows how little goto is actually needed.  Of those 5, were code that
was complex trying to do something simple, usually the reason I was
looking at it, was because it wasn't working (poor design).  Five more
were instances where you had the jack-of-all-procedures, where the
programmer had written a bunch of procedures in one block of code (again
poor design).  They used goto to make the thing work.  The remaining
ones, were places where the programmer had written themselves into a
corner, and under pressure of getting the thing to work, used goto to
bail out (again poor design).

W
iamfractal@hotmail.com - 13 Jun 2005 10:40 GMT
> The Wogster coughed up:

<Snwwip.>

> The cases I'm thinking of almost always involved a function where the exit
> of the function required several things to happen.  The goto was in these
> cases a much clearer alternative to otherwise *horrible* contrivances.  In
> these cases, to say that this was done only because the author missed a
> better non-goto way to design it is to imply that using goto is /always/ to
> be avoided, and is simply incorrect.

Hi, Thomas!

Could you provide this sample of code, or an equivalent?

Just curious,

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition.
Thomas G. Marshall - 13 Jun 2005 20:42 GMT
iamfractal@hotmail.com coughed up:
>> The Wogster coughed up:
>
[quoted text clipped - 17 lines]
>
> www.EdmundKirwan.com - Home of The Fractal Class Composition.

Sure---I think that the examples I pondered in my latest post to The Wogster
should be ok.

Signature

I've seen this a few times--Don't make this mistake:

Dwight: "This thing is wildly available."
Smedly: "Did you mean wildly, or /widely/ ?"
Dwight: "Both!", said while nodding emphatically.

Dwight was exposed to have made a grammatical
error and tries to cover it up by thinking
fast.  This is so painfully obvious that he
only succeeds in looking worse.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.