- 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
I don’t think your conclusion is correct. They have commas and only remove the last, superfluous comma from the list of days, not all of them as you state.
Admin
A variable named "$StrDay" next to a variable named "$DayStr": https://www.menofthewest.net/the-he-needed-killin-defense/
Admin
Lemme guess: TRWTF is not so much MTWTF as the abbreviation for "Wendsday" ...
Admin
To me it looks like only the last comma is stripped, the
substr
is after theforeach
Admin
@huppenzuppen - yeah, but who expects anyone to actually look at the code....
Admin
Clearly they don't have any weak-end courses.
Admin
... and that completely rips the heart out of the meat of the TDWTF.
Admin
Exactly - this is super common. Add a comma after each entry, remove the last comma when the loop finishes so you have a string like "Mon,Wed,Thu" instead of "Mon,Wed,Thu,".
It's embarrassing that this got called a WTF.
Admin
I love to use the explode and implode functions, they have such cool names. Changing the $Days array and then imploding it would have been better than concatenating strings and removing the last comma.
Admin
No WTF here, it just strips the last comma... (Well, admittedly, using PHP for anything is a WTF)
Admin
What's the WFT? The poorly coded string mangling, or the "insurance" code? If I was Kerin I'd start searching for other "insurance" such as URL parameters to unlink/delete/etc. the site, assuming it's a website.
Admin
Today TRWTF is TDWTF, as the code strips only the last comma. Nice teasing for the "vulnerability by MMORPG blog" though…
Admin
Typo: "hard coaded"
Admin
As others have said, the removal of the last character is completely fine, given what is being done.
There's plenty still wrong here - the comma-separated string in the database is a proper WTF, and the "for case" pattern in the code shown is definitely a smell (why not make a dictionary whose keys are the short abbreviations and values are the longer ones, then no switch required). But the code isn't truly WTF worthy, only the commentary is.
And obviously PHP is TRWTF.
Admin
The code should only be stripping off the final comma, but it's immediately after the switch, inside the for loop, so it will be called for each iteration which does mean all the commas will be removed.
TRWTF is the abbreviation "Wen" for "Wensday".
Admin
"Clearly they don't have any weak-end courses." As a friend of mine used to say, "I only work here on weekdays, and today is one of my weaker days."
Admin
Yep - a lot of comments here took a quick look and saw the intent of the pattern, but it looks like whoever wrote the code put that line in the wrong place, and hence stripped out all the commas indeed. And this is why unit tests (or any tests, really) are important.
Admin
I don't get it; the code strips the last comma added of a list separated by commas. Granted, there's a build-in method to to actually does all of this (implode), but the code would actually return the correct string "Mon,Tue" instead of "Mon,Tue,".
Admin
For what it's worth; the comments here are confusing because early readers saw a version of the code that only trimmed the last comma. Then the code in the article was edited, the line that does the comma trimming was moved.
Admin
Unless the code has been changed since it was first posted, I think TRWTF is the number of posters here who can't correctly read rather simple code and hence fundamentally misunderstand what this trivial snippet is doing. Then confidently post of their misunderstanding.
This happens a lot on this site. Heck, I've probably done it at least once. But that doesn't make it respectable. It' just one more meta WTF in a pot-calling-kettle-black sort of way. in other words, maybe it's WTFs all the way down.
Admin
The code has been changed, the
substr
was definitely after the foreach closing bracket.Admin
OK this is even more interesting. If the code was copied and pasted straight from the submission, that means the author didn't understand the code, the editor didn't catch the mistake either, then the article went up and to make it look like a WTF as described, the editor changed the code.
On the other hand, if the code was somehow pasted incorrectly, it raises the question - how does one copy and paste code in a way which flips two lines?
We have a real mystery here, haha
Addendum 2022-05-24 09:44: PS. Or the code wasn't changed, and all the commenters misread the code, due to how similarly it looks to what they did a million times themselves.
PPS. To be fair, even if the substr was inside the loop, it's just a silly typo bug; not truly a WTF. They were obviously trying to do what others described - a delimited string join.
Admin
Hey at least they took care of Tuesday and Thursday, more or less.
Admin
The reality is that I frequently re-frame and edit the code to handle anonymization, and I fuck up sometimes.
Admin
Every time there's a PHP submission, someone says it's the real WTF, but the language has its own use case that it's pretty good at.
Don't see too many people coding web sites in C++, for example.
Admin
" this is why unit tests (or any tests, really) are important" Why bother with testing? Our customers will test the software for us for free once we deploy to production.
Admin
Oh, yeah you are right, now it's in the iteration. Or maybe I misread it in the first place :-) Another good reason to just use implode ...
Admin
Ah I see. The "remove last character" line was definitely outside the loop when I posted earlier - and other comments from before then back this up.
So, if what's there now is the actually submitted code, we have both a genuine WTF and a nice meta-WTF from TDWTF 😅
Admin
The real problem here is the poor data design. What if I want a list of all classes that meet on Tuesday? I can't search for daystr like '%T%' because that will find Thursday as well as Tuesday. And I can't search for daystr like '%T,%' because that won't find a Tuesday that's the last day in the string. This should be a separate table with a many-to-one relationship. Or at least, okay, there are only 5 days (assuming they never have weekend classes) , so have 5 booleans.
Admin
TRWTF is knowing enough PHP to use the explode function, but not enough to use the implode function to combine an array of data into one comma separated string.
That and the abbreviation 'Wen'.
Admin
And of course if you really want to go into TRWTF that is PHP you could use the one-liner
return implode(',', array_intersect_key(['M' => 'Mon', 'T' => 'Tue', 'W' => 'Wed', 'TH' => 'Thu', 'F' => 'Fri',], array_flip(explode(',',$DayStr))));
Admin
TRWTF is that Thursday is coded as "TH" and not as "R" -- which, literally, EVERY school and university I have ever attended does so I presume it's some sort of standard....? O, yeah ... I remember ...
M, T, W, R, F, S, U << yep -- that's all of 'em!
Admin
Then TRWTF is not mentioning that edit. While I'm not a big fan of "edited to add a missing comma" messages in general, in this case there should be once since the edit completely changes the meaning, is relevant to the main WTF of the article, and had been commented on many times already.
(And I mean in the visible text. I don't know and don't care if you mentioned it in the HTML comments. I don't read them on purpose, to make sure that fucking reCAPTCHA doesn't know I'm a bot.)
Admin
Unfortunately, the substr call is after the switch(), not the foreach; it should be two lines lower. This indeed is going strip all the trailing commas, for the desired(Why?) "Mon,Wed,Fri" is going to appear as "MonWedFri" instead. Well, at least the dates are the same length with a leading capital, so it should be easy to find if a given day is in the data set without ambiguity.
Personally, I would have used 7 Boolean fields or one byte and then filtered with distinct bits.
Admin
Technically removing the final character on each iteration could produce some very unexpected results if there were tokens other than M T W TH or F in the input. But I tend to agree that TRWTF is posting a version of the code that didn't actually have the said problem then apparently silently replacing it with one that did...
Admin
The funny part is that, at least in MSSQL, if you make seven individual bit fields, the server will actually implement it as bit masks under the hood but will deal with all of the complexity for you.
Admin
Why do people insist on checking a span is non-empty before iterating over it?
if(sizeof($Days)>0){
foreach($Days as $day) ... }
Admin
Actually, no, it isn't. It's going to appear as
" Mon Wed Fri"
with spaces before each abbreviated day name.Admin
Because PHP will throw an error otherwise. Specifically if you use strict error handling (as you should!).
Admin
It's interesting to think about how this which-days-of-week field would affect the ability to predict scheduling or classroom density. I'd imagine that the density is much more affected by the level of the course, if it's required, which room it's in, etc. The prediction of scheduling is more germane. However, if the classes tend to follow a pattern, e.g. MWF, TR or MW, as they did at my school, then mapping a prediction to one of three strings is equivalent to mapping to one of three one-hot encoded slots, which is as it should be. If there were true scheduling freedom, such as degrees of freedom around the time of day, duration of class, and which days the class met, it would be absolutely horrendous to deal with.
Admin
Now that's a WTF. In the (rare) case you don't want an empty span you can check it yourself, but usually doing nothing is just right. What's next, throwing an error when adding 0 or multiplying by 1?
Don't know if anyone ever said it, but PHP is TRWTF!
Admin
nope, they do actually remove all of the commas, but i think the intention was to only remove the last one, someone just put it AT the end of the loop instead of AFTER the end of the loop
Admin
Except, it is inside the forloop, so the switch chooses the string, and then the comma is removed each time...
Admin
Except, it is INSIDE the forloop but AFTER the switch, so the switch chooses the string, and then the comma is removed each time...
Admin
@Bananafish ref
My prior employer used SMTWRFJ. Yes, Saturday is J as in Juliet. Took some getting used to. Although the company was a multinational, this standard dated from the very US-centric 1960s.
Admin
I'm not sure about PHP shenanigans (what dusoft mentioned), but just from the logic perspective, if we all agree that the comma stripping was supposed to be outside the loop, which I'm pretty sure was the intention, then you wouldn't want to strip last character from an empty string.
Admin
It's not an error to try to loop over an empty array.
Of course, if the variable could be null instead of an empty array, you do need to check it first, But checking
count($Days) > 0
would be wrong becausecount()
requires the argument to be an array.The only excuse for having an
if (count($Days) > 0)
test before the loops is if you have anelse
block that displays something like "No days found", whereas the loop by itself would just display an empty list. But that's not the case here where we're just returning list.Admin
Because for the correct C pattern to implode an array into a string, the removal of the last delimiter is outside of the iteration and it would be a bug to call it on an empty string ;-)
Admin
@WTFGuy - "Unless the code has been changed since it was first posted," That is EXACTLY what was done, and it is not the first time "the staff" has tried to cover up their own WTF rather than simply admitting it.
Admin
one pattern the codebase I'm currently working on is: (its old php)
$result = $mysql_database->query($query); // the query function here just returns false on error, no excpetions to be found foreach($result as $row){ return $row; }unset($row); // no further returns so the function just returns null if anything goes wrong.
gotta love those brain-juices wich produced the foreach(){return;} pattern.