Lazycoder

18Mar/1047

Getting started with node.js on Windows

The title is somewhat misleading. As of right now, node.js doesn’t run on Windows. You have to run it on some kind of *nix/BSD based system. But there is a somewhat low footprint way to run it and play around with it on your Windows box.

Step 1 – Download and install VirtualBox orVMWare Player. I chose VirtualBox. It’s free, and supports 64-bit guests.

Step 2 – Download The Turnkey Linux core appliance and unzip it somewhere. This handy little virtual machine is based on Ubuntu and give you a basic command line environment with networking.

Step 3 – Import the Turnkey core appliance into VirtualBox.

turnkey_step_1

turnkey_step_2

Choose the .ovf file in the Turnkey directory you unzipped earlier.

turnkey_step_4

Click next and review the settings, making any changes as you see fit. The defaults should work fine. Then click import.

turnkey_step_3

Once Virtualbox finishes importing the virtual machine, you can start it up.

Assuming your network is configured correctly, the virtual machine will grab an IP from your DHCP server and be ready to go.

turnkey_startup

Step 4 – At this point you can either SSH into the virtual machine or you can connect using the web shell at the address indicated in the startup screen. Initially you can connect using as the root account with no password. You are almost ready to start installing node.js. First type “apt-get update” at the command line to make sure you have all the latest package information.

Step 5 – Install the developer tools you need to get and build node.js. Node.js isn’t packaged as a binary, you have to build it from source. Luckily it includes it’s dependencies and is pretty easy to build. But first we need to get a compiler. Type “apt-get install build-essential” and hit return. A lot of text will fly past, if it asks you if you want to go ahead press “y”.

Step 6 – Install Git. Now you’ve got a compiler installed, we have to install git so we can fetch node.js from the repository. At the command prompt type “apt-get install git”. Once that is complete, type “apt-get install git-core”.

Step 7 – Clone the node.js Git repository. If you want to put node.js is a specific directory, go ahead and make it then “cd” into the new directory. At the command prompt, type “git clone git://github.com/joyent/node.git“.

Step 8 – configure the source for building. type “cd node” and change into the node directory that Git created. Type “./configure”. You may see a few “fail” messages. Don’t worry about them.

Step 9 – Build node.js. Type” “make” at the command prompt. Get a sandwich or a nice cool drink. It doesn’t take very long, but it’s not very exciting unless the Matrix screensaver is your favorite screen saver.

Step 10 – Install node.js and start build applications. Type “make install” once the build is complete. Once that is complete,you can type “node” at the command prompt and you should see the standard help information fly by.

Building a node module or application is beyond the scope of this short tutorial. I suggest reading up at the Node.js site.

7Jan/102

Goals for 2010

So I’ve been mulling around what I want to do, at least in terms of my development skills and community type stuff, in 2010 (The year we make contact). I figure if I blog them up here, I can refer back to them and check on my progress at the end of the year.

  1. Help out with the MVC Turbine docs
  2. Finish my Ringbinder project and release it
  3. Write a simple little app for use at home in either Clojure or Smalltalk
  4. Continue to learn Python
  5. Write a small app that runs on the Google App Engine
  6. Write a blog post, either here or on my work blog, at least once a week
  7. Redesign this blog
  8. Work on and launch the coderdads.info site I’ve had in mind for a while

I’m sure I’ll come up with more as the year progresses and I, hopefully, finish up these tasks.

23Nov/099

Pros don’t make do

I had a locksmith come out to replace a doorknob that had gone bad. It had gradually gotten hard and harder to turn and finally the bolt got stuck inside the door. Which meant the only thing keeping the door shut was the deadbolt. I had replaced doorknobs before, but this one had a non-standard length and strike plate so I didn’t want to mess with it. The thing that struck me while watching him work is that he never had to try and force a tool to do something it wasn’t designed to do. He always had the correct tool and it worked exactly the way he expected it to. mikeg-longtermfixHe never tried to screw in a Phillips head screw using a knife blade or a flat head screwdriver. He didn’t have to duct tape anything

I have a small battery powered Dremel rotary tool. It works for very small jobs. There have been a couple of times when I’ve had to push it way beyond it’s limit, including once when I needed to make the opening on a door jam for a strike plate a little wider. It took me forever. I had to charge the battery twice and broke a bit before I finally had enough room, but just barely enough room. Contrast that with the locksmith who had to do the exact same thing. He pulled out a much larger Dremel that plugged into the wall. Where it took me almost 45 minutes to rout out the door jam, he took about 2 minutes with his much more powerful tool. I could have stopped, went to the store, and bought a more powerful tool and gotten the job done much faster. But I thought that this tool would do the trick and going to the store would have cost me some resources, it might have taken a little longer. I’d have to learn how to use the new tool. Get a new bit. But overall, it probably would have saved me some time in the future and probably would have saved me some time on that project. I could definitely find other uses for the new tool.

Think about that the next time you reject a new programming tool because you think it might take too long to learn or it’s different. Instead of doing the hacky way you KNOW will work, check to see if there is a more elegant or permanent solution.  Because if you aren’t careful, your cheap and easy workaround might end up sticking around in the project long enough for it to become a problem to you.

Filed under: General 9 Comments
17Nov/0913

We need a language for programmers

Here is a great rant about “modern” programming languages.

And so for 20 years now these folks —
*the* shining lights, in many ways, of “practical” programming
language, operating systems, and general systems research — have
continued to fail to “get” the fundamental practical needs of everyday
programmers working in The Real World. “Go” is just another language
written first for the compiler and only secondarily for the programmer
— and stuck in a 70s mindset* about the relationship of that
programmer to the (digital) world within which they live and work!
(But hey, it compiles fast! Which is, of course, THE problem that
really needs addressing.)

He then goes on to talk about some aspects of languages that frustrate him.

J.H.C, folks, it’s nearly 2010. Let’s get a few things straight:

– most programming involves schlepping a few but complex data types
between different string representations
– programmers have become plumbers and documentation-archaeologists
mostly, which is sad and uninteresting
– programming languages are for *programmers* — not compilers and
compiler-writers
– until you make the everyday, “simple” things simple, it will
continue to be a dark art practiced by fewer and fewer
– any language that makes you explicitly import an IO module to
read a file or stdin is fucked
– declarations are a pointless anachronism (same for explicit
memory management)
– if I have to understand category theory to write a program that
does IO, IT IS A NON STARTER!
– less stupid line-noise syntax and punctuation, people

I don’t understand why the need to compile fast is such a big deal. If your programming language makes you write so much code that it takes more than a few seconds to compile, you should probably look at using a different language. Here are some things that I’d like to see in a programming language that is designed for programmers rather than compilers or computers.

1) No typing – That’s not to say that I can’t declare my own types, classes, objects, etc… But that calls to objects should be type independent. (e.g. messaging and dynamic dispatch)

2) Built in unit testing – I should be able to declare contracts on the methods and pass/fail criteria. The tests should be run as part of the compile step or interpretation (if the language is compiled or interpreted)

3) Jellybeans

4) Automatic inclusion of libraries based on path and usage. – I don’t mind having to pull in a library to do file I/O or consume some other kind of data like ATOM or JSON. But at compile time, if I have referenced any of the objects contained in the library, all of the references to the library should be handled for me automatically. Any name collisions should be compiler warnings so I can specify which library to use by in my code or make the reference explicit in some other way. I like Pythons path based module convention, the language should just search the child directories for a library if the library isn’t already included in the base language pack.

5) Anytime a design pattern emerges, it should be re-factored into the language itself.

6) It should be a pocket language, have a set of keywords and syntax that is small and compact but still understandable. JavaScript and C are good examples of pocket languages. It’s easy to pick up the syntax.

I’m sure there’s more that I would add. What kinds of features would you like to see in a language oriented towards programmers rather than compilers?

Filed under: General, Languages 13 Comments
3Nov/0910

Dependency Injection and Inversion of Control are not rocket surgery

I see a lot of people talking about how “advanced” techniques like dependency injection and inversion of control are and how their team won’t understand either technique.

 

Folks, this isn’t hard. In fact, both of these things are so simple I simply call it “using the programming language”.

 

Let’s look at dependency injection.

C#:
  1. public class MyClass
  2.     {
  3.         private DataTableReader _reader;
  4.         public MyClass()
  5.         {
  6.             _reader = new DataTableReader(new DataTable());
  7.         }
  8.         private void DoStuffWithTheReader()
  9.         {
  10.             while (_reader.Read())
  11.             {
  12.                 //do fun stuff with the reader.
  13.             }
  14.         }
  15.     }

See the reader variable? That's a dependency. You have to have it in there to do fun things later on. But we have to create it ourselves, which is one more thing that we have to do in our class constructor. In reality we also have to populate the DataTable. So what if we make the reader variable constructor parameter so that another class can do the work of creating the DataTable and the reader?

C#:
  1. public class MyClass
  2.     {
  3.         private DataTableReader _reader;
  4.         public MyClass(DataTableReader dataTableReader)
  5.         {
  6.             _reader = dataTableReader;
  7.         }
  8.         private void DoStuffWithTheReader()
  9.         {
  10.             while (_reader.Read())
  11.             {
  12.                 //do fun stuff with the reader.
  13.             }
  14.         }
  15.     }

There, that's better. Now our class doesn't have to worry about creating the reader and the DataTable. This, in a nutshell, is dependency injection. It's not very complicated is it? We've made our class construction a little simpler and if we want to unit test this, we don't have to do any complicated mocking, we can just new up our own DataTableReader instance and populate it with whatever test data we want. If you run into any funny looking data in the DoStuffWithTheReader method, you know that you don't have to look in this class at all to see where the funny data is coming from, only in whatever method is creating this class and passing in the DataTableReader.

Now is there anyone who thinks that developers on their team would have trouble understanding passing in a parameter? Should they really be a developer if they do?
Honestly, it's not that hard.

Ok, so let's look at inversion of control. The original definition of inversion of control I read was by Martin Fowler:

There's a big difference now in the flow of control between these programs - in particular the control of when the process_name and process_quest methods are called. In the command line form I control when these methods are called, but in the window example I don't. Instead I hand control over to the windowing system (with the Tk.mainloop command). It then decides when to call my methods, based on the bindings I made when creating the form. The control is inverted - it calls me rather me calling the framework. This phenomenon is Inversion of Control (also known as the Hollywood Principle - "Don't call us, we'll call you").

If you look at the previous example, you'll see that we have already inverted the control a bit just by using dependency injection. But the class still has a degree of control over WHAT concrete object is created, in this case a DataTableReader. What if we need to switch over to a SqlDataReader or an OleDbDataReader? Well, we could create three other classes that all take the specific type of data reader we might want to use. But that's a bad idea, you end up with the same logic spread all over the place. Instead we can use the IDataReader interface that all three classes implement.

C#:
  1. public class MyClass
  2.     {
  3.         private IDataReader _reader;
  4.  
  5.         public MyClass(IDataReader dataTableReader)
  6.         {
  7.             _reader = dataTableReader;
  8.         }
  9.         private void DoStuffWithTheReader()
  10.         {
  11.             while (_reader.Read())
  12.             {
  13.                 //do fun stuff with the reader.
  14.             }
  15.         }
  16.     }

Now our class not only doesn't have to worry about creating the DataTableReader, it doesn't even really care if it gets a DataTableReader at all. All it cares about is that the reader is referencing something that implements the IDataReader interface. This is a type of inversion of control. Most of the time people get confused between inversion of control and a container that enables inversion of control and dependency injection (like Ninject, StructureMap or Unity). You don't have to use a container to utilize these two techniques, it just makes it a little easier.

update:Also check out this great post "It’s all about the delivery"

update: I mistakingly thought it was hard to do D.I. in Python due to the inheritance mechanism in Python. Turns out, it's just as easy.

PYTHON:
  1. class MyClass():
  2.  
  3.     def __init__(self, dataTableReader):
  4.         self._reader = dataTableReader
  5.        
  6.     def DoStuffWithTheReader(self):
  7.         while(self._reader.Read()):
  8.             #Do Fun Stuff with the reader.
  9.             print(self._reader.item)

26Oct/091

Great Presentaton about software development

Hopefully someone will post video of the presenter giving the presentation to fill in the gaps.

Filed under: General 1 Comment