Thursday, February 16, 2017

ES6 Arrow Notation 'this' Scope Bug with FiddleJS

I just ran into an interesting bug. I've been ramping up on ReactJS and as part of that, one of our senior devs recommended that I get a handle on ECMAScript 6. He pointed me to a nice ECMAScript 6 course on Pluralsight that I really enjoyed.

One of the exercises explored the "this" behavior inside arrow notation and how it differs from behavior within traditional function definitions.
'use strict';
var price = 5.99, quantity = 10;
var productView = {
   price : 9.99,
      quantity : 10,
      traditionalFunction() {
        return this.price;
   },
      arrowNotation : () => this.price
};

console.log('Traditional Notation ', productView.traditionalFunction());
console.log('Arrow Notation ', productView.arrowNotation());

In JSFiddle, the traditional notation returned the expected result (9.99), but the arrow notation says 'undefined' instead of 5.99.

















Running this same code in Chrome or Firefox (or even Plunkr) worked fine!


I thought maybe I needed to add babel to pre-compile down to ES5, but according to the babel website, JSFiddle already runs it for you. I'm not at all familiar with Babel, but the Babel "Try It Out" tool translates the arrow notation into the traditional notation, but swaps "undefined" for this.


Ah-ha! It turns out Babel _is_ the problem. The 'undefined' message is Babel's behavior. Apparently someone else noticed this almost a year ago (Mar 2016), but it wasn't addressed in the forum. I added my two cents and the screen shots above to the thread. When I have more time, I'll dig around to find the bug reporting channels.

Tuesday, September 16, 2014

Reject the Spoon! Effective Online Technology Training


(To skip the retrospective, jump to The Next Step in the Evolution of Online Training)

The Evolution of Technology Training

In the heady pre-internet days, I would dive into a new language or technology by surrounding myself with dictionary-sized books on the topic. I made sure to use books from different publishers so I would have explanations of a particular topic from different points of view. When one book would come up short, I could usually fill in the gaps with another.

In the mid 90s, boot-camp training exploded. Students pay hundreds to thousands of dollars to spend 3-5 days in intensive, hands-on sessions with a new concept and an experienced instructor. Quality crested, then dropped as training centers learned that they were more profitable using less experienced instructors and reusable mediocre course material. VHS and DVD training experienced a similar birth and devolution in the at-home training market.

Once the internet backbone was robust enough to support video, some enterprising business folk realized that presenting an entire course online would have the same net effect as an in-person course, minus the ability for students to ask questions of the instructor. Moreover, the content providers no longer needed expensive training facilities, and could pay their instructors for a single recorded engagement rather than multiple in-person sessions. Win-win.

Lynda.com rocketed to popularity by creating polished presentations using fresh content and a consistent course format. Early on, Lynda.com focused on the art side of technology, leaving room for other companies to bring the same formula to programming and technology infrastructure like PluralSight, Udemy, CodeAcademy and others. (Lynda has since broadened its catalog to include programming topics, but still focuses on the artistic side of technology.)

This is pretty much the state of online training today. Instructional videos are essentially desktop video capture interspersed with power-point slides - a technique that is clearly effective.


The Next Step in the Evolution of Online Training

Several months ago, I was looking for "instructor led online training". I wanted the benefits of on-demand learning with the extra benefit of having someone to talk to when I had questions. This led me to LearningLine (Link removed as the site is now defunct :/ ). While initially unimpressed, LearningLine employed two techniques that helped me learn far more from their classes than from any other online training class I have taken to date.

Let me stop right here to say: this is not going to be a whitewash review. LearingLine, an offshoot of 20-year classroom training veteran DevelopMentor, is a newcomer to the online training space and it shows. Frankly, their UI is awful. As a long time user of Lynda.com and PluralSight courses, I felt cheated when navigating through the cheap-looking LearningLine interface. It was my actual learning experience that prompted me to write this blog post. For me, the techniques they use make it worth suffering through LearningLine's inadequacies.

Real-World Resources

I took an ASP.NET MVC course through LearningLine. Unlike self-contained training courses I have taken in the past, LearningLine points you to industry sources on the internet like Microsoft Blogs, Microsoft reference pages, YouTube videos, Microsoft Evangelist websites...etc. At first, I thought that this was a cop-out; they were co-opting the work of others instead of creating new content. Then, I realized that these were resources that I could use on my own long after the course was done. For the ASP.NET class, I now had a list of well known industry bloggers to follow and Microsoft resources to use. Introducing this sort of external dependency does make LearningLine's courses fragile. I can only hope they have some sort of robust link checking system in place to ensure critical pieces of their course material don't disappear overnight.

Here is a Concept and How It Works. Now Figure This Out!

The second, most effective learning technique came at the end of the lesson. Each segment of the course starts out in the ordinary way: Present a concept, demonstrate it and close with an exercise for the student to work. The exercise, however, was not what I was expecting. In every course I have taken online, the exercise is identical to the presentation example with a few inputs changed. LearningLine, however, challenged me with an exercise that was related to the topic but required information not presented in the source material to complete.

Again, I scoffed at this, thinking LearningLine threw the course into the wild without a proper review to ensure the questions matched the lesson taught. (In fact, even as I write this, I truly hope this was intentional and not some fortuitous accident.)

While researching a question, I would invariably stumble across really cool related topics that would pull me down the rabbit hole. When Google would come up dry, I was inspired to hack out the answers through trial and error (and occasionally blog about the results.) I didn't realize it until much later, but once I returned to LearningLine to answer the question, I had piles of useful related material that helped me stitch the lessons together into a complete understanding of the topic.

It occurred to me that LearningLine was breaking its students out of the classroom and sticking them into real-world, work scenarios. When solving a coding problem in my job, the answer is never textbook. Often times, you will have experience with a vaguely related scenario, but won't know the best approach. You have to synthesize your answer from bits and pieces of information you find through research and trial and error.

Reject the Spoon!

More than just a dose of reality, not having answers spoon-fed inspires engagement. Instead of trying to figure out how to answer the question correctly, I found myself wanting to understand how to solve the problem. It's the difference between wanting to pass the course and wanting to understand what is being taught.

I hope Lynda.com, PluralSight, Udemy and others take note of this technique. Any teaching method that inspires interest and learning in the self-study education arena should be adopted universally. Speaking to LearningLine, I hope this technique was indeed intentional. Explaining the technique up front will clear up any ambiguity and will put your students in the right frame of mind at the outset.


Friday, August 15, 2014

ColdFusion 9.0.1 Bug: Function Executes Twice & Line Preceding Function Call Not Executed!?

This is just insane. I stumbled on a perfect storm of syntax that will lead to the craziest execution error.


First, write a simple function that accepts a struct and displays some text:
public void function SimpleFunction1 (struct incomingStruct) {
 WriteOutput("This is SimpleFunction1.");
}

Then, Call that function using a named, inline short-hand struct for the parameter:
SimpleFunction1(incomingStruct={item1="ok",item2="go"});

So far, this will work fine. However, wrap this function call in a try/catch block and not only will the function execute twice, but any instruction in the line preceding the function call will be ignored.
try {
    abort; //this line will never execute.
    SimpleFunction1(incomingStruct={item1="ok",item2="go"}); //this function will execute 2x
}
catch {
rethrow;
}

As a workaround, you can either remove the name from the argument in the function call, or make the argument a variable instead of an inline struct... I suppose you could also remove the try/catch block, but that's not really a good idea, is it?

Here is a complete example. Numbers One and Two should work fine. Number Three will fail.

try{
 //one
 WriteOutput("

SimpleFunction1 with inline struct

"); WriteOutput("WORKING!"); SimpleFunction1({item1="ok",item2="go"}); //two WriteOutput("

SimpleFunction1 with var substituted for inline struct

"); tempstruct ={item1="ok",item2="go"}; WriteOutput("WORKING!"); SimpleFunction1(incomingStruct=tempstruct); //three WriteOutput("

SimpleFunction1 inline shorthand struct and named parameter

"); WriteOutput("NOT WORKING!"); //this line will never execute SimpleFunction1(incomingStruct={item1="ok",item2="go"});//this function call is executed twice } catch (any e){ rethrow; } public void function SimpleFunction1 (struct incomingStruct) { WriteOutput("This is SimpleFunction1."); }

OUTPUT:
(I have to apologize. The script formatter I use strips out BR tags, so you'll need to add line breaks to the code above to make the output match the screen shot below.)
You may notice I use a "WriteOutput" command to demonstrate the line that fails to execute. You experienced devs out there are probably thinking "use CFFLUSH to clear the output buffer". I tried, but to no effect. Replace that WriteOutput with an Abort; It won't trigger.
Another thought was that perhaps something about the naming convention or the syntax was causing the try/catch to fire somehow. But, if you remove the try/catch, it executes fine.
If you try using a comment as the 'ignored line' it will skip that and ignore nearest preceding command.

8/15/2014 Update: I have reported this one to Adobe  https://bugbase.adobe.com/index.cfm?event=bug&id=3806526

8/15/2014 Update2: It appears to fail on CF 9.0.1, 9.0.2 and 10, but it is fixed and working properly in CF11!

Friday, July 18, 2014

ColdFusion sFTP and ZLIB

A client last week needed an automated sFTP push. I was able to reach the host, but was receiving the error: "Algorithm negotiation fail". Since Filezilla could connect fine, my first thought was that they were using a cipher that was too advanced for ColdFusion 9 to handle.


To get more information on the connection, I looked to Filezilla's debugging tools. Under [Settings], [Debug] I bumped the logging level to [2 - Info]


In the connection scroll, I saw something interesting. The ciphers were ordinary: SHA1 and Blowfish-128, but zlib stood out. Zlib compresses data at the transmission level, similar to gzip but patently different. ColdFusion doesn't handle gzip natively (though there are clever workarounds), so I suspected that CFFTP was having a problem with zlib.

I searched the internet for Java libraries that could sFTP with zlib enabled and the overwhelming majority of links pointed me to JCraft JSch. To my surprise, I discovered that ColdFusion 9 implements a modified version of JSch 1.41 library (C:\ColdFusion9\lib\jsch-0.1.41m.jar). The zlib functionality is not part of the core JSch, but is part of jzlib.jar (also available free from JCraft). I added jzlib.jar to the classpath and it worked like a charm. Here is my proof of concept code:

<cfscript>
FTPSERVER = "someserver.somewhere.com"; 
FTPPORT = "22"; 
FTPUSER = "someuser"; 
FTPPW = "somepassword"; 

jschObj = createobject('java',"com.jcraft.jsch.JSch"); 
jschSession = jschObj.getSession(FTPUSER, FTPSERVER); 
jschConfig = createObject("java","java.util.Properties"); 
jschConfig.put("StrictHostKeyChecking","no"); 
jschConfig.put("compression.s2c", "zlib,none"); //server to client
jschConfig.put("compression.c2s", "zlib,none"); //client to server
jschSession.setConfig(jschConfig); 
jschSession.setPort(FTPPORT); 
jschSession.setPassword(FTPPW); 
jschSession.connect(); 
jschChannel = jschSession.openChannel("sftp"); 
jschChannel.connect(); 
if (jschSession.isConnected()) { 
 WriteOutput("CONNECTED!");
} 
jschChannel.cd("/Test"); 
FileInputStream = createobject("java", "java.io.FileInputStream").init(expandPath("payload_test.txt"));
jschChannel.put(FileInputStream, "payload_test.txt"); 
WriteDump(jschChannel); 
jschChannel.disconnect(); 
jschSession.disconnect();
</cfscript>

The two commented lines enable zlib for upstream and downstream transmissions. This resolved our connection issues.