Click here to Skip to main content
11,575,595 members (64,598 online)

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 Pin
Rob Grainger1-Feb-13 3:21
memberRob Grainger1-Feb-13 3:21 
GeneralRe: What was I thinking Pin
Tomz_KV17-Jan-13 2:58
memberTomz_KV17-Jan-13 2:58 
RantHappy new year... kind of... Pin
hoernchenmeister15-Jan-13 22:03
memberhoernchenmeister15-Jan-13 22:03 
GeneralRe: Happy new year... kind of... Pin
PIEBALDconsult16-Jan-13 3:50
memberPIEBALDconsult16-Jan-13 3:50 
GeneralRe: Happy new year... kind of... Pin
hoernchenmeister16-Jan-13 5:06
memberhoernchenmeister16-Jan-13 5:06 
GeneralRe: Happy new year... kind of... Pin
Matthys Terblanche16-Jan-13 21:08
memberMatthys Terblanche16-Jan-13 21:08 
GeneralRe: Happy new year... kind of... Pin
Gary Huck17-Jan-13 3:41
memberGary Huck17-Jan-13 3:41 
GeneralRe: Happy new year... kind of... Pin
Dave Kreskowiak17-Jan-13 5:26
mvpDave Kreskowiak17-Jan-13 5:26 
GeneralRe: Happy new year... kind of... Pin
Jörgen Andersson17-Jan-13 9:04
memberJörgen Andersson17-Jan-13 9:04 
GeneralRe: Happy new year... kind of... Pin
peterchen28-Jan-13 22:05
memberpeterchen28-Jan-13 22:05 
GeneralRe: Happy new year... kind of... Pin
hoernchenmeister28-Jan-13 22:49
memberhoernchenmeister28-Jan-13 22:49 
JokeRe: Happy new year... kind of... Pin
VUnreal8-Feb-13 10:27
memberVUnreal8-Feb-13 10:27 
GeneralJust got an error from VS2012 Pin
Brisingr Aerowing15-Jan-13 8:48
memberBrisingr Aerowing15-Jan-13 8:48 
GeneralGetting it wrong badly Pin
PIEBALDconsult9-Jan-13 14:12
memberPIEBALDconsult9-Jan-13 14: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 Pin
Sentenryu10-Jan-13 6:31
memberSentenryu10-Jan-13 6:31 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 7:42
memberPIEBALDconsult10-Jan-13 7:42 
GeneralRe: Getting it wrong badly Pin
Sentenryu10-Jan-13 22:31
memberSentenryu10-Jan-13 22:31 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 7:37
protectorAspDotNetDev10-Jan-13 7:37 
GeneralRe: Getting it wrong badly Pin
PIEBALDconsult10-Jan-13 7:52
memberPIEBALDconsult10-Jan-13 7:52 
GeneralRe: Getting it wrong badly Pin
AspDotNetDev10-Jan-13 8:11
protectorAspDotNetDev10-Jan-13 8:11 
GeneralFacepalm Moment Pin
Zac Greve7-Jan-13 8:47
memberZac Greve7-Jan-13 8:47 
GeneralRe: Facepalm Moment Pin
PJ Arends7-Jan-13 9:03
memberPJ Arends7-Jan-13 9:03 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 9:06
memberZac Greve7-Jan-13 9:06 
GeneralRe: Facepalm Moment Pin
peterchen7-Jan-13 10:03
memberpeterchen7-Jan-13 10:03 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 10:12
memberZac Greve7-Jan-13 10:12 
GeneralRe: Facepalm Moment Pin
Dave Kreskowiak7-Jan-13 9:15
mvpDave Kreskowiak7-Jan-13 9:15 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 9:30
memberZac Greve7-Jan-13 9:30 
GeneralRe: Facepalm Moment Pin
BobJanova7-Jan-13 23:29
memberBobJanova7-Jan-13 23:29 
GeneralRe: Facepalm Moment Pin
PIEBALDconsult7-Jan-13 9:17
memberPIEBALDconsult7-Jan-13 9:17 
GeneralRe: Facepalm Moment Pin
lewax007-Jan-13 10:52
memberlewax007-Jan-13 10:52 
GeneralRe: Facepalm Moment Pin
Jörgen Andersson7-Jan-13 10:05
memberJörgen Andersson7-Jan-13 10:05 
GeneralRe: Facepalm Moment Pin
Pete O'Hanlon7-Jan-13 10:17
protectorPete O'Hanlon7-Jan-13 10:17 
GeneralRe: Facepalm Moment Pin
Zac Greve7-Jan-13 10:20
memberZac Greve7-Jan-13 10:20 
GeneralRe: Facepalm Moment Pin
Rick Sparks7-Jan-13 14:17
memberRick Sparks7-Jan-13 14:17 
GeneralMessage Other user using CodeProject Pin
Suvabrata Roy26-Dec-12 1:59
memberSuvabrata Roy26-Dec-12 1:59 
GeneralRe: Message Other user using CodeProject Pin
GeekforChrist26-Dec-12 2:21
memberGeekforChrist26-Dec-12 2:21 
GeneralRe: Message Other user using CodeProject Pin
Suvabrata Roy26-Dec-12 2:35
memberSuvabrata Roy26-Dec-12 2:35 
GeneralRe: Message Other user using CodeProject Pin
OriginalGriff26-Dec-12 3:40
mvpOriginalGriff26-Dec-12 3:40 
GeneralRe: Message Other user using CodeProject Pin
GeekforChrist26-Dec-12 7:45
memberGeekforChrist26-Dec-12 7:45 
GeneralRe: Message Other user using CodeProject Pin
OriginalGriff26-Dec-12 8:53
mvpOriginalGriff26-Dec-12 8:53 
GeneralRe: Message Other user using CodeProject Pin
GeekforChrist26-Dec-12 9:09
memberGeekforChrist26-Dec-12 9:09 
GeneralRe: Message Other user using CodeProject Pin
April Fans26-Dec-12 16:51
memberApril Fans26-Dec-12 16:51 
GeneralRe: Message Other user using CodeProject Pin
Suvabrata Roy26-Dec-12 19:10
memberSuvabrata Roy26-Dec-12 19:10 
GeneralRe: Message Other user using CodeProject Pin
GeekforChrist27-Dec-12 2:33
memberGeekforChrist27-Dec-12 2:33 
GeneralRe: Message Other user using CodeProject Pin
Suvabrata Roy27-Dec-12 20:42
memberSuvabrata Roy27-Dec-12 20:42 
GeneralRe: Message Other user using CodeProject Pin
OriginalGriff27-Dec-12 22:00
mvpOriginalGriff27-Dec-12 22:00 
GeneralRe: Message Other user using CodeProject Pin
Suvabrata Roy27-Dec-12 22:13
memberSuvabrata Roy27-Dec-12 22:13 
GeneralRe: Message Other user using CodeProject Pin
OriginalGriff27-Dec-12 22:23
mvpOriginalGriff27-Dec-12 22:23 
GeneralRe: Message Other user using CodeProject Pin
Suvabrata Roy27-Dec-12 23:22
memberSuvabrata Roy27-Dec-12 23:22 
GeneralRe: Message Other user using CodeProject Pin
Jan Steyn9-Jan-13 21:48
memberJan Steyn9-Jan-13 21: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
Web03 | 2.8.150624.2 | Last Updated 3 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid