Click here to Skip to main content
15,890,845 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 0:16
professionalEddy Vluggen18-May-18 0:16 
GeneralRe: Is it my fault? Pin
Jon McKee17-May-18 18:21
professionalJon McKee17-May-18 18:21 
GeneralRe: Is it my fault? Pin
Eddy Vluggen17-May-18 23:57
professionalEddy Vluggen17-May-18 23:57 
GeneralRe: Is it my fault? Pin
Jon McKee18-May-18 10:13
professionalJon McKee18-May-18 10:13 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 13:09
professionalEddy Vluggen18-May-18 13:09 
GeneralRe: Is it my fault? Pin
David A. Gray25-Jun-18 11:19
David A. Gray25-Jun-18 11:19 
GeneralRe: Is it my fault? Pin
PIEBALDconsult16-May-18 17:09
mvePIEBALDconsult16-May-18 17:09 
GeneralRe: Is it my fault? Pin
Jon McKee17-May-18 14:35
professionalJon McKee17-May-18 14:35 
I think you answered my question. Sounds like you have a well-defined set of values so not finding it is a rare occurrence. But what I was getting at is this:
C#
private IDbDataParameter ExistsStuff(IDbCommand CMD, string Name) =>
    (System.Data.IDbDataParameter) CMD.Parameters[Name];

private IDbDataParameter DoesntExistStuff(IDbCommand CMD, string Name)
{
    IDbParameter result = CMD.CreateParameter() { ParameterName = Name };
    CMD.Parameters.Add(result);
    return result;
}
//--------------------------------------
if (CMD.Parameters.Contains(Name))
    result = ExistsStuff(CMD, Name);
else
    result = DoesntExistStuff(CMD, Name);
//Versus
try
{
    result = ExistsStuff(CMD, Name);
}
catch ( System.IndexOutOfRangeException err )
{
    result = DoesntExistStuff(CMD, Name);
}

In order to understand the differences, the statements they share in common were yanked out to normalize things. The main difference is as you said: the if front-loads the cost while the exception back-loads it. My other post[^] shows some code that tested Contains vs exceptions. Both that and another test I ran later using an invalid access to trigger the exception showed that exceptions are about 13 to 17 times as computationally expensive as a check.

Using that information, we can describe the functions mathematically. Shared code will be weighted as a 0 since it's shared (the code inside the if/try and else/catch). The if...else comes in at a weight of 1 for both branches since the check is required for both. The try...catch comes in at a weight of 0 for no exception and 15 for the exception branch. Now we'll use the variables x and y to denote the ratio that each branch is visited. Slap it all together and we get:
if/else: (1x + 1y)
try/catch: (0x + 15y)
Balance point: x + y = 15y => x = 14y => 1:14

This ratio shows that for the two methods to be equivalent x must occur 14 times as much as y. So for this example, 93.3% or more of the calls must generate no exception or you'd be better off with a check.
GeneralRe: Is it my fault? Pin
PIEBALDconsult17-May-18 14:42
mvePIEBALDconsult17-May-18 14:42 
GeneralRe: Is it my fault? Pin
Jon McKee17-May-18 15:16
professionalJon McKee17-May-18 15:16 
GeneralRe: Is it my fault? Pin
PIEBALDconsult21-May-18 14:11
mvePIEBALDconsult21-May-18 14:11 
GeneralRe: Is it my fault? Pin
Jon McKee22-May-18 13:54
professionalJon McKee22-May-18 13:54 
GeneralRe: Is it my fault? Pin
Mark Smeltzer23-May-18 6:08
Mark Smeltzer23-May-18 6:08 
GeneralRe: Is it my fault? Pin
Richard Deeming23-May-18 7:02
mveRichard Deeming23-May-18 7:02 
GeneralRe: Is it my fault? Pin
Mark Smeltzer23-May-18 7:18
Mark Smeltzer23-May-18 7:18 
GeneralRecursion in SQL Pin
dan!sh 14-May-18 1:50
professional dan!sh 14-May-18 1:50 
GeneralRe: Recursion in SQL Pin
Jörgen Andersson14-May-18 3:27
professionalJörgen Andersson14-May-18 3:27 
GeneralRe: Recursion in SQL Pin
Richard Deeming14-May-18 8:50
mveRichard Deeming14-May-18 8:50 
GeneralRe: Recursion in SQL Pin
Super Lloyd14-May-18 16:52
Super Lloyd14-May-18 16:52 
GeneralRe: Recursion in SQL Pin
dan!sh 14-May-18 19:36
professional dan!sh 14-May-18 19:36 
GeneralRe: Recursion in SQL Pin
Super Lloyd14-May-18 21:18
Super Lloyd14-May-18 21:18 
GeneralRe: Recursion in SQL Pin
GuyThiebaut14-May-18 20:45
professionalGuyThiebaut14-May-18 20:45 
GeneralRe: Recursion in SQL Pin
DaveAuld19-May-18 21:36
professionalDaveAuld19-May-18 21:36 
GeneralRe: Recursion in SQL Pin
David A. Gray25-Jun-18 11:26
David A. Gray25-Jun-18 11:26 
GeneralTime Stamps Rendered by CMD.exe Pin
David A. Gray13-May-18 19:43
David A. Gray13-May-18 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.