Using the Immediate Window on Visual Studio

This is a quick tip for visual studio users. It may be that most developers using VS already know this, but I only recently found about the immediate window. The immediate window is useful as a complement to visual C++ integrated debugger, as it allows one to evaluate small C++ (or C#) expressions during debugging. It is the closer you get from having a REPL in C++, using visual studio technology (of course, for people used to Python, Ruby, or Lisp, saying this is almost an insult, but that is what can be done).

To access the immediate window in debug mode one has the following options:

  • Type Control-Alt-I (the fastest way)
  • In the command window, type imme
  • Or find the option “Immediate Window” in the “view” menu (if you are lucky enough, some people can’t find it [1])

Once you have an immediate window, you can type simple expressions. The most useful of them are assignments, so you can easily change the value of a variable while observing its behavior.

You can also use this window just to check the value of data, without the need to call a “watch” or “quick watch” window. It is just easier to type the expression you want to see.

Finally, you can combine this with an unrelated but useful feature: you can move the instruction pointer for the program by drag-and-drop of the instruction pointer (the yellow arrow on VS editor). This can be used to simulate at least part of the “drop-to-frame” feature in Java.

[1] http://msdn.microsoft.com/en-us/library/f177hahy%28VS.80%29.aspx

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Day 10: Learn Keyboard Shortcuts

Hi, this is the 10th part of a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.


Programming is like playing a game. It gets better when you have more practice. Although you need a lot of intellectual acuity in order to create good programs, it also helps if you can do it faster.

Working faster is one of the abilities of good programmers that separate them from the remaining of the pack. If you can program fast, you can find better solutions by just iterating. You can do three different solutions and select the best one, while your fellow will be stuck on the first option. It is a numbers game, in some way.

While it doesn’t make any good to write thousands of lines of bad code, writing more code will improve your capacity to reason about what needs to be developed. It is the typical reinforcement loop that can make you a real performer.

So, although working fast is not what makes good code, by practicing fast paced work you will actually achieve better results quicker. And one of the best ways to improve your speed is learn the tricks of the keyboard.


Developing as a Musician

The metaphor of a musician is an interesting one, because musicians don’t need to play fast every time, but it is a fundamental skill to play fast when necessary. The same goes for developers. But to “play” well, you need to understand the “instrument”.

To improve speed, I think it is essential to learn the most keyboard shortcuts you can. Typing is faster than using the mouse for programming tasks, that involve textual composition (it is different for people working on graphics, but many of them also love keyboard shortcuts).

Every decent development tool has a lot of keyboard shortcuts that can make you go faster. Start by learning just a few of them. Then, try to learn a new one every day. In a few weeks, you will see that your speed has increased dramatically.

It also helps to get a good text editor. What a good text editor is changes from person to person (I am a Vim fan), but there is a lot of good choices: vim, emacs, texmate/e, eclipse. Take the tool that best solves your problem and is adapted to your work style, and learn it like you would learn a good video game.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Recursion Using Trampoline Functions

Trampoline function is a general term used to describe an additional level of indirection between the calling code and the called code. For example, a trampoline can be used to intercept calls to a function and supply additional functionality that was not present on the original function (see this Microsoft research project [2] to understand what is possible).

To illustrate the idea, here is a simple example of trampoline function that can be used effectivelly to eliminate recusive calls. It is important to do this in languages like C because the compiler is usually unable to optimize tail calls, unlike languages such as Scheme.

The usual recursive way of calculating a factorial would be something like this:

int fact(int x, int total) {
   if (x <= 1) return total;
   return fact(x-1, total*x);
}
int main() {
   printf("result is %d\n", fact(10, 1));
   return 0;
}

The problem with this version is that you are using the function stack to maintain the intermediate results. While this is not really a problem here (because the size of the integer is more of a limitation than the size of the stack), it can be a real concern in other recursive implementations.

Let us see what we can do using a trampoline function. That is, instead of calling the function itself, we call a pointer to a function that is supposed to do what it needs to do, and then return a pointer to a place where we need to go next:

#include <stdlib.h>
#include <stdio.h>

typedef void * (*Func)(int *x, int *total);

void *fact0(int *x, int *total) { return NULL; }

void *fact(int *x, int *total) {
   if (*x <= 1) return fact0;
   *total *= *x;
   *x = *x-1;
   printf("total %d, x: %d\n", *total, *x);
   return fact;
}

int main() {
   int res=1, n = 10;
   Func f = fact;
   while (f != NULL) {
      f = (Func)f(&n, &res);
   }
   printf("result is %d\n", res);
   return 0;
}

This also works, and now there is no problem with the stack size: at each time, the stack holds only the context for the function that is being called. It is always possible to do something like this in a tail recursive function, because we don’t need to store return information for the last call.

In fact, as mentioned by Knuth [1], the method described above can be used to implement any algorithm. This is exactly the direction taken when implementing some interpreters to scheme. The basic idea is to use continuations to store control structure information, so that one never needs to return from a function. However, as stack size is limited in languages such as C, the tactic explained above is used, so one can work with trampoline functions instead of continuations. Tricky but useful when implementing functional languages.

For a similar example in Python, see [3].

[1] Structured Programming With Goto Statements, D.E. Knuth, A.C.M. Computing Surveys, Vol. 6, pp. 261-301.

[2] http://research.microsoft.com/apps/pubs/default.aspx?id=68568

[3] http://mail.python.org/pipermail/tutor/2004-August/031553.html

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Using Git Efficiently

I started using Git a few years ago, and to be sure, I cannot say that I know everything about it. However, I know enough of it to say that I am much more efficient using Git than other source control method.

In this post, I will summarize some of the commands everyone need to know in order to start using Git.


Creating a Repository

Creating a repository is just a matter of initialize a directory with git metadata, and add the files to the repository. For example, suppose you want to create a repository of directory /home/john/myproject To do this, you need to use the commands

cd /home/john/myproject
git init
git add .
git commit -m 'first version'

The first git command just initializes the .git directory, where all git metadata is stored. The second command adds everything under the current directory to the repository. Finally, the git commit command creates the initial revision of the project into the repository.


Making Changes

The next common operation in a repository is to make changes to the existing files. After you make the needed changes using your text editor or IDE, the following commands will do the trick:

git add ChangedFile1 ChangedFile2
git commit -m 'description of change'

The git add is necessary even if the file exists, because without it git doesn’t know if the files needs to be updated or not. In git parlance, git add is used to create the “staging area”, which will be written by the commit.

This is a difference from SVN, for example, where anytime we do a commit the system accepts all changes by default. With git you need to be specific about what is being commited.

Optionally, if you prefer to use a GUI to view the changes, you can use the command

git gui

This will invoke the Tk interface, which works both on UNIX and windows (and probably Mac OS X).


Deleting and Recovering

Another common operation is to remove files. This can be done with

git rm FileName

notice that this will work if the files hasn’t been modified. If you need to delete a file that has been changed, use

git rm -f FileName

To recover a file that has been deleted or changed, you can use the following

git checkout -- FileName

Creating Branches

With the commands above you can do pretty much anything you need in terms of normal usage of a VCS. However, creating branches is where the fun is. Git favors the creation of branches, because creating them is very fast and cheap (as measured in memory usage).

To create and use a new branch, type

git branch BranchName
git checkout BranchName

The first command creates the branch, while the second changes to the new branch. You can always go back using

git checkout master

where master is the default branch name.


Merging Files

Git makes it very easy to create branches, but also to merge the results of two or more branches. To merge a branch called “BranchName”, you can use the command

git merge BranchName

Git does a great work of solving conflicts automatically. However, if it can’t solve the conflict, you will be able to edit the files manually and commit them as normal.

You can visualise the results of merges and of other branches using a graphical tool. Just type

gitk --all

and you will be able to see all branches in the repository, along with the history. For a short log of changes, you can also type

git log

Advanced Tricks

There are thousands of advanced tricks you can learn with git, since each part of the system may work independently of the others. I will give you just a flavor of what you can do.

For example, if you want to change the order in which commit appear in a branch, you can use

git rebase -i CommitID

The commit id is the hexadecimal number listed on each commit when you use git log. The command above will call an editor and allow you to edit the commits that have been made since CommitID. You can change the order in which they are applied, merge two or more commits, or even remove some of them. In this way, you can rewrite the history of your commits, and make changes that you wouldn’t be able to do in something as SVN.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter