- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
return secnod; //yes i know, don't frist me
Admin
Aren't the offset and numCharToRead arguments mixed up?
Admin
First one is 100% offshore "debeloper" code. Second one is fairly amusing for the name and comment alone. The code equivalent of "Don't @ me" kek
Admin
Why is s2, an obvious temporary, declared as a class variable? My concurrency spidey-sense is tingling; if this gets used by two threads at the same time, Bad Stuff will happen. Not just any Bad Stuff, but the worst kind, a Heisenbug.
Admin
It won't cause concurrency bugs because it is never modified.
Admin
It looks like the result of an inside job by somebody disguising themselves as an imbecile while introducing a kind of buffer overflow (in the sense of being able to access previously stored data). The level of self-deprecation in both the code as well as comments makes them rather smart.
Admin
I misread numcharstoread as NumCharStoreAd for whatever reason. That might even be a better name since it's not as completely misleading.
Admin
It's not exactly a substring function. If I'm reading this correctly, it's extracting each individual character and returning it as a separate string in an array of strings. I can't even fathom why someone might want to do this. But, as you say, if this method were ever called it would throw a null reference exception, so maybe it's just a floating bit of dead code left in there for your (and our) entertainment.
Admin
The parameters aren't exactly mixed up. Yes,
numcharstoread
really is an offset, but the reverse isn't true. Theoffset
isn't the length to read, but rather the absolute stopping point (one past the end).Assuming aInstance is an instance of a that has s2 correctly initialized,
string[] result = aInstance.mid(11, 7, "Hello, world! Goodbye!");
would return 4 characters (worl), not 11.Admin
So if offset were less than numcharstored, it would crash with an "index out of range" error. You could fix it by adding a comparison with the length of the string or try-catch the error.
Admin
No. If
offset
were less thannumcharestoread
the loop wouldn't execute even once and the function would return the untoucheds2
. Likewise ifoffset
was equal tonumcharestoread
.IndexOutOfRangeException
will be thrown ifoffset
(ornumcharstoread
) is greater thans.Length - 1
On top of everything else, this will also fail if
s
contains any of the Unicode characters that need more than onechar
to represent them.Offshore "work" at it's finest. (misplaced apostrophe intentional).
Admin
Everyday, I wake up in the afternoon and go along until it's time to rise. By this time, I'm usually done with my work.
It's night owl code!
Admin
The second one could be worse:
Admin
And some people say the quality of WTFs here are going down. This is wow. The WTFs have layers.
Admin
OMG this code is a WTF on so many levels.
I'm amazed to see so many WTFs in such a small piece of code. I have a coworker who is a terrible coder, and I don't think even he could have made such a horrible mess.
Admin
I can understand the argument against stateful implementation for something like someone's wonky reinvention of not-quite substring. Or in microservices. Or web apps. But I really don't see the merit in arguing against stateful implementation in a general sense in any ol class.. I saw that a couple of times in this thread. Is that the intention of the argument in those threads, or have I misunderstood? I figured perhaps I misunderstood how generalized the dread was. There are advantages and disadvantages, as always.
Admin
This is top drawer. A real WTF, in that you would actually say those three words when you find it, and it has you scratching your head for a while to even understand what the author was trying to achieve.
If it actually worked (i.e. it initialised s2 to a string of the right length, and you were allowed to assign by index in C# strings) it does
mid("Test string", 9, 6) -> [ "r", "i", "n" ]
What is that ever useful for?
And then of course there's the 'not working' - NullReferenceException, assignment to immutable string index. And then the bonus WTFs: no bounds checking, Engrish comment, unnecessary instance state (which makes it non-thread-safe for no good reason).
Admin
Can't actually see much wrong with this.
Ahaa, I think I've got it. The comment lines are introduced with 3 forward-slash characters when in reality only 2 are needed? Yes indeed, what an appalling waste of computer resources.
Admin
Now, to be fair, what seems to have happened here: this was originally a regular implementation of
substr()
(meaning, return n chars starting at given offset), and later the specs changed (e.g., some other party implemented code, which provided parameters in this weird format) and the code was quickly fixed to adjust to the new requirements, but comments and parameter names stayed the same. (Probably, "Make this work and don't invest too much, just make it work. — And no grumpy comments!")Admin
I love docstrings, that just repeat the signature(or method name).
I don't need to know that String[] is an array, I ALREADY know that. I want to know what it is supposed to CONTAIN, and I won't take "Strings" for an answer.
Writing a proper docstring often uncovers issues with the design before writing any code. Or just outright demonstrates that the idea is nonsense. Here it more looks like "documentation" was a style guideline they didn't agree with.
Admin
By the way, is there some possibility to subscribe to further comments? So far I just use Pushbullet to put the article from iPad to PC and later take another look, but that's quite the crutch. Plus, Pushbullet isn't ven officially on the AppStore anymore, AND it lacks a plausible monetization scheme. The premium plan is too expensive for what it brings to the table, and they generate zero revenue from free users instead of using lightweight ads. Heck, even showing an ad before showing pushed files would be acceptable.
Admin
Offtopic rambling, sorry.
Admin
The problem with stateful classes is that they are prone to thread safety problems - primarily race conditions. Thus in order to use them in any multi-threaded application (which nowadays is practically any application) you need to use them with caution: either never share instances between threads or ensure that any invocations are carefully guarded and synchronized in order to ensure that it always works the way it was "designed" or "intended" regardless of the order that the various threads end up walking through the code (which can also lead to bottlenecks which can hurt the performance of the application).
Because of the problems you run into in multi-threaded applications, stateful implementations have become a bad practice and are generally discouraged. With few exceptions, the benefits are marginal while the drawbacks are significant.
Admin
I propose a consciousness experiment called the Stack Overflow Room. It's like John Searle's Chinese Room, except that the subject is always a human, and the problem is whether he can be said to "learn" to code by pasting from Stack Overflow without understanding any of the English words involved.
Admin
There's a reason that the Java SimpleDateFormat documentation has had a callout about thread-safety since 1.4. Every so often, someone who needs to process date strings inside a method gets the bright idea to save some resources by reusing a single format instance, because surely those smart guys at Sun made it thread-safe internally...
Admin
brb. Adding a function to my php god utility file that aliases 'explode' as 'beSplited'.
Admin
HP's version of dtksh let you write GUI procedures with the shell. I wrote a simple taskbar that kept abending. But it didn't fail when I put in debug echos. The following line fixed the bug (dtksh was built on an old buggy version of ksh):
Admin
@Best of 2021 ref:
and
Not sure what you mean. The code is
Indexing into a System.String and retrieving the Char from that position is normal C#. See https://docs.microsoft.com/en-us/dotnet/api/system.string.chars?view=net-5.0 . So the first line is just fine.
SInce
s2
is an array of strings there's also nothing wrong with the second line. If the array had been initialized to an array of the correct length rather than left as scalar null.What are you seeing?
Admin
All these worries about thread safety is assuming the calling code is keeping one instance of class "a". I fully expect a new instance to be initialised every time someone wants to call "mid", even from the same block of code, given the quality of this code.
Admin
@van Dartel I vote with you. That first one is a concealed memory sniffer.
Admin
@van Dartel I vote with you. That first one is a concealed memory sniffer.
Admin
O, penis sues!
Admin
@van Dartel ref:
and @CoyneTheDup ref:
Not in C# it's not. It's a concealed IndexOutOfRangeException generator.
I'm not 100% certain we can conclude this is C# versus some other C-like language. But it sure looks like it to me.
Admin
Yeah you're right I was blanking out the part where the result is an array of strings, because WTF why would you do that.
Admin
I will admit it... this past week in the midst of frustrating debugging (25 year old codebase) I did use variable "Voo" and "Doo".... might not be so bad, bu tI did commit them to SCCM.
Admin
I feel the concern is geared more towards objects that are not immutable rather than objects that are not stateless, in the general sense regarding threading. I'd find myself hard-pressed to have entirely stateless class design throughout an entire application, I guess? But objects whose state doesn't change, that I can sign up for more often, but still not entirely. Maybe I'm a lost cause :D I appreciate your feedback, though.