Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Sunday, December 2, 2018

Hour Of Code Is Here Again!

Tomorrow is the start of Computer Science Education Week, which also brings with it Hour Of Code. I've been coding professionally for almost 2 decades, but I got my start at a much younger age. I was fortunate. My Dad was always slightly a technophile, so my brother and I always had access to the gadgety stuff of the late 70s and early 80s. And when the Commodore 64 came out in 1982, my Dad had to have one. I loved that machine, and in fact it still sits in my garage (waiting for me to figure out that I should plug it back in somewhere). That machine, alone, likely affected both of his son's futures much more than he ever thought it would (my sister was born slightly before the C64, so was probably too young to see the full potential; she's great at Nintendo, but not a full geek like her brothers).
Load "Deadline", 8,1

But I digress.... Between the C64 and Family Computing Magazine, I taught myself BASIC. I became a wanna-be geek. My Mom said I should be a computer programmer. I wanted to be a pilot. So I became a pilot. But then decided I wanted to be a computer programmer. Listening to Mom is probably smart.

I was fortunate that I had people in my household that were able to support my fledgling geekism. Computers have changed a WHOLE lot in the decades since Copy/Paste was "look at the magazine text and type that into the console".  I look at initiatives like Code.org, and I see the possibilities that I did when I was a kid. We've reached a point in our history where computers are involved in pretty much everything we do. There are even LIGHT BULBS that can be programmed to do something other than provide white light to a room. Look around and you'll probably see a vast majority of people on a cell phone or browsing the internet on some device. For my entire life, Moore's Law has held true.

AI is scary-smart, but we still need people to program both the internet and that scary-smart AI that will be writing itself all too soon. I think Hour Of Code does a fantastic job of encouraging kids to see the possibilities of what they can do. So, as they say, "What will you create?".

Thursday, October 6, 2016

More Date Masking

I'm a bit ashamed of myself. I've been a member of the ColdFusion Community for over 15 years, yet I just submitted my first bug to the BugBase yesterday. And I'm not even really sure that it's a "bug" per se; just an inconsistency in behavior of similar functions.

https://bugbase.adobe.com/index.cfm?event=bug&id=4194962
(Lucee bug added: https://luceeserver.atlassian.net/browse/LDEV-1025)

Basically, it touches on what has become a pet-peeve of mine: Date Masking. I know I've ranted about it before, but I wish that everyone could just agree on what a proper date mask was for all of the common date parts.

In the latest update to ColdFusion, Update 3, one of the things Adobe announced in the release notes was that you can now use lower- or upper-case letters in DateFormat() masking. My issue with this is that it is completely different behavior from DateTimeFormat(), which seems to make a bit more use of Java's case-sensitive SimpleDateFormat. Granted, I don't agree with Java's way of using "M" to mean "months" and "m" to mean "minutes" (I like CF's "n" minutes), but I still think the two CF functions should be consistent with each other. It's feasible that a dev would have to switch between a date format with and without a time component or have multiple types of date masks on the same page. And the natural assumption would be that the mask from one should work as the mask for the other. At least I would think so.

Last year, I got bit by this behavior when it came to the Year mask. December 29, 2015 was being displayed as 12/29/2016. ???? FYI: DateFormat(someDate,"MM/DD/YYYY") may not return the same thing as DateTimeFormat(someDate,"MM/DD/YYYY"). This is where I learned about the slight difference between a Calendar Year and a Week Year. Not to mention the fact that it will only happen at most on the last few days of a year.

And to make matters worse, we also used moment.js, which expects the year to be formatted as a capital "Y". :-/

I've never made it a secret that I'm not a fan of silent errors. And, yes, this would be an error on the developer's part for not using the proper mask, but it's an easy error to make (especially when using different masks) and even easier to completely miss.

I would like to see Adobe make the date formatting functions be a bit more consistent with each other.


THANK YOU, TRYCF.COM! http://trycf.com/gist/78a374effc62c05330cd33030da265cd/acf2016?theme=monokai

Adobe ColdFusion 2016
Adobe ColdFusion 10

And Lucee just gets wonky with its CFMXish behavior of kinda passing through masks it can't interpret.
Lucee 4.5

Monday, May 2, 2016

Testing: A Confession

I have a confession to make. In the software world, this confession probably ranks right up there with kicking kittens or believing that Firefly deserved to be cancelled, but it's one that I still feel I should make. I've been a developer for over 15 years, and almost all of that time has been spent NOT WRITING TESTS. I have always done the traditional developer QA and smoke testing, but I have spent years making all sorts of excuses as to why I don't actually write tests: I don't have the time to do it. My company's infrastructure isn't set up for it. We have a QA team that does our testing. Or my favorite: We have a mass of legacy code and it's too hard to test.

Those may be some valid "reasons", but they're really just excuses.

I'm a hypocrite, too. I've known the value of testing for most of my career. I've preached that it should be done. But I've never really strongly advocated for it where I've worked. I'm changing that.

I work on a fairly significant code base that doesn't currently have any tests. We constantly make changes and we deploy often. In the last couple of weeks, I've been bitten twice by code that I just created that affected other pieces of my site in ways that I didn't expect. Proper Unit Tests would have picked up those regressions before I ever even checked in my code. So this has given me a bit of a kick to actually start doing something about it. I'm fortunate in that I have a few allies at work who see this need, too. I don't have to be the new guy bucking the system.

We work mostly with ColdFusion and jQuery, so a large hurdle has been just figuring out what the best way to test is. I've chosen Ortus' TestBox for my CF code. It's got great support. It's fairly easy to setup and use. I like it a lot. I've only got a couple dozen tests in place, but I already see several ways that I should have written my code better. I'm seeing some things that appear to be hard to test, but I think that's because I'm just not well-versed in actually testing software like this. I'm determined to get much better at it.

I've confessed my sin. I don't really feel that much better, but let the adventure begin.

Monday, February 29, 2016

Date Masking Inconsistency

What better way to celebrate Leap Day than with a Date Masking post? It's been cooking in my noggin a while and has been in some form of draft for a month, but better late than never, right? :-/

TL;DR: ALWAYS VERIFY YOUR DATE MASKS ARE DOING WHAT YOU THINK THEY'RE DOING!

I originally encountered this at the beginning of February and while digging for the cause, came across the issue in the CFDocs site (https://github.com/foundeo/cfdocs/issues/266). I played a bit with some of the masking and learned that some of my old ways were smacking me. I've been in the habit of masking a DateTime object with "MM/DD/YYYY..." or something like that. I absolutely hate date math and manipulation, but that hatred has led me to learning more than I ever wanted to know about the bazillion different epoch times and how 12:59:59.998 can sometimes be the same as 00:00:00.000. There's a bit of a rabbit hole there, and I've fallen down it more than I care for.

But I digress. The CFDocs site referenced an Adobe bug for CF10 (https://bugbase.adobe.com/index.cfm?event=bug&id=4105828) reported by Alexander Kwaschny, and as I was playing with this, I began to realize how big of an issue this actually could be for us. FORTUNATELY I didn't find anywhere in our code where we were using dateTimeFormat(), just dateFormat(), but we _were_ using the "YYYY" mask. I'm a firm believer in consistency (that may be a holdover from my piloting days), and I think that if you're going to do something, you should always try to do it the same way to minimize the opportunity for error. Date Masking is a big area that has a lot of potential to cause hard-to-detect problems, and we were setting ourselves up for a big one here. Though I can't really fault anyone, since pretty much every example of date masking or formatting that I've ever seen uses capital letters. I think it's insane for a language (THANKS JAVA, and maybe even ISO8601) to treat "Y" and "y" as two different things for such a narrow edge case. Until this issue, I had never even heard of a Week Year ("Y") (https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html). And to make the issue more fun, it would only even come into play at most about 6 days out of an entire year. You probably wouldn't even notice the problem.

So, my solution.... make sure my date masks reflect the proper things in the languages that I'm using. My years now are formatted with "yyyy" instead of "YYYY", though most of the time, it doesn't make a difference either way. Grrrr.....

======= MY RESEARCH ========
First, I wanted to verify that Oracle is documented to behave as I expect it to. https://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA Oracle 11g Datetime Format Elements

Yup, Oracle treats "Y" and "y" as the same thing in its formatting. Note: even the Oracle examples show "Y" for the date mask.

One other cool thing I discovered about Oracle... the capitalization of the Month mask will determine the capitalization of the output.
'' Months : To_Char(myDate,'MONTH') AS CAPDATE, To_Char(myDate,'Month') AS Casedate, To_Char(myDate,'month') AS lowdate ''
I guess it's not something I've ever used nor paid attention to, but it's still kinda cool to know.

Now, back to ColdFusion:

For the Year difference, DateFormat() doesn't seem to have the problem, but DateTimeFormat() does. Interesting. And Lucee/Railo gets even weirder. http://trycf.com/gist/908f3b1ed6d7490bc470/acf11?theme=monokai

<cfscript>
myDate = CreateODBCDateTime('12/30/2015 12:34:56.789') ;
writeDump(dateFormat(myDate,'mm/dd/yyyy')) ;
writeDump(dateFormat(myDate,'mm/dd/YYYY')) ;
writeOutput('<br>') ;
writeDump(dateTimeFormat(myDate,'mm/dd/yyyy')) ;
writeDump(dateTimeFormat(myDate,'mm/dd/YYYY')) ;
</cfscript>

Will return "12/30/Y'Y'Y'Y" for the dateTimeFormat(), but the proper date for simple dateFormat(). Isn't that old pre-CFMX behavior?

The other mask that bugs me is "S" and "s". "s" is Seconds and "S" is milliseconds, except for when "l" or "L" are milliseconds. And, "S" returns Seconds in dateTimeFormat() and timeFormat(), where it returns the milliseconds in dateFormat(), which technically doesn't even specify a time component to return. ARGH!!!!

Once again, CONSISTENCY CONSISTENCY CONSISTENCY. And the case of a mask shouldn't affect its functionality. There's just too much room for a silent error.

Anyway, that's enough for tonight. I guess these things are semi-documented in various places, but I still don't like it. It just seems way too common to not be an issue. The Adobe ticket says the documentation in CF was fixed to be clearer. I'll check that. And I need to pull down CFDocs and change the date function masks to note these differences in behavior. But this post became the rabit hole that I warned about earlier.


===== OTHER NOTES ========================================================

CF > DateFormat() vs DateTimeFormat()
DateFormat -
 9 = http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ff4.html
 10+ = https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateFormat.html

TimeFormat - http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6de0.html
DateTimeFormat - CF10+ - https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateTimeFormat.html
 "The function also follows Java date time mask. For more information, refer to Date and Time Patterns topic in SimpleDateFormat Java API page."

--------------------------

<cfscript>
myDate = CreateODBCDateTime('12/30/2015 15:16:17.19') ;
writeDump(dateFormat(myDate,'s')) ; // 17
writeDump(dateFormat(myDate,'S')) ; // 190
writeDump(timeFormat(myDate,'s')) ; // 17
writeDump(timeFormat(myDate,'S')) ; // 17
writeOutput('<br>') ;
writeDump(dateTimeFormat(myDate,'mm/dd/yyyy hh:nn:ss.l')) ; // 12/30/2015 03:16:17.190
writeDump(dateTimeFormat(myDate,'mm/dd/YYYY HH:NN:SS.L')) ; // 12/30/2016 15:16:17.190
writeOutput('<br>') ;
writeDump(datePart('yyyy',myDate)); // 2015
writeDump(datePart('YYYY',myDate)); // 2015
writeOutput('<br>') ;
writeDump(dateAdd('yyyy',1,myDate)); // {ts '2016-12-30 15:16:17'}
writeDump(dateAdd('YYYY',1,myDate)); // {ts '2016-12-30 15:16:17'}
</cfscript>
>>>>>>>>>>>>

http://trycf.com/gist/5a4209466ccf5e5ea854/acf11?theme=monokai
http://trycf.com/gist/ef686d4c4bf45fb85619/acf11?theme=monokai
http://trycf.com/gist/b24b9c91e6cee5e3c12e/acf?theme=monokai
http://trycf.com/gist/9db92000bb204a561edd/acf?theme=monokai

Links:
Original CFDocs Issue: https://github.com/foundeo/cfdocs/issues/266
http://www.sql-server-helper.com/tips/date-formats.aspx
http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-by-category/date-and-time-functions.html
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DatePart.html
http://www.petefreitag.com/cheatsheets/coldfusion/dateformat/
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateFormat.html