15,749,346 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.

 Re: ChatGPT "solves" a riddle for me. adriancs6-Apr-23 1:26 adriancs 6-Apr-23 1:26
 Re: ChatGPT "solves" a riddle for me. DrWalter PE26-Apr-23 18:38 DrWalter PE 26-Apr-23 18:38
 Re: ChatGPT "solves" a riddle for me. GKP199226-Apr-23 20:26 GKP1992 26-Apr-23 20:26
 Re: ChatGPT "solves" a riddle for me. Amarnath S18-May-23 1:08 Amarnath S 18-May-23 1:08
 How I learned to stop worrying & love The Error raddevus12-Mar-23 12:48 raddevus 12-Mar-23 12:48
 Re: How I learned to stop worrying & love The Error jschell20-Mar-23 7:13 jschell 20-Mar-23 7:13
 Re: How I learned to stop worrying & love The Error DrWalter PE26-Apr-23 18:38 DrWalter PE 26-Apr-23 18:38
 But it feels so _diiirtyyy_! PIEBALDconsult1-Mar-23 8:01 PIEBALDconsult 1-Mar-23 8: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.
 Re: But it feels so _diiirtyyy_! Mircea Neacsu1-Mar-23 8:34 Mircea Neacsu 1-Mar-23 8:34
 Re: But it feels so _diiirtyyy_! PIEBALDconsult1-Mar-23 9:08 PIEBALDconsult 1-Mar-23 9:08
 Re: But it feels so _diiirtyyy_! Mircea Neacsu1-Mar-23 9:22 Mircea Neacsu 1-Mar-23 9:22
 Re: But it feels so _diiirtyyy_! PIEBALDconsult1-Mar-23 9:32 PIEBALDconsult 1-Mar-23 9:32
 Re: But it feels so _diiirtyyy_! jschell20-Mar-23 7:20 jschell 20-Mar-23 7:20
 Re: But it feels so _diiirtyyy_! PIEBALDconsult20-Mar-23 10:04 PIEBALDconsult 20-Mar-23 10:04
 Re: But it feels so _diiirtyyy_! jschell21-Mar-23 5:35 jschell 21-Mar-23 5:35
 Re: But it feels so _diiirtyyy_! PIEBALDconsult21-Mar-23 5:50 PIEBALDconsult 21-Mar-23 5:50
 Re: But it feels so _diiirtyyy_! jschell23-Mar-23 5:55 jschell 23-Mar-23 5:55
 Re: But it feels so _diiirtyyy_! PIEBALDconsult23-Mar-23 6:53 PIEBALDconsult 23-Mar-23 6:53
 ChatGTP: Write me a poem about programming Marc Clifton1-Feb-23 3:00 Marc Clifton 1-Feb-23 3:00
 Re: ChatGTP: Write me a poem about programming Sean Ewington1-Feb-23 3:02 Sean Ewington 1-Feb-23 3:02
 Re: ChatGTP: Write me a poem about programming Slacker00716-Feb-23 1:13 Slacker007 16-Feb-23 1:13
 Re: ChatGTP: Write me a poem about programming Andre Oosthuizen28-Feb-23 2:03 Andre Oosthuizen 28-Feb-23 2:03
 Re: ChatGTP: Write me a poem about programming Daniel Pfeffer1-Feb-23 3:22 Daniel Pfeffer 1-Feb-23 3:22
 Re: ChatGTP: Write me a poem about programming Slacker0071-Feb-23 4:33 Slacker007 1-Feb-23 4:33
 Re: ChatGTP: Write me a poem about programming Daniel Pfeffer1-Feb-23 6:41 Daniel Pfeffer 1-Feb-23 6:41
 Last Visit: 31-Dec-99 18:00     Last Update: 4-Oct-23 17:49 Refresh ᐊ Prev1...3456789101112 Next ᐅ