Wednesday, January 30, 2013

Using nvm to manage multiple active node.js versions

Today I had to install a different version of node.js on my laptop, because the program I wanted to try required a newer version than the one I had.

After googling a bit, I found nvm developed by Tim Caswell. It is a "simple bash script to manage multiple active node.js versions", (nvm stands for Node Version Manager). It helped me to quickly solve my problem.

I followed the installion instructions and in 3 steps I had the last version of node (v0.8.18) up and running.

This is what I did:

First I show you the very old version that was installed on my laptop for a previous pet project:
$ node --version
v0.5.11-pre
I cloned nvm repository from GitHub:
$ git clone git://github.com/creationix/nvm.git ~/.nvm
Initialized empty Git repository in /home/bscuser/.nvm/.git/
remote: Counting objects: 557, done.
remote: Compressing objects: 100% (327/327), done.
remote: Total 557 (delta 290), reused 471 (delta 219)
Receiving objects: 100% (557/557), 76.33 KiB | 8 KiB/s, done.
Resolving deltas: 100% (290/290), done.
And followed Tim's instructions by, first, sourcing the nvm.sh script
$ . ~/.nvm/nvm.sh
and then using nvm to install the current version of node
$ nvm install v0.8.18
######################################################################## 100,0%
Now using node v0.8.18
Once it finished, the node version was:
$ node --version
v0.8.18
As I plan to be using the program that needed this newer version of node for a while, I added the following lines to my .bashrc file:
. ~/.nvm/nvm.sh
nvm use v0.8.18
so I have the v0.8.18 version every time I open a console.

nvm has many other useful options that I plan to use. Thank you very much Tim for sharing it.

Tuesday, January 29, 2013

Monday, January 28, 2013

Articles and Posts read

Inspiring
Pure possibility
Big Problems, Little Solutions: For the New Occupy, Size Is Everything
Grace Hopper

Learning and software craftsmanship
Ruby Dependencies, Notifications, and Adjustments
Thoughts on "Arguments and Results", Part 1
Preferring objects over class methods in OOP
“The best minds of my generation are thinking about how to make people click ads”
The Year of the MOOC
Rise and Fall of the Third Normal Form
Quality is Not Job 0xff: There is No Good Enough
A Programmer’s Thanksgiving
Who's Afraid Of Legacy Code?
Debugging Heisenbugs
Some things I've learnt about programming
Programming, Motherfucker
Looking inside the black box
Seams: Some thoughts
Book Club: The Readability of Tests – Growing Object Oriented Software (Steve Freeman/Nat Pryce)
Book Club: Growing Object Oriented Software – Chapter 7 (Steve Freeman & Nat Pryce)
OO: Micro Types
Software Development and Lifelong Learning
Quotes of the day: Constraints on programming
airbnb: Our JavaScript Style Guide
Isn’t It Ironic?

Agile and Lean
Caso de estudio: Como Spotify organiza, de manera ágil, su departamento de desarrollo software

C++
Continuous Integration for C++ using Hudson

JavaScript
Testing JavaScript with PhantomJS

Entrepreneurship and Management
Preventers, Not Managers
Respect and the 40-hour work week
El arte de compartir: cómo conseguir que difundan tus contenidos en redes sociales
La deuda técnica. Todo el mundo debería saber que la mala calidad software al final se paga

Software Development in Spain
El drama de las pequeñas consultoras
La copia de la web del Senado hecha por un programador

Science
The paper was rejected, but do the readers care?
Blind Patient Reads Words Stimulated Directly Onto Retina: Neuroprosthetic Device Uses Implant to Project Visual Braille

Rebooting Spain
¿Dónde está el interruptor de la revolución industrial en España?
Modernizar España

Spain and Europe
Un investigador español agradece en un artículo científico al INEM la financiación recibida
El ataque alemán desahucia a España
El ministerio 'concede' las becas doctorales FPI tras cuatro meses de retraso

Wednesday, January 16, 2013

How to access the first element of an Json::Value array

To access the first element of an Json::Value array the following code may not work:
...
double x = point[0].asDouble()
...
Instead you have to write:
...
double x = point[0u].asDouble()
...
To find out why just check the following fragment of the Json::Value documentation:
const Value & operator[] (int index) const
Access an array element (zero based index)
You may need to say 'value[0u]' to get your compiler to distinguish this from the operator[] which takes a string.

Tuesday, January 15, 2013

How to move files or directories in Subversion without losing their history

Sometimes we need to move files or directories in our code to a better location.

So far, we were doing it using the Move option in Eclipse.

However, this was wrong, let's see why:

Our code is under Subversion, a version control system.
The way we were moving things was interpreted by Subversion as if we had deleted them and the added new ones. That means that all the history of the moved item is lost and that's not what we intended.

To keep the history of the item when moving it, we must use Subversion to move it.

This is how it's done from the console:
$ svn move source destiny
This command moves the file/directory in your working copy or in the repository keeping its history. It's equivalent to an svn copy followed by svn delete.
The only limitation is that you can only move files within a single repository and the destiny must be a working copy, that is, it must be already included in your repository (to know more).

So from now on, anytime we want to move files or directories in our code to a new location, we'll use svn move.

Sunday, January 13, 2013

Interesting Talk: "Sensible Testing"

I've just watched this interesting talk by Justin Leitgeb:
He talks about applying the CUPID principles to get a habitable and sensible set of tests.

Sunday, January 6, 2013

Interesting Talk: "Yay! Mocks!"

I've just watched a great talk by Corey Haines:
He shows how test doubles can help to highlight design problems. It also talks about what object orientation means.

Saturday, January 5, 2013

Embedding Python in C++: Hello World

This afternoon I've been "procrastiworking" (working on something that isn't mandatory to postpone doing something mandatory) a bit.
As a result I end up watching this talk by Michael Fötsch:
I just couldn't get to the end of the talk.
When I saw the "Hello world" example, I had to stop the video and try it myself.
This is the code snippet of the experiment:
#include <Python.h>
int main(int argc, char* argv[])
{
  Py_Initialize();
  PyRun_SimpleString(
    "print 'Hello world!'"
  );
  Py_Finalize();
  return 0;
}
This is how I compiled it:
$ g++ -o helloWorld helloWorld.cpp -I/usr/include/python2.6/ -lpython2.6
And this is the result of running it:
$ ./helloWorld
Hello world!
Nice!!
This is just the tip of the iceberg. To learn more about embedding Python in C++ watch Michael's talk.
He also has more advanced material in his blog:
There's also more information in Python's Documentation:
and in this Jun Du's tutorial:
Now you also have material to procrastiwork for a while.
Have fun!

Interesting Talk: "Refactoring from Good to Great"

I've just watched a great talk by Ben Orenstein:
It's a live refactoring session. It explains by example several refactorings and talks about the reasons that make them big wins.

Interesting Talk: "Thinking in Objects"

I've just watched this talk by Josh Susser about object oriented programming:
For me it's very interesting to see how a dynamic language like Ruby approaches OOP.

Traversing members of a JsonCpp's Json::Value object (2)

This morning I learned how to traverse the members of a Json::Value object using its iterators.
This is the same example I used in my previous post, but using iterators instead of the list of member names:
#include <iostream>
#include "jsoncpp/json.h"

using namespace std;

int main(int argc, char **argv)
{
  Json::Value change;
  Json::Value minParameters;
  Json::Value minParametersAnm;
 
  minParameters["MinimumRMS"] = 0.2;
  minParameters["sgbUpdated"] = true;
  change["Minimizer"] = minParameters;
  minParametersAnm["MinimumRMS"] = 0.5;
  minParametersAnm["sgbUpdated"] = false;
  change["Minimizer::ANM"] = minParametersAnm;
 
  cout<<"Traverse members of: "<<endl
    <<"\"change\":"<<endl
    <<change.toStyledString()<<endl<<endl;
 
  Json::Value::iterator it = change.begin();
 
  cout<<"List of members:"<<endl;
  for(Json::Value::iterator it = change.begin(); it !=change.end(); ++it)
  {
    Json::Value key = it.key();
    Json::Value value = (*it);
    
    cout<<"Key: "<<key.toStyledString();
    cout<<"Value: "<<value.toStyledString();
  }
  return 0;
}
Note the use of the key() method to get the key of the Json::Value from the iterator. Also note how we need to dereferentiate the iterator to get the Json::Value from the iterator.

Ok, so, I compiled it doing:
 
g++ -o test_iterator_members test_iterator_members.cpp -ljson_linux-gcc-4.4.5_libmt
And I executed it and got this result:
 
Traverse members of: 
"change":
{
   "Minimizer" : {
      "MinimumRMS" : 0.20,
      "sgbUpdated" : true
   },
   "Minimizer::ANM" : {
      "MinimumRMS" : 0.50,
      "sgbUpdated" : false
   }
}


List of members:
Key: "Minimizer"
Value: {
   "MinimumRMS" : 0.20,
   "sgbUpdated" : true
}
Key: "Minimizer::ANM"
Value: {
   "MinimumRMS" : 0.50,
   "sgbUpdated" : false
}

Friday, January 4, 2013

Traversing members of a JsonCpp's Json::Value object (1)

Today I needed to traverse the members of a Json::Value object.

After having a quick look at the documentation, I found the getMemberNames method, which returns a list with the member names, and did this little experiment:
#include <iostream>
#include "jsoncpp/json.h"

using namespace std;

int main(int argc, char **argv)
{
  Json::Value change;
  Json::Value minParameters;
  Json::Value minParametersAnm;
 
  minParameters["MinimumRMS"] = 0.2;
  minParameters["sgbUpdated"] = true;
  change["Minimizer"] = minParameters;
  minParametersAnm["MinimumRMS"] = 0.5;
  minParametersAnm["sgbUpdated"] = false;
  change["Minimizer::ANM"] = minParametersAnm;
 
  Json::Value::Members memberNames = change.getMemberNames();

  cout<<"Traverse members of: "<<endl
    <<"\"change\":"<<endl
    <<change.toStyledString()<<endl<<endl;
 
  cout<<"List of members:"<<endl;
  for(unsigned int i=0; i<memberNames.size(); ++i)
  {
    string memberName = memberNames[i];
    Json::Value value = change[memberName];
    cout<<"Key: "<<memberName<<endl;
    cout<<"Value: "<<value.toStyledString()<<endl;
  }
  return 0;
}
I compiled it:
 
g++ -o test_traverse_members test_traverse_members.cpp -ljson_linux-gcc-4.4.5_libmt
And this is what I got:
 
$ ./test_traverse_members 
Traverse members of: 
"change":
{
   "Minimizer" : {
      "MinimumRMS" : 0.20,
      "sgbUpdated" : true
   },
   "Minimizer::ANM" : {
      "MinimumRMS" : 0.50,
      "sgbUpdated" : false
   }
}


List of members:
Key: Minimizer
Value: {
   "MinimumRMS" : 0.20,
   "sgbUpdated" : true
}

Key: Minimizer::ANM
Value: {
   "MinimumRMS" : 0.50,
   "sgbUpdated" : false
}
We could get the same result using Json::value's iterators.
This will be material for a future post, though.

Wednesday, January 2, 2013

How to create a nested JsonCpp's Json::Value object

I needed to create a nested JsonCpp's Json::Value object.

This task is very easy using Json::Value's implicit constructors.

In the following example I created on the fly the configuration for a Minimizer object I wanted to include in a simulation:
#include <iostream>
#include "jsoncpp/json.h"

using namespace std;

int main(int argc, char **argv)
{
 // Creation
 Json::Value minimizer;
 Json::Value minParameters;
 minParameters["MinimumRMS"] = 0.2;
 minParameters["sgbUpdated"] = true;
 minimizer["Minimizer"]["parameters"] = minParameters;
 minimizer["algorithm"] = "TruncatedNewton";
 
 // Output to see the result
 cout<<"creating nested Json::Value Example pretty print: "
     <<endl<<minimizer.toStyledString()
     <<endl;
 
 return 0;
}
Compile it doing:
 
$ g++ -o test_create_json test_create_json.cpp -ljson_linux-gcc-4.4.5_libmt
And you'll get:
 
$ ./test_create_json 
creating nested Json::Value Example pretty print: 
{
   "Minimizer" : {
      "parameters" : {
         "MinimumRMS" : 0.20,
         "sgbUpdated" : true
      }
   },
   "algorithm" : "TruncatedNewton"
}