Click here to Skip to main content
15,846,826 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: ChatGPT "solves" a riddle for me. Pin
adriancs6-Apr-23 2:26
mvaadriancs6-Apr-23 2:26 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
DrWalter PE26-Apr-23 19:38
professionalDrWalter PE26-Apr-23 19:38 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
GKP199226-Apr-23 21:26
professionalGKP199226-Apr-23 21:26 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
Amarnath S18-May-23 2:08
professionalAmarnath S18-May-23 2:08 
GeneralHow I learned to stop worrying & love The Error Pin
raddevus12-Mar-23 13:48
mvaraddevus12-Mar-23 13:48 
GeneralRe: How I learned to stop worrying & love The Error Pin
jschell20-Mar-23 8:13
jschell20-Mar-23 8:13 
GeneralRe: How I learned to stop worrying & love The Error Pin
DrWalter PE26-Apr-23 19:38
professionalDrWalter PE26-Apr-23 19:38 
GeneralBut it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 9:01
mvePIEBALDconsult1-Mar-23 9:01 
So I have this method ( C# , .net ), let's call it F(i) where i is an integer representing a UTF-16 character. The method determines which one of the following classes the character is a member of:
  Control (ASCII control characters)
  Delimiter (the caller can specify which characters are delimiters)
  EOF (-1)
  Escape (\)
  Non-ASCII (i > 127)
  Normal (ASCII characters which are not members of another classes)
  Quote (")

This is implemented as an array look-up with a catch for IndexOutOfRangeException which will fire for EOF and non-ASCII characters. This has been working well for a while. The data (JSON files mostly, but not exclusively) is nearly all ASCII characters with only an occasional non-ASCII character -- maybe a few "smart-quotes" or similar, which are OK, in many cases I replace those with their ASCII versions anyway.

BUT once in a while we receive a corrupt file which (in the latest case) includes a JSON value which contains more than a million non-ASCII characters (in the file they are encoded as three-byte UTF-8).
F(i) was not performing well in this case. Apparently having the catch fire occasionally is OK, but firing a million times in rapid succession is decidedly not.

Once I tracked the issue to F(i), I could try altering it to add a test for i > 127 and avoid the exception (which I am loathe to do on principle). But unit testing did show that it improved the performance considerably for the non-ASCII characters without significantly hindering the performance of ASCII characters (EOF is still handled by a catch).

That sounds like a win, except... I just don't like having the extra test which is essentially needless given that we don't expect any/many non-ASCII characters in most files we receive.

Sooo... I named the original version Fa(i) and the new version Fn(i) and I made F(i) a delegate which starts out pointing to Fa(i) but:
  If Fa(i) encounters a non_ASCII character it will re-point F(i) to Fn(i)
  If Fn(i) encounters an ASCII character it will re-point F(i) to Fa(i)

Slick as snot. Unit testing shows good performance.
  Time required to read the million non-ASCII characters with Fa == 12 seconds
  Time required to read the million non-ASCII characters with Fn == 0.06 seconds

I have integration testing running now. The current production version times out the file read after ten seconds (a protection I had to add a while back for another corrupt file), but with the new version, it should read successfully then I should get an error when trying to stuff more than a million non-ASCII characters into a database column which is defined for 500 ASCII (CP-1252) characters.

In the meantime, the people who send us this file are trying to find out what's causing the issue. So far, it's intermittent (a dozen times in the last four years), so it hasn't become critical.


I'm pretty sure I've done this sort of thing before -- having a delegate which points to one of two slightly different implementations of a method depending on what has been encountered in the data, and flipping back and forth dynamically as required. I guess I'll be code-spelunking this afternoon to review that code.


This is the way.
GeneralRe: But it feels so _diiirtyyy_! Pin
Mircea Neacsu1-Mar-23 9:34
Mircea Neacsu1-Mar-23 9:34 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 10:08
mvePIEBALDconsult1-Mar-23 10:08 
GeneralRe: But it feels so _diiirtyyy_! Pin
Mircea Neacsu1-Mar-23 10:22
Mircea Neacsu1-Mar-23 10:22 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 10:32
mvePIEBALDconsult1-Mar-23 10:32 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell20-Mar-23 8:20
jschell20-Mar-23 8:20 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult20-Mar-23 11:04
mvePIEBALDconsult20-Mar-23 11:04 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell21-Mar-23 6:35
jschell21-Mar-23 6:35 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult21-Mar-23 6:50
mvePIEBALDconsult21-Mar-23 6:50 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell23-Mar-23 6:55
jschell23-Mar-23 6:55 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult23-Mar-23 7:53
mvePIEBALDconsult23-Mar-23 7:53 
GeneralChatGTP: Write me a poem about programming Pin
Marc Clifton1-Feb-23 4:00
mvaMarc Clifton1-Feb-23 4:00 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Sean Ewington1-Feb-23 4:02
staffSean Ewington1-Feb-23 4:02 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Slacker00716-Feb-23 2:13
professionalSlacker00716-Feb-23 2:13 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Andre Oosthuizen28-Feb-23 3:03
mveAndre Oosthuizen28-Feb-23 3:03 
GeneralRe: ChatGTP: Write me a poem about programming PinPopular
Daniel Pfeffer1-Feb-23 4:22
professionalDaniel Pfeffer1-Feb-23 4:22 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Slacker0071-Feb-23 5:33
professionalSlacker0071-Feb-23 5:33 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Daniel Pfeffer1-Feb-23 7:41
professionalDaniel Pfeffer1-Feb-23 7:41 

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.