Re: Extended break
From: Alex Hunsley (lard_at_tardis.ed.ac.molar.uk)
Date: 12/09/03
- Next message: Devin Panchal: "if and else statements"
- Previous message: Brandon COrbin: "Re: Doing a Text comparison"
- In reply to: Adam: "Re: Extended break"
- Next in thread: Adam: "Re: Extended break"
- Reply: Adam: "Re: Extended break"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 09 Dec 2003 16:24:23 +0000
Adam wrote:
>>>>Heh, true.
>>>>So another approach, nicely using language features:
>>>>make your recursive method throw I_Found_It_Exception.
>>>>Put your most outer call to recursiveMethod it a try-catch block,
>>>>You can just do nothing in the catch block then.
>>>>:)
>>>
>>>
>>>Actually, I'd discourage that kind of use of an exception. Exceptions
>
> are
>
>>>for "bad" things.
>>>
>>> - Virgil
>>
>>Agreed. An exception shouldn't be used here; as Virgil noted, exceptions
>>are for When Things Go Wrong, and not for catching a condition which you
>>expect in normal (correct) running of a program.
>
>
> I'm aware of that, and that's why i've put a smiley there.
> The approach was meant to be a contrary for goto statement.
>
>
>>One of the reasons for
>>this is that Exceptions are expensive to throw; I remember reading one
>>estimate that you waste ten times as much execution time throwing an
>>exception as setting a signalling flag or somesuch other thing (which
>>would suffice).
>
>
> Not always necessary true.
> For a speed-performance tuning
> I've seen using exceptions to end
> a simple loop:
> <code>
> int[] someArray = ...
> try{
> for(int i=0;;++i) someArray[i] = ....
> }catch(IndexOutOfBoundsException e){}
> </code>
> as it was faster not to check i<someArray.length every time.
> (I'm just not sure if it was in java though...)
>
> Adam
I see what you mean...
I think it's careless, though, to rely on an exception when you hit the
end of an array, rather than *knowing* how far you should go. It will
also be more confusing to other programmers seeing the code who are
trying to work out what's going on!
It may function as an optimisation, as you say. Whether it's worth it,
vs. readability/maintainability, is worth examining though.
alex
- Next message: Devin Panchal: "if and else statements"
- Previous message: Brandon COrbin: "Re: Doing a Text comparison"
- In reply to: Adam: "Re: Extended break"
- Next in thread: Adam: "Re: Extended break"
- Reply: Adam: "Re: Extended break"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|