Cooldown WTF
-
var options = { cooldownDurationMs: 1000 * 60 * 2 // 2 minutes }; //... function isCooldownPeriodOver(cooldownStartedAt) { return !cooldownStartedAt || cooldownStartedAt + options.cooldownDurationMs < new Date(); }
Just your typical javascript cooldown code. What could go wrong?
Well...
console.log(isCooldownPeriodOver(new Date(1999, 1, 1))); //> false
... shit.
console.log(new Date(1999, 1, 1) + options.cooldownDurationMs < new Date()); //> false console.log(new Date(1999, 1, 1) + options.cooldownDurationMs, " < ", new Date()); //> Mon Feb 01 1999 00:00:00 GMT+0100 (Central Europe Standard Time)120000 < Thu, 05 Mar 2015 11:55:20 GMT
Wha... Oh.
console.log(typeof(new Date(1999, 1, 1) + options.cooldownDurationMs)); //> string
I'm an idiot.
function isCooldownPeriodOver(cooldownStartedAt) { return !cooldownStartedAt || new Date() - cooldownStartedAt >= options.cooldownDurationMs; } console.log(isCooldownPeriodOver(new Date(1999, 1, 1))); //> true
Here's the lesson kids. NEVER take javascripts type system for granted. This shit was in production for months.
-
-
NEVER take javascripts type system for granted
Now we'll see all future developers type checking every freaking variable before using it. Thanks to you.
-
And thats a bad thing with Javascript how?
Filed Under: Isn't that why you never use == but === ?
-
If only we had some kind of system that could check types automatically...
-
This shit was in production for months.
... and this is the first time you actually tested it?
-
... and this is the first time you actually tested it?
Tested it before in general, but not in detail. Poor unit test coverage. Live and learn.
-
-
but there is no
==
in that code...
-
On most other languages, using
+
with an object and an integer will raise an error (either because the language is strongly typed -- Python, or because+
is only used to add numbers, not concatenate strings -- PHP).
-
Tested it before in general,
What does that mean?
Automated integration testing. Manual testing. Can't dig deep enough to add unit tests. Not nearly enough time to even suggest.
Also, time-based stuff is a bitch to test anyway.
-
You're too impatient to sit for 2 minutes?
-
The following also works in C#:
var x = "7" + 9; Console.WriteLine(x);
The output is:
79
-
You're too impatient to sit for 2 minutes?
Times hundred for each little snippet like this. Times how many time I change the code. I don't scale.
Stuff like this needs unit tests.
-
On most other languages, using + with an object and an integer will raise an error (either because the language is strongly typed -- Python, or because + is only used to add numbers, not concatenate strings -- PHP).
So I question how checking the type of a variable everytime before using it is a bad thing and you tell me that there will be errors if you add two variables together that cannot be added together... this makes sense how? (inb4 you tell me that adding is not the same as multiplying...)
in C++ you could totally overload operator+ for this ;PAm I missing something crucial here? I mean besides that Javascript is (as PHP) TRWTF?
but there is no == in that code...
I should really use that "quote" thing more...== vs === is testing the type as well (as you are probably aware of) hence I tried to use it as an example on why always testing the type is a usually good practice.
Filed Under: Wow, this post came out somewhat aggressive... but I refuse to change it now :D
-
Unit tests are great and all, but I think what you really need is developers who don't take it on faith that what they wrote works without witnessing it. That's step one. Unit tests is like step 37.
-
The following also works in C#:
var x = "7" + 9;
Console.WriteLine(x);The output is:
79
Is that to do with how boxing works in C#?
Although, to be fair, you're bringing that on yourself by using
var
. I pretty much only use it in Linq and when I'm instantiating some long type, to save a bit of space on the line
-
Is that to do with how boxing works in C#?
More likely the compiler transforming
var x = "7" + 9;
into
var x = string.Format("{0}{1}", "7", 9);
or similar.
-
So I question how checking the type of a variable everytime before using it is a bad thing and you tell me that there will be errors if you add two variables together that cannot be added together... this makes sense how?
@Kuro said:Am I missing something crucial here?
What I think @VinDuv is saying is you don't need to check types in Python because if you've got mismatched types the compiler will complain instead of silently coercing the values.
-
-
which includes implied boxing, since 9 is a value type and has no explicit ToString() method
-
This is what the IL tab in LINQPad tells me:
IL_0000: nop IL_0001: ldstr "7" IL_0006: ldc.i4.s 09 IL_0008: box System.Int32 IL_000D: call System.String.Concat IL_0012: stloc.0 // x IL_0013: ret
Looking up String.Concat on msdn tells me that it uses
.ToString()
on all parameters.
-
which includes implied boxing, since 9 is a value type and has no explicit ToString() method
C# doesn't do boxing;int
aliases toSystem.Int32
, astruct
with aToString()
method.
-
not correct, as @RaceProUK shows.
and yes @aliceif is more correct, it's the internals of String.Concat that does the .ToString ()
-
-
C# doesn't do boxing
https://msdn.microsoft.com/en-gb/library/yz2be5wk.aspx would disagree.
I assume you mean it doesn't do autoboxing, but boxing is certainly a thing in C#
-
-
C# doesn't do boxing;
Da fuq? Yeah it does.
int aliases to System.Int32, a struct with a ToString() method.
This statement is true, but C# does boxing.
-
Da fuq? Yeah it does.
Late to the party again?
@RaceProUK said:@Jaloopa said:
I assume you mean it doesn't do autoboxing
Yes. That.
That's right above your post too.
-
Late to the party again?
Jeff Atwood says to read the whole thread before replying, so I expressly do not do that. We've been over this like a million times.
-
Maybe with other people, but not with me.
Or maybe I just forgot.
Either way, erm, hi
-
Jeff Atwood says to read the whole thread before replying,
reading the whole topic is a barrier to high post counts.
duh! :-P
-
Unit tests are great and all, but I think what you really need is developers who don't take it on faith that what they wrote works without witnessing it. That's step one. Unit tests is like step 37.
So your solution to "build me a pyramid" is "get more slaves", not "improve engineering"?
-
-
The interns we have don't work half of what most of us do. They also have the "I don't know how to build an operating system" excuse... ugh! you're no team player!
-
No; I'm saying that you need recognition that code should be tested as a preliminary step before you worry about the specifics of how to test it.
Step 1 isn't happening here, so I'm not convinced skipping to step 37 is gonna help.
-
-
The following also works in C#:
var x = "7" + 9; Console.WriteLine(x);
The output is:
79 ````</blockquote> Hehe, it should be: ``` "16" ``` Right?
-
-
Jeff Atwood says to read the whole thread before replying,
I like to read the whole thread out loud as I work my way down, but my wife hates it.
Filed under: my mouth moves as I type my replies
-
You're living in a sitcom?
-
Can't even screenshot all this shit in time!
I had a red heart (meaning I liked this post) showing, along with "1 person liked this. Like it too."
By the time my brain snapped out of the loop and I started Shutter it fixed itself
-
Happens to me sometimes, too.
-
You're living in a sitcom?
Ssshh! The neighbours aren't supposed to find out! Quick, help me hide the vicar's trousers!
-
https://dl.dropboxusercontent.com/u/1920627/Screencast 2015-03-06 09%3A19%3A49.mp4
Link if mp4 doesn't work: https://db.tt/b9GageZU
I fucking give up!
-
Browsers don't like 714x206 MP4's. Or High Complexity Profile, yuv444p, Predictive MP4's. Or MP4's in general, really.
-
Eh, I tried to turn it to gif using
ffmpeg
but it was over the upload limit.Why I didn't upload the result to Dropbox anyway? Because I'm stupid, and was at work, pretending to be working.
Also, fucking Kazam, it removes my panel when selecting the area and consistently shifts my recordings by 24px upwards when on primary monitor.
-
Yeah,
ffmpeg
tries to make it conform to Web Safe Colors using error diffusion dithering. Also known as "an amazingly effective way to make a 10 second, 100K video into a 10MB monstrosity."