- 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
So basically if 0 than one color, else another. Honestly this looks to me like something that could make perfect sense in any number of contexts. And the color being the same could just be that at some point someone decided that the colors should not be different after all but wanted to keep the code in there just in case people changed their mind.
Admin
PHP has = for assignments and == for comparisons, right? Two major problems in one line.
Admin
fixed
Admin
It's a powerful signal. It's saying, "no matter what you think about tno, I'm setting bgc to #FFFFFF!" Who knows what sort of hijinx the next programmer might try to pull off if that line weren't in there, keeping everyone in line.
Admin
Huh. I've seen that exact same pattern in some of my hacky javascript from back in the day.
Admin
Reminds me of a student who kept writing statements like (in FORTRAN): A = A When asked why the response was "I want to make sure it takes."
Admin
It's an assignment statement. "?:" is a ternary operator, as the punny title points towards.
Admin
Yes, but ....
As Remy points out, there shouldn't be any branching at all. No decision making, just set the color! "... in case people change[d] their mind" they could put in a new decision. My guess is, ChangeMyMind guy will probably want something other than white, anyway.
Admin
I seem to recall a quote on this very site about needing a barrier to entry before using any programming language. And here we are with the language with no barrier to entry.
Admin
I think he meant to say that since the tern does nothing therefore it should not be there. Where as I pointed out that we only know it does nothing at this point in time and that there could in fact have been a point where it did before the code was disabled.
Admin
Right. I did read the code wrong, my brain inserted brackets like
($bgc = $tno) ? '#FFFFFF' : '#FFFFFF';
Admin
This looks very much like code that comes from a developer who just knows that at some point in the future their manager is going to want to be able to have a different background colour. It's code that's written from experience of having to deal with ever-changing specifications and manglement instructions.
Admin
We don't know. It could be "predictive coding" as you suggest, or it could be "we don't want different colors anymore". In either case this line should be commented out, with a hint of the reason. It doesn't need to be there because it does nothing, so why bother including it?
` // Just in case they decide to change colors:
` // We aren't changing colors anymore:
Admin
While I understand these feelings, I think it's very important to think things through, as we need to be real careful about pushing for any kind of regulation for what one can do on their own computer. We already have governments trying push age restrictions into operating systems, which is bad enough. These sort of ideals are very much contrary to free and open computing and getting to this sort of regulation can very easily lead down a dystopian path that I don't think any of us really want.
Admin
Yeah, that's too easy to do. I think the fact that that would leave an expression that calculated a value and then did nothing with it pushed me towards the (assumedly) intended meaning.
That said, if I encountered the fragment
$bgc == $tno ? '#FFFFFF' : '#FFFFFF';
I'd have no idea if it needs parens to be interpreted the way you wrote it. I like ternary ?: - it's concise and gets the job done. But oh my the precedence issues it raises!
Admin
"I'd have no idea if it needs parens to be interpreted the way you wrote it. I like ternary ?: - it's concise and gets the job done. But oh my the precedence issues it raises!"
Yes, that can be a real gotcha. While most C-ish languages evaluate
?:right to left, like(C1 ? V1 : (C2 ? V2 : V3)), PHP (IIRC, fixed in 8.x) went left to right, like((C1 ? V1 : V2) ? V3 : V4)throwing off anyone used to any other language (C/C++/Java/C#/JavaScript/Switft/Perl/Ruby/etc) which adheres to the former.Admin
I don't want regulations on what you can do in your private life. But I do want restrictions on what you can do commercially. Nobody should be regulating my right to sketch buildings in a notebook for fun. But if I want those things built in stone and people living in them I sure as hell should be forced to get a certification in architecture. Same here.
And it does not have to be prohibitively complex either. We can have tiers. Your website assembling script monkey needs a different level of permit than the guy building software for an airplane or worse a bank. But we need SOMETHING.
Admin
Oh, I think I remember seeing people complain about PHP acting that way. I wonder if that's why I associate ternary ?: with precedence issues? Sometime with coding I don't remember the specific accident, all I know is that I have a scar in a vague area.
Admin
I remember reading of it over a decade ago at https://web.archive.org/web/20180102221920/http://phpsadness.com/sad/30 (using the Wayback Machine as the original site appears to be gone), among other places where it was pretty routinely talked about it, as it seem to trip up a lot of people.