CodeSOD collection
-
@BernieTheBernie Don't run this test microseconds before midnight.
-
@topspin said in CodeSOD collection:
@BernieTheBernie Don't run this test microseconds before midnight.
Yep, there's gremlins at midnight.
-
@topspin said in CodeSOD collection:
@BernieTheBernie Don't run this test microseconds before midnight.
I actually had a real bug caused by (almost) this, years ago. I don't remember the details anymore, but I remember that the code was checking some sort of delay or timeout with
if (current_time-start_time < some_constant)
and of course it failed at midnight.The reason I remember it is that it took me and a tester a couple of weeks to find it, because it was part of a long process that me and the tester happened to run at different times of the day (by chance) so I never witnessed the issue myself. Basically, it went: bug reported, I change something in the code first thing in the morning, run a successful test which finishes by mid-afternoon, send it to the tester who starts the test before leaving in the evening and in his case it should finish by early morning, but of course it fails because midnight so the next morning I get the error report and start the process again.
-
@remi Always do date and time arithmetic on either Unix seconds or Julian days. They're both monotonic and non-crazy. (That library that you're using? It probably does exactly that, and that's cool.)
-
@dkf I definitely was using the library's functions, but this is when I learnt the difference between
QTime
andQDateTime
.I'm not entirely sure why Qt has both, my guess would be 1) hysterical (Qt 1 probably started with just "time" and that's now too ingrained to easily change) and 2)
QTime
is probably much lighter-weight as it doesn't have to worry about days and time zones and what-not, meaning in many cases it's a better fit than the more complicatedQDateTime
.But the upshot of all that is that while the naive idea is to use
QTime
to measure time, well, it's naive.
-
@remi said in CodeSOD collection:
@dkf I definitely was using the library's functions, but this is when I learnt the difference between
QTime
andQDateTime
.I'm not entirely sure why Qt has both
My guess is that it's like SQL, in that a time represents a time within a day without specifying what that day is, making it a very different type of type to a timestamp (more akin to an angle than a length).
-
@dkf said in CodeSOD collection:
(more akin to an angle than a length)
Great simile.
Time of day <-> angle around a clock/sundial
Date+time <-> length in history line
-
@Zecc Yup, very good analogy that I'd never heard before, thanks @dkf!
Even more fitting for us (=my team) because we're used to dealing with angles (either azimuth of a geographical object or phase of a signal), so we're all familiar with the issues they cause.
And now I've already spent too much time thinking about what
sin(time of day)
could represent.
-
@remi said in CodeSOD collection:
And now I've already spent too much time thinking about what sin(time of day) could represent.
Must not be nerd-sniped, must not be nerd-sniped, ...
-
@remi said in CodeSOD collection:
@Zecc Yup, very good analogy that I'd never heard before, thanks @dkf!
Even more fitting for us (=my team) because we're used to dealing with angles (either azimuth of a geographical object or phase of a signal), so we're all familiar with the issues they cause.
And now I've already spent too much time thinking about what
sin(time of day)
could represent.It’s 0.777523 somewhere
-
@topspin said in CodeSOD collection:
@BernieTheBernie Don't run this test microseconds before midnight.
I won't do that - 5 o'clock means Feierabend.
Anyway, with currently a few dozen tests failing during the Jenkins job, no one would bother if that test added another failure...
-
@BernieTheBernie said in CodeSOD collection:
Anyway, with currently a few dozen tests failing during the Jenkins job, no one would bother if that test added another failure...
You need a cluebat? We make a lighter unit than the British, but it hurts a lot more to get hit with, s'faster.
-
So our software shows a false color image of temperatures.
What do you think of following scale:
-
@BernieTheBernie said in CodeSOD collection:
What do you think of following scale
It's a bit... ambiguous.
-
@topspin Each color corresponds to exactly one temperature.
-
@topspin said in CodeSOD collection:
ambiguous
Did you confuse that with "ambidextrous"? Or "amphibian"?
-
@Zecc 40? 25?
-
@BernieTheBernie said in CodeSOD collection:
@topspin said in CodeSOD collection:
ambiguous
Did you confuse that with "ambidextrous"? Or "amphibian"?
It's a valid but ambiguous use of ambiguous.
-
-
@Watson said in CodeSOD collection:
Inverted from the usual color symbolism is far from the weirdest false color I've seen. You get plaid easier when they try for higher contrast. For instance contrast perceptible to the RG colorblind. And BG colorblind.
-
@Watson said in CodeSOD collection:
@Zecc 40? 25?
I didn't say anything about it being an injective and/or surjective function.
Only that if you see a color it's not ambiguous what temperature it corresponds to.
-
@Watson said in CodeSOD collection:
@Zecc 40? 25?
40 is a very light yellow, but still yellow. 25 is white. Similar, but still different.
-
@BernieTheBernie 40 and 25 show up twice.
-
@Benjamin-Hall ehm, yes, and we do not know witch one is inclusive and witch one is exclusive. Perhaps both are exclusive.
-
@BernieTheBernie It's 2021; everything must be inclusive. How would you feel if you were the 40 or 25 that was excluded?
-
@HardwareGeek said in CodeSOD collection:
@BernieTheBernie It's 2021; everything must be inclusive. How would you feel if you were the 40 or 25 that was excluded?
Similar to the sublimated envy of a misanthrope? I bet it would feel a lot like that.
-
@Benjamin-Hall said in CodeSOD collection:
@BernieTheBernie 40 and 25 show up twice.
40 is shown like , 25 like . What's so hard to understand?
-
Does calling the
IsValid
property destroy the object? Bernie did not dig fürther into Kevin's code, that snippet is bad enough:GeometryUiContainer geometryUiContainer = new GeometryUiContainer(m_TransformedGeometry, panoramaSize, _textRotation); return geometryUiContainer.IsValid ? new GeometryUiContainer(m_TransformedGeometry, panoramaSize, _textRotation) : null;
-
@BernieTheBernie I especially like the spacing with 31,33,34,36...
-
Johnny looked at Bernie's code:
if (clusters.IsNullOrEmpty()) { return; }
WTF? Let me clean that up!
IEnumerable<ICluster> enumerable = clusters as ICluster[] ?? clusters.ToArray(); if (enumerable.IsNullOrEmpty()) { return; }
Yeah, that's much clearer now!
-
@BernieTheBernie said in CodeSOD collection:
Johnny looked at Bernie's code:
if (clusters.IsNullOrEmpty()) { return; }
WTF? Let me clean that up!
IEnumerable<ICluster> enumerable = clusters as ICluster[] ?? clusters.ToArray(); if (enumerable.IsNullOrEmpty()) { return; }
Yeah, that's much clearer now!
Any allergies to ashwood? Thinking of getting you a bat.
-
Bernie uses to program his classes such that mis-configuration gets detected at startup, and have the application crash immediately (the configuration must not be changed by the end user - it is setup by our service team). Better crash immediately than hunt for hidden bugs later on.
But that was beyond the capacity of Kevin. He commented one line of code out:
public void Initialize() { // ConfigurationHelper.AssertMultipleDependecySet(_List, Name, ConfigurationConstants.ALARM_CONFIRMATION_RESPONSE_SENDER_DEPENDENCY); }
That originally made sure that the component has one or more items of a specific type which it will communicate with.
But now, Bernie just had to deal with an exception:The given key was not present in the dictionary.
and some stack trace leading to this class.Why? Well, because the dependency was not set at all...
-
@BernieTheBernie said in CodeSOD collection:
Why? Well, because the dependency was not set at all...
Well, yeah. Bernie forgot that Kevin does not add dependencies, but removes code.
Simplify, Bernie. Simplify.
-
@Gribnit And Bernie is not in a position to remove Kevin.
Unfortunately.
-
@BernieTheBernie said in CodeSOD collection:
@Gribnit And Bernie is not in a position to remove Kevin.
Unfortunately.
Can you arrange a position 2 kilometers away at similar elevation with cover and minimal wind? Line of sight is not essential. It sounds like you could use an umbrella stand.
-
@BernieTheBernie You need to add something that is basically
if CurrentUser == 'Kevin': schedule hourly reboot; reboot now
. Like that, he'll not have recovered from the last reboot by the time the next one rolls round and he'll be kept out of the way.Some obfuscation might be required.
-
@dkf said in CodeSOD collection:
@BernieTheBernie You need to add something that is basically
if CurrentUser == 'Kevin': schedule hourly reboot; reboot now
. Like that, he'll not have recovered from the last reboot by the time the next one rolls round and he'll be kept out of the way.Some obfuscation might be required.
I think
git checkout
a random modified path every 3d6 minutes, is most to the point.
-
@BernieTheBernie said in CodeSOD collection:
Johnny looked at Bernie's code:
if (clusters.IsNullOrEmpty()) { return; }
WTF? Let me clean that up!
IEnumerable<ICluster> enumerable = clusters as ICluster[] ?? clusters.ToArray(); if (enumerable.IsNullOrEmpty()) { return; }
Yeah, that's much clearer now!
Now you get a NullReferenceException if clusters is null.
-
@error said in CodeSOD collection:
@BernieTheBernie said in CodeSOD collection:
Johnny looked at Bernie's code:
if (clusters.IsNullOrEmpty()) { return; }
WTF? Let me clean that up!
IEnumerable<ICluster> enumerable = clusters as ICluster[] ?? clusters.ToArray(); if (enumerable.IsNullOrEmpty()) { return; }
Yeah, that's much clearer now!
Now you get a NullReferenceException if clusters is null.
As it was, in the beginning. But you get a new NPE in the new one if this is in a static scope and the variables are volatile and your thread saw a non non-null clusters and then the enumerable copy was smashed by a null from another thread before the empty check. Which is a lot more fun.
-
@Gribnit said in CodeSOD collection:
@error said in CodeSOD collection:
@BernieTheBernie said in CodeSOD collection:
Johnny looked at Bernie's code:
if (clusters.IsNullOrEmpty()) { return; }
WTF? Let me clean that up!
IEnumerable<ICluster> enumerable = clusters as ICluster[] ?? clusters.ToArray(); if (enumerable.IsNullOrEmpty()) { return; }
Yeah, that's much clearer now!
Now you get a NullReferenceException if clusters is null.
As it was, in the beginning.
Extension methods can be called on null objects, and that's part of the contract of IsNullOrEmpty. null.ToArray() will throw though, as it's not possible to convert null to an array. Not even getting into weird multithreaded conditions.
-
@error said in CodeSOD collection:
Extension methods can be called on null objects, and that's part of the contract of IsNullOrEmpty.
Yikes. Leave it to C# to solve a problem in the bug-friendliest possible way. TIL. WIH.
-
@Gribnit said in CodeSOD collection:
@error said in CodeSOD collection:
Extension methods can be called on null objects, and that's part of the contract of IsNullOrEmpty.
Yikes. Leave it to C# to solve a problem in the bug-friendliest possible way. TIL. WIH.
An extension method is a static method with a first argument of the type you're extending.
Are you saying you can't have null as the first argument of a static method?
-
@Zecc said in CodeSOD collection:
@Gribnit said in CodeSOD collection:
@error said in CodeSOD collection:
Extension methods can be called on null objects, and that's part of the contract of IsNullOrEmpty.
Yikes. Leave it to C# to solve a problem in the bug-friendliest possible way. TIL. WIH.
An extension method is a static method with a first argument of the type you're extending.
Are you saying you can't have null as the first argument of a static method?In Java, statics invoked from a null reference is like invoking them any other allowed but non-semantic way. The reference don't mean shit. Or maybe it breaks... dunno b/c I don't do that. And Java defaults act like instance methods, so, no bridge there. This looks like a way to detect a null later. Danger, Will Robinson.
In review, having that static call look dynamic hides a source of error.
-
@Gribnit said in CodeSOD collection:
Or maybe it breaks... dunno b/c I don't do that.
It works. You can't just do
null.staticFoo()
of course; the basic type ofnull
has no static methods (and is a weird case in the type system anyway). But you can do((Foo) null).staticFoo()
. It's resolved at compile time using the type that the null reference is declared to be, and it's discouraged (because using the class name instead is a lot clearer) but it does work. Unless you're doing it all reflectively, but then you have to deal with the reality in the JVM (which doesn't support any of that shit because it is stupid and unnecessary).You will be pleased to note that you do get a warning if you insist on doing this stupid thing.
-
is happening here? The number of failing test during the Jenkins built used to be higher!
Bernie was astonished. He looked up the built history.
Yes, just a couple of days ago the number of failing tests has fallen by 1.
?
Bernie investigated deeper. He looked at the change set comment connected with this odd built: "WTF repair". What a comment.
By Bernie...
Oh, yes, "That was when I repaired Kevin's fuck up."Remember the line commented out by Kevin in an
Initialize
method mentioned above?public void Initialize() { // ConfigurationHelper.AssertMultipleDependecySet(_List, Name, ConfigurationConstants.ALARM_CONFIRMATION_RESPONSE_SENDER_DEPENDENCY); }
There was a test for this method. Of course, Kevin does not care for failing tests.
And Kevin has almost coped with educating Bernie for not caring either...
-
Johnny checked in a new project. Interesting title, Bernie thought, and looked at the code of this small project.
There's a Service, and there's a MainWindow incl. MainViewModel, too.
Lots of the code seem duplicated from other projects, but simplyfied: hard coded values which are appropriate for a specific device, but wrong for a different type. Or existing functions re-invented. Etc.
But no code was found which fits the title.
Well, so what?Bernie wants to show you a small gem of that project, which does not require too much background knowledge on InniTech's projects.
It's a WPF window, which contains two items, placed on a grid:<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="310*"/> <ColumnDefinition Width="125*"/> <ColumnDefinition Width="406*"/> </Grid.ColumnDefinitions> <Image x:Name="image" Margin="10,10,186,10" Source="{Binding TheImage}" Grid.ColumnSpan="3"/> <TextBox x:Name="textBox" Height="23" Margin="0,635,16,0" TextWrapping="Wrap" Text="{Binding TheText}" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" Grid.Column="2"/> </Grid>
Despite there are 2 items only, the Grid has 3 columns.
The Image starts in Column 0 and extends over all 3 columns. Note its Right Margin of 186.
The TextBox is placed in Column 2 (the rightmost column). Note its Top Margin of 635.
With these sepecified margins, the TextBox still sits right of the Image, and on the lower end of the Window,In the end, Johnny again placed the controls in VB6 style (there you had to specify the position relative to the Form).
Welcome to the future of WPF.
-
Let's go round Math.Round()!
Another little gem from GröPaZ Kevin:double posX = Math.Round(motorData.Position.X * 1000) / 1000; return posX.ToString(CultureInfo.InvariantCulture);
What's wrong with it?
Well,Position
is aPointF
. Kevin wants to show 3 decimals, I guess...If only there was a method to tell
Math.Round(double a, int decimals)
how many decimals are requested...And beyond that, the current motor implementation (as well as the prototype of the next generation) reports the values with 1 decimals only. So both values of that PointF will only have 1 valid decimal anyway. Let's add 2 zeros to its end...
-
@BernieTheBernie said in CodeSOD collection:
If only there was a method to tell Math.Round(double a, int decimals) how many decimals are requested...
That's one place where C# is better than Swift (among many). Their rounding function doesn't have that way. And frequently gets things wrong if you don't explicitly round and truncate to 2 decimals for UI layouts. Because iOS.
-
@BernieTheBernie If only there were a method to tell
double.ToString(string format, CultureInfo formatProvider)
a format string it wanted to use, including the number of decimals...
-
@TwelveBaud But that won't
round
, it wouldtruncate
.