- 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
It's not just "not exactly now". Imagine running the script just before the end of the hour, the hour may be '17' and minute '59', and on the next operation, the hour may be '18' and the minutes '0'. It'll be an hour off. At the end of the day, it may be a whole day off. Or a whole month. Or even a whole year.
That should be fun to debug!
Admin
Can't see how you came to that conclusion. I see how each parameter would get parsed in turn (left-to-right in C#?), but even so it just means time would move on a fraction of a second as each parameter gets processed. The end result would may have switched on to a new second, hour, month etc, but the difference between 'now' and the final result would only be the sum of each fraction of a second it takes to process each parameter. Unless I'm missing something :)
Admin
20131231235959.99999... + 0.00...1 --> 20130101000000.00000... is (almost) one year off
Admin
10.. 9... 8... 7... 6... 5... 4... 3... 2... 1...
Happy new, er, wait, last year!
Admin
Now.Hour -> 17:59:59 -> returns 17 now.Minute -> 18:00:00 -> returns 0
So now we will build a date time for 17:00 (which definitely is NOT close to 17:59 or 18:00
And it also works the other way around (e.g. if minute is done before hour. You just will be one hour late --> 18:59)
Admin
Consider the time is '2014-12-31 23:59:59' when your method begins.
If the time happens to tick over to '2015-01-01 00:00:00' just after you read the year part, then your full timestamp will be '2014-01-01 00:00:00' - an entire year out.
Admin
Thank you for all the people who corrected my flaky conclusion!
Admin
And as a little side effect you can even get nice stuff like 31 of february if you time it right.
Not sure how the DateTime constructor reacts to these parameters. Chances for an error are pretty high though...
Admin
The last one is not that much of a WTF... None of the suggested existing types provide the same behavior.
On the other hand, it is a mild WTF for these reasons:
Admin
Admin
You're obviously not that familiar with the requirements for a hash code. Two objects of the same type returning the same hash code might be equal. If you have such a situation, then you have to check for actual equality using Equals.
GetHashCode returns an int, but there are more than 2^32 possible combinations of two strings (heck, there are more than 2^32 individual strings) and so you will have hash collisions anyway.
Admin
The StringString struct is fine. Tuple is a class, and KeyValuePair<T,U> doesn't implement IEquatable<KeyValuePair<T,U>>.
The only thing missing from StringString is declaring the implementation of IEquatable<StringString>, and the fact that key and value are not readonly.
Admin
You're getting the month before the day.
Admin
Can't help but think of MoonMoon when reading that last snippet eheh
captcha: letatio - things you do to a lamp
Admin
I feel like the compiler should be smart enough to know that a dozen different copies of Now.DateTime that aren't used more than once and have no explicit delay between their invocation should really all be the same DateTime object.
Admin
I believe the objective here is make the code unmanageable for the next programmer who steps in.
Have you not seen Kitchen Casino?
Admin
If you're interested, here's the IL code generated with LINQPad 4, with optimization on:
See, the compiler doesn't optimize the code. Not sure if the JIT does that, but probably not since DateTime.get_Now is not a pure function
Admin
I disagree - at the lowest level, Now.DateTime should be marked volatile, and there's nothing that allows the compiler to cache the static method invocation.
Admin
You're right. Just found that C# strictly defines the order in which the parameters are resolved (I originally thought it would be compiler magic...)
Admin
So your in faver that the following code should "cache" the results of Now?
Log("Starting at" + Now.ToString()); ExecuteSomeLengthyProcess(); Log("Finished at" + Now.ToString());
This would then result in the following output "Starting at <some time>" <Log entries of process> "Finhshed at <same time as above>"
Admin
Hashes should be xored, not added. Adding up evenly distributed values doesn't result in an even distribution.
Admin
The first looks all too much like something I've seen in all too many PHP scripts.
With maybe a "$minute + 1" so that "$time" is one minute in the future - this being the point of the exercise. I'm willing to bet that "not exactly now" is less exactly now for PHP than for C#.
Bonus points for those who set $minute to date('m').
Admin
My Java (?) is a bit rusty, but if I am not mistaken the == operator for StringString - and therefore also the equals method - will check that the key and values of two StringStrings are the exact same objects, rather than possibly different objects with identical values.
Of course this may have been intentionded by the programmer, but it is counter-intuitive at the least for the equals method.
Admin
For a reference type, the rule of GetHashCode is that is must never change, otherwise you can't put it in a dictionary/hashset as the key. But I guess it's okay for a value type.
Admin
Surely there's no need to implement both == AND !=....right?
Admin
And the culprit of the string reverse comes clean. Pop over to Reddit and give him some karma to drown his sorrows.
Admin
All this carping about the possibility of the date rolling over between calls to Now! This problem is easily fixed:
There!
Admin
How would it know?
If you write
I'd guess you want the hour and minute for the same time. But
You surely do NOT want the same time used in both cases.
A human being looking at your code could probably make a good guess which you wanted, but how would the compiler know? The compiler isn't supposed to figure out the intent of your code, it's just supposed to do what you wrote.
Didn't we just have a story on here about the computer being required to read the user's mind?
Admin
Okay, the DateTime one is a WTF, but whoever submitted the "LINQ" one doesn't know their .NET.
That isn't a LINQ function. It's a lambda function, as clearly indicated by the '=>' operator.
Also, Remy should be ashamed (nothing new there). I took a look the source of the page, and he flagged this code as objective-C. This is clearly a .NET code block, whether it is a LINQ or lambda sample. Either way, an objective-C flag should never have come close to this code.
Addendum (2014-05-01 10:38): Some references:
LINQ Lambda Expressions
Addendum (2014-05-01 12:13): CORRECTION: I forgot that LINQ can be used as standard function calls as shown in the code snippet. I haven't used LINQ in a while due to the standards where I work, but I get plenty of chance to use lambda expressions for cross threading purposes. Just goes to show we can all have stupid moments.
Admin
Umm ... no. If the state of the object changes, I'd expect the hash code to change, or at least to possibly change.
In this example, if the value of "key" is changed, I wouldn't want or expect the hash code to remain the same.
If you're building a dictionary of these objects that is keyed by hash code, than it probably makes no sense for the has code NOR the key to change. It might or might not make sense for the value to change, depending on what you're doing. So you might say that once the object is created the key can never change, and that the hash code is derived only from the key. Or you might say neither key nor value can change. But I doubt I would allow the key to change but keep the hash code constant. That would not seem to make sense.
If you're not building a dictionary and you can freely change the key and/or value of the object, then it would almost certainly not make sense for the hash code to be constant regardless of changes to the key and value.
Maybe you could spin scenarios, depending on just what you're up to.
Admin
It gives you a way to implement four way value for equality.
a==b, a!=b T, T T, F F, T F, F
So now you have to check both.
Admin
All you people talking about just using substring instead of a complex LINQ function, or just saying time1=DateTime.now instead of writing a complex and involved function ... Oh, so you just want everything to be simply and easy! Wow, lazy people. If you don't do everything the hard way, how will you build character?
Admin
You just walked in circles.
If you imply a value is a key in code, and that it is used for the hash code, you shouldn't allow it or the hash code to change.
That's just being a polite coder.
The programmer should have just returned key in the hash.
If they wanted to have both values in the hash, and intend the whole object to be a key, they should have just used the default object.GetHashCode();
Either way, they've circumvented all the default logic that provably solves their problem.
Admin
(ducks and runs) :-D
Admin
Interesting that Reverse(), Take() and Aggregate() all appear in the System.Linq namespace, if it isn't using LINQ functions. Not buying your logic on that.
Captcha: usitas, as in "usitas la fuerza, Lucas!"
Admin
"That isn't a LINQ function. It's a lambda function"
What on earth does that mean? There's more than one call in that code snippet. Several of them are LINQ functions, one of which takes a lambda function as a parameter. Whatever made you think that you can't have LINQ and lambdas in the same code?
Admin
But this should use only two instances of Now, not six:
Admin
Whoops, missed those. I haven't had a chance to use LINQ in a while due to the standards where I work, but I use plenty of Lambda expressions for cross-threading. Guess my rustiness in that area is showing this morning.
My complaints about Remy tagging that code snippet as objective-C still stand though.
Admin
Where did you see that tag?
Admin
I think there are a number of reasons this is a kind of dumb suggestions, but the above would at least be workable...
Admin
I forgot to say: for my suggestion to work, you'd perhaps have to define DateTime.Now.xxxx properties so that they didn't inject sequence points for purposes of that behavior.
Admin
What does compiler do with:
log( "started " + now.hour + get_delimiter() + now.minute )
or
log( "started " + format( now.hour ) + ":" + format( now.minute ) )
Admin
Admin
Admin
Well you could probably do that.
But on the other hand, what if the code is explicitly written to identify IF the minute changed between the calls ;)
I know, its extremely far fetched, but on the other hand, the compiler should not make guesses that actually change the behavior just in case.
Imagine the horror of trying to debug it.
That said, the editor could, like the resharper plugin for visual studio, high light the multiple calls and suggest that you merge them, but the decision must stay with the developer.
Admin
Well, I didn't flag the code as Obj-C. The syntax highlighter we use flagged it that way. I should probably upgrade that- newer versions are supposed to have better detection.
Admin
Sure. Computers are so accurate at correcting our text messages, so why not let them guess at what we really meant to code?
Admin
Admin
Yes, it does, because int addition is modulo 2^32.
Admin
This is the type of comment that I was hoping for when I submitted the snippet. Thank you!