News: 1641301512

  ARM Give a man a fire and he's warm for a day, but set fire to him and he's warm for the rest of his life (Terry Pratchett, Jingo)

Microsoft rang in the new year with a cutesy tweet in C#. Just one problem: The code sucked

(2022/01/04)


Microsoft has ushered in 2022 with an amusing (and now deleted) tweet from its Windows Developer account that answers oh so many questions about the quality of code emitted from Redmond nowadays.

As is so often the case, the code (which looked like it aimed to greet 2022 with a perky "Happy New Year") doesn't appear to have troubled a reviewer before going live. Taking aside the fact that it's going to whinge about it still being 2021 when it isn't, there's also the use of the ToString() method when [1]other properties might well have done the job better. And why the " == "?

[2]

Click to enlarge

Still, it's the thought that counts. One should consider oneself fortunate that one's own well-intentioned coding cockups have never plopped out of the social media orifice of a company with a market cap in the trillions.

Memories of having to deal with the frankly barking mad US date format still haunt your correspondent to this very day. And then there were the time zones to worry about...

[3]Microsoft patches Y2K-like bug that borked on-prem Exchange Server

[4]US Government Accountability Office explains why it sustained Microsoft's protests over $10bn NSA contract

[5]When product names go bad: Microsoft's Raymond Chen on the cringe behind WinCE

[6]Microsoft closes installer hole abused by Emotet malware, Google splats Chrome bug exploited in the wild

The tweet attracted the usual barrage of supportive and helpful comments typical of Twitter, including a meme from one of Microsoft's own coding boffins, Scott Hanselman, who later followed up with a [7]useful video he described as "an overly detailed analysis of a bad DateTime comparison in C#."

[8]https://t.co/wxwQcJ9oWY [9]pic.twitter.com/qjtT1RXcwo — Scott Hanselman (@shanselman) [10]January 3, 2022

The video is well worth a watch, not least because it delves into not just the obvious flaws in the code but also other considerations that need to be taken into account when handling dates.

As for the original code... well, at best it'll probably keep insisting it is 2021 aside for one brief second. And let's face it, even that isn't right. As another commenter [11]observed : "Yeah 2021 is wrong. We're still stuck in 2020." ®

Get our [12]Tech Resources



[1] https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=net-6.0#properties

[2] https://regmedia.co.uk/2022/01/04/windows_developer_mistake.jpg

[3] https://www.theregister.com/2022/01/03/exchange_servery2k22_flaw/

[4] https://www.theregister.com/2021/12/15/gao_nsa_microsoft/

[5] https://www.theregister.com/2021/12/15/chen_wince/

[6] https://www.theregister.com/2021/12/15/patch_tesuday/

[7] https://youtu.be/Y195oMMLlqM

[8] https://t.co/wxwQcJ9oWY

[9] https://t.co/qjtT1RXcwo

[10] https://twitter.com/shanselman/status/1478037629723561989?ref_src=twsrc%5Etfw

[11] https://twitter.com/adpead/status/1478041820823236614

[12] https://whitepapers.theregister.com/



msobkow

Yep. It is 2022-01-04 everywhere in the world EXCEPT the US, which has to stubbornly be "different" with the likes of Imperial measurements.

YYYY-MM-DD only makes sense - it sorts properly in string form. :)

Not all Yanks are wrong.

ShadowSystems

Some of us have always written it as YYYY.MM.DD, even when threatened by our school teachers with detention for "doing it wrong".

I'd simply point to my military background, highlight the fact that all the date+time stamps on every bit of paperwork that came from there was in said format, & ask them to take it up with the government.

It's fun to force a teacher to eat their words every now & then. =-)p

*Hands you a plate of homemade cookies still hot from the oven*

They're your favourite flavour thanks to the magic of my insanity! =-)p

Re: Not all Yanks are wrong.

Joe W

Yeah, and don't get me started on time zones. I work in a project where some brilliant person (before my time) decided to have date and time in local time. Did not matter when we had mostly data from business hours (not the case for oh such a long time), not much, and then we also have daylight saving. What. A. Mess.

Re: Not all Yanks are wrong.

Anonymous Coward

I've worked with the US, UK, and EU for 30 years so I always spell the month as three letters, I never count it. And I've never had a problem, if your language isn't English everyone still knows the date.

Tom 7

You wait until we get to the year 10000 problem!

Def

Hopefully I'll have retired by then.

devin3782

Yes that code does indeed suck there's not nearly enough 3rd party libraries abstracting/obfuscating that code, date formatting errors aside.

There's an XKCD for that!

Red Ted

As if by complete coincidence there is a recent [1]XKCD Cartoon about date formatting!

[1] https://xkcd.com/2562/

Re: There's an XKCD for that!

Art Slartibartfast

Beat me to it!

Re: There's an XKCD for that!

KarMann

And of course, the classic-but-not-so-coincidental [1]ISO 8601 of 8 years ago .

[1] https://xkcd.com/1179

Re: There's an XKCD for that!

Ian Johnston

And of course, the classic-but-not-so-coincidental ISO 8601 of 8 years ago.

Which misses the points of standards entirely. They do not define "the right" way to do anything, just "a" way. It's the fact that it has been defined which matters. There is absolutely nothing wrong with having more than one standard in existence, as long as you make clear which one you are using.

Coding, Sigh

b0llchit

The perfect example of the insanity of " everyone can code " and " everyone needs to learn coding ". It is the road to disaster. Imagine these people coding a part of the fly-by-wire system...

Re: Coding, Sigh

Jellied Eel

I know enough about coding to know I'd rather leave it to someone who knows what they're doing. I know enough to vaguely recognise it as C on account of the ==, but can't remember why C did that. Isn't 1 equal enough? Which I guess it possible if you're not too fussed about data types. Or if one = just means it's good enough for government work, and 2 equals means you really want it to equal that.

Sadly my copy of K&R succumbed to gravity, and then the Cam. I think I ended up putting Numerical Recipes in the microwave. Guess if I'd waited for YT, I could have made that into torture pron for programmers.

Re: Coding, Sigh

John Riddoch

One = mean assign, two means comparison. As to why that was done way back when, I don't know for sure. It does mean that both of these are valid in C although they behave differently (in most cases):

if ( x = y ) // assigns value of y to x, checks if that is true or false (true=0, false anything else IIRC - it's been a while)

if ( x == y ) // compares x to y, doesn't change value of either

The fact that the top option works and will compile must have been the source of millions of bugs in C programs over the years. I will note that, for my sins, I have used the method of assigning a variable in an "if" statement intentionally (in Java, FWIW, although the methods are the same) and the code worked as expected.

Re: Coding, Sigh

Ace2

No modern (for an expansive definition of modern) C compiler will allow that through without at least a warning.

Something like, “Suggest extra parens around assignment used as conditional.”

I’ve seen it once or twice…

Re: Coding, Sigh

DeathStation 9000

You're close but utterly wrong :-)

In C zero is false and everything else is true.

Re: Coding, Sigh

Tom 7

Compilers will warn about common possible errors nowadays. As a software engineer I used to try and write code to add to check for things I could work out how to check for so code was always as thoroughly automatically checked before it hit the compiler.Even in the 80s there was a shed load of code and tricks to leave your C code free of most of the things that still haunt many today.

Re: Coding, Sigh

Ian Johnston

The perfect example of the insanity of "everyone can code" and "everyone needs to learn coding". It is the road to disaster. Imagine these people coding a part of the fly-by-wire system...

Unlike the professionals who coded the payments systems for Santander (did 'em twice) or Nationwide (didn't do them at all) you mean? Or the professionals who coded the Crossrail signalling system (three years late and counting) and every NHS computerisation ever.

Face it - standards of coding right across the industry are abysmal and getting worse.

Video..

Zanzibar Rastapopulous

Why does everyone do this stuff in videos rather than the text where it belongs?

Re: Video..

David 132

Gah. Upvoted. I get so furious when I google “how to fix $error” and the top 10 results are youtube videos. I don’t want a video! A simple numbered list of steps would be more than enough, have more clarity, and save megabytes of download!

Can’t decide whether to use thumbs up icon, for you, or exploding nuke icon, for my current state of mind. Let’s compromise and have a drink. Cheers.

TrevorH

> "Yeah 2021 is wrong. We're still stuck in 2020."

No, it's 2020 too.

Joe W

*groans*...

*hands over pint*

You think you've got problems with dates....

RockBurner

Try ensuring csv files created to local standards in every possible country's 'local' standards can be parsed by the same bit of code....

(as a hint... not everyone uses the ' . ' character as the delimiter between integer figures and the following decimals...)

Nowt wrong with a bit of genuine old fashioned C

Eclectic Man

void PrintTime( )

{

time_t current_time;

char* c_time_string;

/* Obtain current time. */

current_time = time(NULL);

if (current_time != ((time_t)-1))

{

/* Convert to local time format. */

c_time_string = ctime(¤t_time);

if (c_time_string != NULL)

{

/* Print to stdout. ctime() has already added a terminating newline character. */

(void) printf("%s", c_time_string);

}

}

}

Fixed it for you

Philip Stott

if (DateTime.Now.Month == 1 && DateTime.Now.Day == 1)

Console.WriteLine("Happy New Year");

else

Console.WriteLine("It's still " + DateTime.Now.Year.ToString());

Re: Fixed it for you

richardcox13

Code review failed:

1. dependent on user's time zone!

2. doing system call to get current time twice

The garden is in mourning;
The rain falls cool among the flowers.
Summer shivers quietly
On its way towards its end.

Golden leaf after leaf
Falls from the tall acacia.
Summer smiles, astonished, feeble,
In this dying dream of a garden.

For a long while, yet, in the roses,
She will linger on, yearning for peace,
And slowly
Close her weary eyes.
-- Hermann Hesse, "September"