Re: why does IndexOf still return -1

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Gordon Cowie" <gordy@xxxxxxxxxxxxxxxxxx> wrote in message
news:OHu6GY1UGHA.5900@xxxxxxxxxxxxxxxxxxxxxxx
No because then if you need the index if it is there but it is optional
(a common case) then you must either incur the exception cost or incur
the search cost twice.


I see. Also, from reading other's replies. The real issue is the "cost"
related to exceptions. If they were free I think it would make for a much
cleaner coding style. Probably even anywhere a special return code is
returned.

try
{
x = s.IndexOf('x');
}
catch( Exception e )
{
// how is it clear that this relates to the indexing?
}

v

x = s.IndexOf('x');
if( x < 0 )
{
}
else
{
}

Worse yet you just know that you are going to find a load of code like:

try
{
x = s.IndexOf('x');
}
catch {}

Another negative about -1. It forces IndexOf to return an int, when a uint
would be more appropriate. The max length of the string is effectively cut
in half in order to provide space for the possible -1 result.

uint is not CLSCompliant so it's never going to happen.

If you have strings of over 2GB then indexing will be the least of your
problems.

P.S. In my opinion they should at least have provided

public const int NotFound = -1;

I'm not a fan of magic numbers even for error returns.



.



Relevant Pages

  • Re: why does IndexOf still return -1
    ... common case) then you must either incur the exception cost or incur the ... search cost twice. ... - it would very rarely be appropriate to let an exception bubble up. ... There are various things already constraining the length of a string: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: perf & Try Catch
    ... I'd read that paper and unfortunately the statement "you only incur the cost ... The cost of the exception itself only occurs when it is thrown, ... In most cases the perf impact of these hits are amortized over a much ... >> Most of the perf hit comes from actually throwing the exception, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java Exceptions cause performance hit?
    ... In your comparison the number of iterations ... > compared to the cost of creating and throwing the exception. ... enables.So why does hasNext() exist? ...
    (comp.lang.java.programmer)
  • Re: perf & Try Catch
    ... The cost of the exception itself only occurs when it is ... > try-catch or try-finally gets JITd (the runtime uses this to determine ... >>> small perf hit in the catch block when the exception is caught because ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: when to use component-based design vs. stayign with traditional OOP
    ... IOW, in both cases reuse is enabled by cohesion, encapsulation, and capturing invariants. ... When should a standard OOA/D construction approach be used vs. a component-based construction approach? ... throwing an exception? ... How much testing you do depends on your company's business model (i.e., the trade-off between cost of testing and cost of escapes). ...
    (comp.object)