Click here to Skip to main content
15,890,123 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: Isomorphic Git Pin
GenJerDan16-May-18 19:41
GenJerDan16-May-18 19:41 
GeneralRe: Isomorphic Git Pin
Marc Clifton17-May-18 3:38
mvaMarc Clifton17-May-18 3:38 
GeneralRe: Isomorphic Git Pin
Super Lloyd18-May-18 20:12
Super Lloyd18-May-18 20:12 
GeneralRe: Isomorphic Git Pin
megaadam21-May-18 5:41
professionalmegaadam21-May-18 5:41 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 6:16
Super Lloyd21-May-18 6:16 
GeneralRe: Isomorphic Git Pin
Richard Deeming21-May-18 7:23
mveRichard Deeming21-May-18 7:23 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 7:47
Super Lloyd21-May-18 7:47 
GeneralIs it my fault? Pin
PIEBALDconsult14-May-18 14:31
mvePIEBALDconsult14-May-18 14:31 
I do a lot with ADO.net, and I use parameters as much as I can. Recently I was working on this method:

C#
private static System.Data.IDbDataParameter
GetParameter
(
  this System.Data.IDbCommand CMD
,
  string                      Name
)
{
  System.Data.IDbDataParameter result ;

  if ( CMD.Parameters.Contains ( Name ) )
  {
    result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
  }
  else
  {
    result = CMD.CreateParameter() ;

    result.ParameterName = Name ;

    CMD.Parameters.Add ( result ) ;
  }

  return ( result ) ;
}


It gets a parameter by name or creates one with the provided name if there isn't one already.
After a while I decided to change it to avoid the call to Contains (the getter and Add have to check for existence anyway) and use try/catch to detect the absence of the parameter:

C#
private static System.Data.IDbDataParameter
GetParameter
(
  this System.Data.IDbCommand CMD
,
  string                      Name
)
{
  System.Data.IDbDataParameter result ;

  try
  {
    result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
  }
  catch ( System.IndexOutOfRangeException err )
  {
    result = CMD.CreateParameter() ;

    result.ParameterName = Name ;

    CMD.Parameters.Add ( result ) ;
  }

  return ( result ) ;
}


And it was good.

Until today.

And Oracle.

Oracle.DataAccess.Client.OracleParameterCollection returns NULL when the parameter doesn't exist! Mad | :mad: Mad | :mad: Mad | :mad:

I had to revert back to the earlier code. Sigh | :sigh:


Edit: After a little more experimentation, I find that other ADO.net Providers I have handy also differ from Microsoft's lead -- four vendors, four outcomes. But, then again, MSDN doesn't say what should happen in this situation!

Anyway, I have since decided that the whole concept of the above code was a bad idea -- I know whether or not the parameter exists and I should act accordingly.
In any case, I rarely have to get a parameter by name -- it's a code smell.

Here, then, is an improved GetParameter (by Name), with no trying to Create and Add missing parameters -- I don't intend to call this when I know the parameter doesn't exist.
When a parameter doesn't exist, it is consistent in its behavior, so the caller can react effectively.

C#
private static System.InvalidOperationException
    GetParameterException
    (
      string           Name
    ,
      System.Exception Exception
    )
    {
      Exception = new System.InvalidOperationException 
      ( 
        "A Parameter with ParameterName '" + Name + "' is not contained by this ParameterCollection." 
      , 
        Exception 
      ) ;

      Exception.Data [ "ParameterName" ] = Name ;

      return ( (System.InvalidOperationException) Exception ) ;
    }
 
    public static System.Data.IDbDataParameter
    GetParameter
    (
      this System.Data.IDbCommand CMD
    ,
      string                      Name
    )
    {
      object result ;
 
      try
      {
        result = CMD.Parameters [ Name ] ;
      }
      /* Microsoft's Providers                     throw  System.IndexOutOfRangeException    */
      /* FirebirdSql.Data.FirebirdClient.FbCommand throws System.ArgumentOutOfRangeException */
      /* Devart.Data.Oracle.OracleCommand          throws System.ArgumentException           */
      catch ( System.Exception err )
      {
        throw ( GetParameterException ( Name , err ) ) ;
      }
 
      /* Oracle.DataAccess.Client.OracleCommand returns NULL */
      if ( result == null )
      {
        throw ( GetParameterException ( Name , null ) ) ;
      }
      
      return ( (System.Data.IDbDataParameter) result ) ;
    }



<rhetorical>
Why do Microsoft's implementations throw System.IndexOutOfRangeException ?
Do they simply iterate the collection until they find the item or hit the end? OMG | :OMG:
</rhetorical>


Edit 2 :
Ca.Ingres.Client.IngresCommand throws System.ArgumentOutOfRangeException
InterSystems.Data.CacheClient.CacheCommand throws System.IndexOutOfRangeException
MySql.Data.MySqlClient.MySqlCommand throws System.ArgumentException

modified 17-May-18 1:56am.

GeneralRe: Is it my fault? Pin
Super Lloyd14-May-18 16:51
Super Lloyd14-May-18 16:51 
GeneralRe: Is it my fault? Pin
Jon McKee14-May-18 18:17
professionalJon McKee14-May-18 18:17 
GeneralRe: Is it my fault? Pin
Eddy Vluggen15-May-18 1:01
professionalEddy Vluggen15-May-18 1:01 
GeneralRe: Is it my fault? Pin
Jon McKee15-May-18 8:36
professionalJon McKee15-May-18 8:36 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 0:46
professionalEddy Vluggen16-May-18 0:46 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 1:21
professionalF-ES Sitecore16-May-18 1:21 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 1:39
professionalEddy Vluggen16-May-18 1:39 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 2:33
professionalF-ES Sitecore16-May-18 2:33 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 3:03
professionalEddy Vluggen16-May-18 3:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 3:28
professionalF-ES Sitecore16-May-18 3:28 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 3:56
professionalEddy Vluggen16-May-18 3:56 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 4:09
professionalF-ES Sitecore16-May-18 4:09 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 10:35
professionalEddy Vluggen16-May-18 10:35 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 22:27
professionalF-ES Sitecore16-May-18 22:27 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 0:03
professionalEddy Vluggen18-May-18 0:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore18-May-18 0:10
professionalF-ES Sitecore18-May-18 0:10 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 0:16
professionalEddy Vluggen18-May-18 0:16 

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.