The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,308 other followers

Funny Delphi code of the day: on literals

Posted by jpluimers on 2013/12/09

The fact that the [WayBack] 31-character limit on ClientDataSet field and index names is worse, but I just encountered this GetParamNameWODog function in a Delphi 3rd party library:

What’s wrong with using the Length function here?

Yes, SQL Parameter names will probably less than 1000 characters, but then the 1000 literal should be a constant with a meaningful name, and the '@' literal should be too.

Apparently, Dog is a meaningful name for the at-sign, as it is not the name for a SQL parameter prefix, but it is not a named const either in the rest of their code:

–jeroen

12 Responses to “Funny Delphi code of the day: on literals”

  1. The question is not what’s wrong with the length function, but why MaxInt is not used as the length.

    • jpluimers said

      That’s because, for various reasons, the maximum string length isn’t guaranteed to be MaxInt for all types of strings: http://docwiki.embarcadero.com/RADStudio/XE5/en/String_Types_(Delphi)

      • There aren’t any cases where it’s greater than MaxInt, is it? The Copy() routine will clip the value to Length() anyway. Since Copy() is going to check length no matter what, I always pass in MaxInt if I want the rest of the string.

        • jpluimers said

          So you rather depend on an implementation dependency than doing this little extra effort of making it right?

          I’ve bitten by too many implementation dependencies in the past, so I don’t rely on those any more.

      • Anthony Frazier said

        It’s not an implementation dependency, it’s a documented behavior.

        From: http://docwiki.embarcadero.com/Libraries/XE5/en/System.Copy

        “If Count specifies more characters or array elements than are available, only the characters or elements from S[Index] to the end of S are returned.”

        There’s also…

        “If Index is larger than the length of S, Copy returns an empty string or array.”

        Copy is a very safe thing to call with all kinds of parameters.

        • jpluimers said

          It is documented behavior for the current version. Just like the string length was 255 for Delphi < 2 and strings were single byte for Delphi < 2009. The behavior comes from processor and/or memory architecture. Those change over time and have caused lots of issues. It is really simple to prepare for those and developers should as the cost will be much lower than finding the bugs and fixing them afterwards.

    • Actually you can omit the 3rd parameter on Copy which means “til the end”.

      P.S. WTF is _string?

  2. ain said

    “Apparently, Dog is a meaningful name for the at-sign”

    The code is probably written by russian as AFAIK in russian they call at-sign “sobatska” which means “doggy”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: