Click here to Skip to main content

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, embarrasing 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: What was I thinking PinmemberRob Grainger1-Feb-13 4:21 
GeneralRe: What was I thinking PinmemberTomz_KV17-Jan-13 3:58 
RantHappy new year... kind of... Pinmemberhoernchenmeister15-Jan-13 23:03 
GeneralRe: Happy new year... kind of... PinmemberPIEBALDconsult16-Jan-13 4:50 
GeneralRe: Happy new year... kind of... Pinmemberhoernchenmeister16-Jan-13 6:06 
GeneralRe: Happy new year... kind of... PinmemberMatthys Terblanche16-Jan-13 22:08 
GeneralRe: Happy new year... kind of... PinmemberGary Huck17-Jan-13 4:41 
GeneralRe: Happy new year... kind of... PinmvpDave Kreskowiak17-Jan-13 6:26 
GeneralRe: Happy new year... kind of... PinmemberJörgen Andersson17-Jan-13 10:04 
GeneralRe: Happy new year... kind of... Pinmemberpeterchen28-Jan-13 23:05 
GeneralRe: Happy new year... kind of... Pinmemberhoernchenmeister28-Jan-13 23:49 
JokeRe: Happy new year... kind of... PinmemberVUnreal8-Feb-13 11:27 
GeneralJust got an error from VS2012 PinmemberBrisingr Aerowing15-Jan-13 9:48 
GeneralGetting it wrong badly PinmemberPIEBALDconsult9-Jan-13 15:12 
<context>SQL Server 2008 R2</context>
 
Someone added a function to my database! Well OK, it's a team effort so that's fine, but it's incorrect and not well-written.
 
What I think the purpose is is to take a column name in PascalCase and make it look more presentable by adding a SPACE before each capital (uppercase) letter. That's what I understand from the name and it's a reasonable thing to do and it does that.
Buuut... what it really does is add a SPACE before each non-lowercase letter (e.g. digits, symbols, whitespace). D'Oh! | :doh:
 
Aside from producing unexpected output it's also hard to read and difficult to maintain -- I opted to rewrite it rather than try to fix it. Here's the original:
 
CREATE FUNCTION [dbo].[SpaceBeforeCap]
(
 @str nvarchar(max)
)
returns nvarchar(max)
as
begin
 
declare @i int, @j int
declare @returnval nvarchar(max)
set @returnval = ''
select @i = 1, @j = len(@str)
 
declare @w nvarchar(max)
 
while @i <= @j
begin
 if substring(@str,@i,1) = UPPER(substring(@str,@i,1)) collate Latin1_General_CS_AS
 begin
  if @w is not null
  set @returnval = @returnval + ' ' + @w
  set @w = substring(@str,@i,1)
 end
 else
  set @w = @w + substring(@str,@i,1)
 set @i = @i + 1
end
if @w is not null
 set @returnval = @returnval + ' ' + @w
 
return ltrim(@returnval)
 
end
 

And here's mine (I added the fn wart because that's the standard I inherited Sigh | :sigh: ):
 
CREATE FUNCTION [dbo].[fnSpaceBeforeCap]
( @str NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
  DECLARE @offset INTEGER
  SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )
 
  WHILE ( @offset > 0 )
  BEGIN
    SET @str = STUFF ( @str , @offset + 1 , 0 , ' ' )
    SET @offset = PATINDEX ( '%[^ ][A-Z]%' , @str COLLATE Latin1_General_BIN )
  END
 
  RETURN @str
END
 
It uses PATINDEX to find a non-SPACE followed by an uppercase letter then STUFFs a SPACE between them.
I'd like to say that mine is more efficient, but I don't feel like doing any testing. What I will point out is that the original uses SUBSTRING (twice!) to test each character and SUBSTRING to form the new value, whereas mine has that kind of thing hidden in black boxes. What I don't like about mine is that PATINDEX doesn't allow a parameter to tell it where to start so it always starts over from the beginning -- see also Schlemiel the painter's Algorithm[^].
GeneralRe: Getting it wrong badly PinmemberSentenryu10-Jan-13 7:31 
GeneralRe: Getting it wrong badly PinmemberPIEBALDconsult10-Jan-13 8:42 
GeneralRe: Getting it wrong badly PinmemberSentenryu10-Jan-13 23:31 
GeneralRe: Getting it wrong badly PinprotectorAspDotNetDev10-Jan-13 8:37 
GeneralRe: Getting it wrong badly PinmemberPIEBALDconsult10-Jan-13 8:52 
GeneralRe: Getting it wrong badly PinprotectorAspDotNetDev10-Jan-13 9:11 
GeneralFacepalm Moment PinmemberZac Greve7-Jan-13 9:47 
GeneralRe: Facepalm Moment PinmemberPJ Arends7-Jan-13 10:03 
GeneralRe: Facepalm Moment PinmemberZac Greve7-Jan-13 10:06 
GeneralRe: Facepalm Moment Pinmemberpeterchen7-Jan-13 11:03 
GeneralRe: Facepalm Moment PinmemberZac Greve7-Jan-13 11:12 
GeneralRe: Facepalm Moment PinmvpDave Kreskowiak7-Jan-13 10:15 
GeneralRe: Facepalm Moment PinmemberZac Greve7-Jan-13 10:30 
GeneralRe: Facepalm Moment PinmemberBobJanova8-Jan-13 0:29 
GeneralRe: Facepalm Moment PinmemberPIEBALDconsult7-Jan-13 10:17 
GeneralRe: Facepalm Moment Pinmemberlewax007-Jan-13 11:52 
GeneralRe: Facepalm Moment PinmemberJörgen Andersson7-Jan-13 11:05 
GeneralRe: Facepalm Moment PinprotectorPete O'Hanlon7-Jan-13 11:17 
GeneralRe: Facepalm Moment PinmemberZac Greve7-Jan-13 11:20 
GeneralRe: Facepalm Moment PinmemberRick Sparks7-Jan-13 15:17 
GeneralMessage Other user using CodeProject PinmemberSuvabrata Roy26-Dec-12 2:59 
GeneralRe: Message Other user using CodeProject PinmemberGeekforChrist26-Dec-12 3:21 
GeneralRe: Message Other user using CodeProject PinmemberSuvabrata Roy26-Dec-12 3:35 
GeneralRe: Message Other user using CodeProject PinmvpOriginalGriff26-Dec-12 4:40 
GeneralRe: Message Other user using CodeProject PinmemberGeekforChrist26-Dec-12 8:45 
GeneralRe: Message Other user using CodeProject PinmvpOriginalGriff26-Dec-12 9:53 
GeneralRe: Message Other user using CodeProject PinmemberGeekforChrist26-Dec-12 10:09 
GeneralRe: Message Other user using CodeProject [modified] PinmemberApril Fans26-Dec-12 17:51 
GeneralRe: Message Other user using CodeProject PinmemberSuvabrata Roy26-Dec-12 20:10 
GeneralRe: Message Other user using CodeProject PinmemberGeekforChrist27-Dec-12 3:33 
GeneralRe: Message Other user using CodeProject PinmemberSuvabrata Roy27-Dec-12 21:42 
GeneralRe: Message Other user using CodeProject PinmvpOriginalGriff27-Dec-12 23:00 
GeneralRe: Message Other user using CodeProject PinmemberSuvabrata Roy27-Dec-12 23:13 
GeneralRe: Message Other user using CodeProject PinmvpOriginalGriff27-Dec-12 23:23 
GeneralRe: Message Other user using CodeProject PinmemberSuvabrata Roy28-Dec-12 0:22 
GeneralRe: Message Other user using CodeProject PinmemberJan Steyn9-Jan-13 22:48 

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

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


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 28 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid