Blog Archives

Codecademy.com – the good – the bad and the ugly.

School holidays are a great time for kids to well, have a holiday.

But this year, this poor programmer has been besieged by some pupils of a local School. The poor things had been set the task of doing the javascript course on the http://www.codecademy.com site.

It’s a big course. I was a bit dubious about some of it’s teaching methods though. One poor lad was stuck for 24 hours when he wrote a lovely infinite loop. Instead of the Code Academy site working that out, it just let the code hang the browser. The lad thought his computer had gone wrong.

I also detected a large amount of copy and paste. There is  a help forum which is very copy and pastey and I wondered about how much javascript they were REALLY learning and how much of CTRL C/V was going on.

I noticed with loops (and this is a minor point) they did not point out it is better to do:-

var a=b.length;
for (var c=1;c<a;++c)

than

for (var c=1;c<b.length;++c)

Because the for next loop is reading the b.length over and over. Ok I’m being picky but.

I’m not sure about storing functions in variables either.

var x=function() {
 console.log ("Hello");
};

as opposed:-

function x() {
console.log("Hello");
}

The first example is 97% slower running than the second. That’s a whole lot of slower. (Credits to this link)

I did like the way it taught object oriented programming and different notations although the student I worked through it with got very confused by it. But point taken it’s probably really good to learn all three things.

But my main bug bear was really that you would get code that would fail even though it was correct. In one exercise the output of J.R.R Tolkien vs J.R.R. Tolkien caused the code to fail. Technically the code was right so it should have passed. Yes, there was a slight difference in the output from what was required but I get the impression the test of whether the code works is not able to actually tell if the code is right but only what output is given under certain circumstances, it’s like JUnit testing where you write a test in java that really doesn’t actually test the code but just the output. Although the student didn’t notice at one point I did manage to fool the tester into thinking I’d written correct code just by “console.logging” what I thought it wanted to hear.

Overall it’s a very good concept, the site is laid out well and nice to use. *BUT* I think it’s inappropriate to throw it at a complete new programmer (who is 14) without much help. At worst it teaches you to Copy and Paste. At best you actually learn to code.

Lastly I wondered why thee was no interaction with the web itself, Javascript is essentially a web technology but there was no effort to give any more output than “console.log”. Sure, you learn javascript but all you can do is put up input boxes and output to a console. Maybe at the end they could have introduced web technologies like JQuery as  a pre-cursor to a javascript course.

Yup, it’s a great site, I love what it’s trying to do, but the big no no for me is the hanging when a student writes an infinite loop, it can’t be that hard to write a “code timeout” that says if it’s not executed within a set amount of time give the “infinite loop” warning. Also, some of those error messages just don’t really help!. At one point I was going to get the student to install NetBeans and paste the javascript in there as it would pretty much help them to get it right. All the students I worked with kept doing this:-

for (var i=0;i<k.length;++i) ;

they would get very confused about when you { or ( or [ rather than ;.

Anyway – we got there in the end. Perhaps I should become a teacher?

Advertisements

Writing out from a Java Server Faces table to a CSV….

Imagine if you will that you have a Java Server Faces table and you want to write it out to a CSV file.

1. Put The following code in your application bean.

public String OutputFromTableToCSV(
String sFilename,
TableRowGroup tableRowGroup1,
CachedRowSetDataProvider tblzonesDataProvider) {
// Create an ooutput text file
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try {
out = new FileOutputStream(sFilename
+".txt");

p=new PrintStream(out);
} catch (Exception e) {
return "Error, could not write to "+sFilename+".txt";
}

String sFields = "";
String sColumnTitle = "";
String sThisFieldName="";
List theseColumns = tableRowGroup1.getChildren();
// Columns
for (int i = 0; i < tableRowGroup1.getColumnCount(); ++i) {
sColumnTitle = sColumnTitle.concat(sColumnTitle);
sColumnTitle = sColumnTitle.concat(",");

// Get the SQL mapped fields
TableColumn thisComponant = (TableColumn) theseColumns.get(i);
try {
sColumnTitle = sColumnTitle.concat(
thisComponant.getHeaderText());
} catch (Exception e) {
}
}
if (!sColumnTitle.equals("")) {
sColumnTitle=sColumnTitle.substring(1); // Chop off initial ,
}
// Write it out
p.println(sColumnTitle);

RowKey[] TheseRows = tblzonesDataProvider.getAllRows();
for (int rowCount = 0; rowCount < TheseRows.length; ++rowCount) {
// Now do the rows
tblzonesDataProvider.setCursorRow(TheseRows[rowCount]);
for (int i = 0; i < tableRowGroup1.getColumnCount(); ++i) {
sFields = sFields.concat(sFields);
sFields = sFields.concat(",");
// Get the SQL mapped fields
TableColumn thisColumn = (TableColumn) theseColumns.get(i);
UIComponent thisChild=(UIComponent) thisColumn.getChildren().get(0);
// ",[#{currentRow.value['ZoneDescription']}]"
try {
sThisFieldName = thisChild.getValueExpression("text").toString();
sThisFieldName = sThisFieldName.substring(
sThisFieldName.indexOf("['") + 2);
sThisFieldName = sThisFieldName.substring(0,
sThisFieldName.indexOf("']"));

} catch (Exception e) {
}

// Add the field to the string to write out

sFields = sFields.concat(
tblzonesDataProvider.getValue(sThisFieldName).toString()
);

}
if (!sFields.equals("")) {
sFields = sFields.substring(1); // Chop off initial ,
}
p.println(sFields);

sFields="";
} // Next Row
// At this point we have to csv strings
// sfields and column title

// Now we have a comma delimited string called sresult in theory

p.close();
return "OK";
}

2. On the form with the JSF page.
a. Add the binding attribute to the tableRowGroup (in the left hand bottom pane).

Create an “Export to CSV file” button and in the event when clicked add this code:-

thefilename – the filename you want to write to
tableRowGroup1 – The table row group from your JSF page table
yourdataprovider – The data provider that feeds the JSF table

getApplicationBean1().OutputFromTableToCSV("thefilename", tableRowGroup1, yourDataProvider);