Monday, December 15, 2014

SSDT get your DB back in sync with your project

So I've been using SQL Server Data Tools (SSDT) in visual studio to manage database changes.  I'm the only one really using it on my team right now.  I had been working on a branch project in isolation for a while and the trunk database schema kept changing.

Today I am working in trunk (HEAD/master) again.  So I need to switch to the trunk branch of my SSDT solution and get it back in sync with everyone else.  I was amazed at how easy this was.

Simply do a database schema compare using a database with the most recent schema as the source and the SSDT project as the target.  Run the update to make changes to all the SQL files in the target (the project).  Then you can commit all those changes to your source control and VOILA!  - Your SSDT project is back in sync.

Now I can develop my own schema changes and generate the change scripts easily.  Sweet!

Saturday, December 6, 2014

Print from an iPhone to an HP Photosmart plus all in one

JI used to be able to print from my iPhone 4S.  But I don't print much and I have since upgraded my router and upgraded to an iPhone 6. 

I couldn't remember how I set it up a few years ago.  Anyway, it's really easy. This printer does support a way of emailing documents to the printer which is one way to do it. 

The other way is to go to the App Store on your iPhone. Search for HP ePrint. Download and install the app (it's free). If your printer is not detected right away, restart it by simply powering it off powering it back on. You may also have to restart your wireless router.  These resetting steps are a pain, but they really work. Next add your wireless printer. Now you can print from any app on your iPhone.

Wednesday, December 3, 2014

HTML CSS Form Layout

I just created an awesome plunker showing a form with different layout markup and styling.  I have one for percent widths, 2 using floats and one using a table.

Tuesday, December 2, 2014

touchend javasccript event not working

So I came to find out today that I think the touchend event has a pageX and pageY populated in Safari, but not in the Android browser.

Here's the code.

if (canvas.addEventListener) {
canvas.addEventListener("mousedown", function(e) {
   click(e.pageX, e.pageY);
}, false);
canvas.addEventListener("touchstart", function(e) {
   click(e.touches[0].pageX, e.touches[0].pageY);
}, false);

Originally, I was using e.pageX and e.pageY and it worked on chrome (desktop) and mobile safari, but it was failing on a Samsung device.  So, reading about the touchend event on Mozilla, I found that there is a touches array with the list of touches.  That was populated on a mobile.

Saturday, November 15, 2014

Saturday UTF8 Reading

Reading about UTF8 on a Saturday afternoon?  Yeah I didn't expect that either. But I was having trouble yesterday at work sending html email. I was getting some mojibake. That's when characters are encoded one way and interpreted in another way. It looks like some of the characters turn into garbage.  For example in Microsoft outlook you get a black diamond with a question mark for some special characters.

I'm learning that UTF8 is really a dominant standard mostly because it can be used to encode all Unicode characters as well as support good old ASCII. ASCII is UTF8. UTF8 can do these tricks because it can encode characters with a variable number of bytes. For common and ASCII characters it uses just one.  For special characters it might use two or more bytes. 

Also UTF-8 is being used more and more for all Internet related documents and email clients all support HTML encoded in UTF-8. Read more about it at Wikipedia.  

This is a good read too from email on acid.

I learned that most email clients IGNORE the content type in the HTML but instead use the content type from the email header.  That was certainly tricking me up yesterday. 

The problem was with File.ReadAllText.  The solution was to use the correct encoding.

File.ReadAllText(tempFilePath, System.Text.Encoding.GetEncoding(1252))

See also this Stack Overflow question.

Friday, November 7, 2014

Nhibernate LINQ query with null Where condition

So today I was trying to profile a query and for the life of me I could not see the query in the profiler.  Simple queries where working fine:

Session.Get<User>(123); // I could see the SQL in the profiler.

Session.Query<User>().Where(u => u.thing == someVar) // I was not seeing this in the profiler.

Come to find out, "someVar" was null.  OMG.  Wow.  It's kind of like trying to run this SQL

select u.*
from user
where 1=0

There's pretty much no point in running that query and I guess LINQ to NHibernate thought the same thing!

This happened because someVar was coming from config.  The config file for my unit test was missing the variable, so it was null.  Fun times.

OK, so I fixed that problem and it turned out not to be it.  My queries from the unit test kept returning empty IEnumerables.  So I come to find out that the Fluent Nhibernate Models I am using are special models that are not in the same namespace as the majority of our models.  I had to add the models to the NHibernate Config code and NOW the LINQ queries are working.

Monday, October 20, 2014

AngularJs vs KnockoutJs

I've used AngularJs before.  It's pretty cool.  I just watched a presentation on KnockoutJs.  Knockout is also pretty cool in that it has 2 way data binding, so when the view model changes, the view changes and vice version - the view and view model are always in sync.

I have not used either of these frameworks extensively, but I will say that with Knockout, you have to explicitly make all of your data into observables.  What's worse is that in a very common example of an array of objects, you have to make the array observable as well as all of the properties of the objects in the array.  Likewise when you want to POST the data, you have to convert it from a Knockout observable object/array to a plain old javascript object/array using ko.toJSON(koObject).

With angular, all you have to do is put data into your $scope and it is immediately observable.  There is no encapsulating it with observables, nor converting it back to a plain old javascript object when you need to POST to a web service.

Another thing I don't quite like about Knockout is that is has no way to "commit or undo" a change to the view model.  For example, if I change a text field in the view and then I hit cancel to "undo" the change, I can't - the view model has already been changed.  You have to manage that yourself.  AngularJS has this same issue too.

Some more interesting tidbits... Incrementing is not as fun as in angular.  Since obserables are functions you have to increment like this:

this.count(this.count() + 1);

In angular you can simply do:


Also I found out that using the ko array utilities, you lose access to the "this" pointer.  I found out the hard way that it is actuallly a good thing.  Why?  You should not be editing your view model in a loop!  You will be updating the view N times, which is not very performant!

Monday, September 29, 2014

App Icon drawn with HTML5 canvas

It was a busy hacker weekend thanks to JsAny on my iPhone 6.  I developed a Facebook style comment stream (in the previous blog post) and this app icon.  I looked for a free vector drawing app on the iPhone (I don't use it that much), but I couldn't find one that worked well.  So I honed my skills with the drawing API on the HTML5 canvas.  Check it out.

The plunker for the App Icon

Facebook style comment stream in AngularJS

I built a little prototype of a comment stream in AngularJS.  It has a similar look and feel to Facebook posting streams, but it is a comment stream instead.  In the application I'm working on now, we don't have postings and comments on postings.... just comments.  Anyhow, this was really easy to do with Angular and is very few lines of code.  It was a pretty fun little project.

Here's the plunker: Facebook style comment stream

Tuesday, June 24, 2014

JSON to C# POCO - again!

This is really frustrating.  There are so many (bad) ways to deserialize JSON in C#.

In my previous post, we discovered that dynamic types really are not a good way to do it.

Now I have discovered that new JavaScriptSerializer().Deserialize<T>(json) will not convert datetimes properly.  Json.Decode<T>(json) has the same issue.


Today I tried ReadAsAsync<T>.  I originally had datetime conversion issues, but it was because my C# POCO did not have the correct property names.  If the json is not from ASP.NET and the JSON keys have underscores, your POCO property names must have underscores also.

For reference:

2 SO Questions that are the most popular:

2 Asp.NET articles that are helpful for ReadAsAsync

Monday, June 23, 2014

JSON to C# POCO - don't use dynamic types

Hi all,

Wow there are about a million questions and answers regarding this on stack overflow, "How do you convert JSON to a C# Object?".  There are a ton of different ways to do it.  One SO Q/A I looked at was this:

One of the answers suggests using a dynamic type, which sounds awesome.  Using a simple one liner, you can parse the JSON into an object.

dynamic user = Json.Decode(json);

Then you can do this.

var name =;

GREAT!  But the problem is, the name field is not type checked at compile time.  For example, this will also compile.

var name = user.naem;  // Compiles but generates a run-time error!

This leads me to the conclusion that dynamic types are really no better than a dictionary.  For example, you could convert the JSON to a dictionary and have the same problem:

var name = user["name"]; // Compiles and runs!
var name = user["naem"]; // Compiles and has a run-time error.

I am going to NOT use dynamic types as much as possible.

Tuesday, June 10, 2014

CSS Page Transitions

Check out this cool page transition animation I made to slide pages in from the right and from the left.

SO Answer


Thursday, May 22, 2014

C# Async HttpWebRequest Console POC

So I have to connect to an API and get some JSON.  I researched the WebRequest class.  I found that there is an option in .NET to use Async programming with this class.  So I did some research to find out that there are many ways to do the async.  The easiest is using the new keywords async and await.  See Asynchronous Programming with Async and Await (C# and Visual Basic).  

There is a full working example in the doc for GetResponse Method which is not async.

This is the working Proof of Concept (POC) that I came up with.


Friday, April 18, 2014

Enough technologies for one week

In the last week on the job at IBM, I have been transitioning my projects and training some projects (which are not really even mine.)  Here's what technologies I've used (It's a bit nutty).

  1. Java, Jetty, Jersey, Guice, JAXB, Hibernate, Eclipse, git, MySQL
  2. Perl, Ruby on Rails, MySQL, javascript (of course)
  3. Angular, Node, sqlite, git
  4. WebSphere, dojo, java, junit, maven, ant
  5. Maybe throw in some bash, ksh, DB2 too.
The crazy thing is I am probably missing something.

Tuesday, April 1, 2014

Connecting to Windows Azure from a Linux machine

Connecting to Windows Azure from a Linux machine is not as easy as it may seem and there are many options.  Here are some of the options:

  1. tsclient.  Just run it from the command line.  Or you can use rdesktop.  Here's an example: rdesktop  Make sure you use the correct external port which is listed in Azure in the info about your VM.  I could not get rdesktop to work for me.
  2. remmina.  The tsclient is actually obsolete.  Remmina takes up where tsclient left off.
  3. azure-cli.  This is a tool you can install with npm.  It's actuall not a terminal services/RDP client.  It gives you a command line tool to administer your VM.
  4. Have an iPad or iPhone with iOS 6 or greater?  You can download the Remote Desktop app from the App Store.
  5. KVM.  Or any other virtual machine that you can run on your Linux machine.  Then use the normal Windows version of Remote Desktop to access your VM.
In all cases you want to use the user and password you set up when you created the VM.  There is also a default DOMAIN.

Saturday, March 29, 2014

What's up lately

1. I've been getting ugly and mini with gulp.js.

2. Cool REST API project at work with Jetty, Jersey, Guice (DI), and JAXB. A little mvn too of course. 

3. Some C# and Groovy explorartion gists. 

4. Implemented a Clock object in JS. It's a gist, but it's easy to throw into a fiddle. 

The gists are here:

Wednesday, March 19, 2014

Tar install

Here is a sweet command for all of your tar installing needs.

  • sudo tar --directory=/opt -xv f <file>.tar.[bz2|gz]
  • add the directory to your path: export PATH=$PATH:/opt/[package_name]/bin


Monday, March 17, 2014

Comparing Embed-able Java HTTP/REST Servers

I have a project where I want to create a REST service in Java, but embed the HTTP and REST server in the application itself.  Why embedded?  Because we can't install a servlet container on any of the machines we need to run on.  I don't want to serve web pages, just REST services.  Maybe in the future I would like to support web sockets, but that is just a "nice to have".  This is not a public REST API, so we don't need to worry about firewalls.

There are a lot of embed-able java HTTP/REST servers out there.  The good thing is that REST framework part of it seems like a no brainer; Use the reference implementation, Jersey.  We already have a project using Jersey, so it's an easy choice.

But the embed-able HTTP server is not an easy choice.  Here is a list with some features as of 2014-03-17. Options that support NIO and an async model would be nice.

I wish I could offer a conclusion, but you will probably need to research these to meet your own needs.  As far as popularity I think it boils down to Jetty and Simple.

We chose Jetty.  Here are the steps to get your hello world Jetty/Jersey embedded server up and running:

The hard way:
  1. Embed Jetty
  2. Using Jersey with embedded Jetty HTTP Server.  Search for section Jetty HTTP Server.
The easy way:

Saturday, March 15, 2014

Private variables in Javascript classes

So, in most classical languages, you can declare a private variable in a class.  No big deal.  You can declare a private function too.  Private is good as well all know.  You can't really have encapsulation without private/protected variables.  Encapsulation is maybe the most important part of Classical Object Oriented Design.

In javascript, you can create a private variable in a constructor as a local var like this:

Person = function(age) {
var _age = age; this.getAge = function() { return _age; } }; var p = new Person(4); p._age; // undefined p.getAge(); // 4

Cool!  I have encapsulation, but one thing we know about javascript is that functions declared withing the constructor are duplicated for each object.  That's not good.  So if I create another person, it has its own getAge function!


Person {getAgefunction}
  1. getAgefunction () { return _age; }
  2. __proto__Object

var q = new Person(5);

q.getAge = function() { 2; };
function () { 2; }

Person {getAgefunction}
  1. getAgefunction () { 2; }
  2. __proto__Object

Protoype functions solve the problem of function duplication among many objects.  BUT only functions that are declared in the same scope as the local variable will have access to that variable (via a closure).  So prototype functions cannot access private variables.

So I got to thinking.  Coffeescript implements classes.  How does it implement private variables?  Answer.  It does not .  You put an underscore in front of variables and functions you want to "act" as private and then hope that developers are smart enough not to access them directly.

But there is a way to implement private functions!  See this answer on stack overflow.
The comments are interesting too.  It is impossible to unit test private functions, but then maybe you just need to implement a public wrapper around the private one.

Reading this post at,the private variable is not an instance variable, but a class level private variable.  That is almost never what we are looking for unless we are implementing a singleton.  He comes up with a pretty reasonable conclusion.  Don't implement private variables and functions.  It's not worth the trouble.  I am a little let-down by that, but I guess we just move on and keep it simple.

Friday, March 7, 2014

Monday, February 3, 2014

Working on your programmer 'game'. Calc pi.

OK.  I'm feeling pretty good right now.  I was looking at stackoverflow and saw a question by Jon Skeet,  "What is your most controversial Programming opinion".  Someone posted that developers should know how to code and gave an example of an interview question to calculate pi.  Wow.  I have never seen the calc to generate PI before, so I gave it a whirl.  Here's what I came up with in javascript:

var pi = function() { var a = 0; var b=true; for (var i = 1; i< 10000000; i+=2) { if (b) a+= 1/i; else a-=1/i; b=!b; } return 4 * a; }

I only did that in a few minutes, so I might code it differently if I wanted to do more iterations, but it worked for now.
And I also did the area calc for fun too:

var area = function(r) { return Math.PI * r * r; };

Monday, January 13, 2014

Jasmine parameterized test

I built a jasmine test to test the output of a single command (my logax command).  Then I wanted to test various input file formats with the same exact test code.  I like to keep it DRY, so I created a parameterized jasmine test.  The example above in github is for coffeescript, but it can easily be adapted to javascript.

Here's my JavaScript version: