bling.github.io

This blog has relocated to bling.github.io.

Wednesday, July 29, 2009

Assimilated by ALT.NET

It feels good to be revitalized again! A couple months back I was very unhappy. I was at a job I didn't particularly like. The team I worked with was very rigid and used waterfall practices (even though it was a small 5 man team), and they were very resistant to change (I struggled to move them off SourceSafe, and failed). It was time to job ship and try something else, but then the market crash hit and suddenly having money to put food on the table became a priority. Life got boring and dull. I was writing C++ code (which didn't even use boost because it wasn't allowed), and .NET 2 code when 3.5 was out for 3 years already. I lost all drive to learn and grow, and after 8 hours of work I really didn't want to have anything to do with coding when I was home. Then my life changed... I found a new job, got hired, and it has been a HUGE turnaround. I haven't been there long (almost done my 3 month probation), but I'm finding myself working at home simply because I love what I'm doing. The difference? My new team actually wants to learn new things and continually improve. It is just under 3 months, and I have already helped my team implement branching conventions (rather than 2 week code freezes), unit testing conventions (rather than no conventions), and maintaining a CruiseControl.NET build server (rather than no build server). It sure makes a HUGE difference when what you're doing is valued by your coworkers. In my 2nd month I was sent to my first conference, DevTeach in Vancouver, and I'd probably consider this the biggest trigger of change in my way of thinking. I was only away from the .NET community for about a year, but sitting in on the sessions with a panel of experts really drove home exactly how obsolete I already was. I've heard of Test Driven Development. But what's this Behavior Driven Development all about? What's Domain Driven Development? What is Inversion of Control and why should I care? And then I came across ALT.NET. I first heard about it in James Kovacs' Vancouver DevTeach session on IoC containers, who was telling everyone about a ALT.NET conference that weekend after the DevTeach conference. I had next to no knowledge about what ALT.NET was, so I skipped out on the conference because I had other obligations that weekend. I wish I knew more about ALT.NET earlier, because I would have rescheduled my obligation so I could attend the conference!! It didn't take long for me to be fully immersed in everything the ALT.NET community has to offer, and I was reading up on anything and everything I could. I practically had years of catchup to do. And then I was given my big break. A completely new project with absolutely no dependencies on anything else was assigned to me. It was going to be a massive n-tier project with web clients, windows clients, business logic, database, and anything else you could think of. I got the chance to design something big from scratch. So now I'm reading everything I can about domain driven development and making sure I don't do the classic Gang of Four mistake of overdesigning after reading ;)

Saturday, July 25, 2009

Best tool for the job...

So I recently moved, and my new place doesn't make internet by wire all that accessible...so now I have a big honkin desktop computer with no internet, but I have a little netbook which has wireless. Hmmmm...my netbook's main operating system is Archlinux...and Linux is strong in networking...so it should be easy to set it up right? Right I was! - give my ethernet an IP address and start it # ifconfig eth1 192.168.0.1 netmask 255.255.255.0 # ifconfig eth1 up - enable packet forwarding and reboot (or modify /proc) # set /etc/sysctl.conf to set net.ipv4.ip_forward=1 - set up iptables # iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE Done! Now on my desktop I just set it to static IP 192.168.0.2, connected to my gateway 192.168.0.1, and boom, I got internet! Now I can wait patiently for a wireless adapter to go on sale...

Wednesday, June 24, 2009

CodeRush, ReSharper, Visual AssistX

Hmmmmm...CodeRush and ReSharper...It seems like these are the only 2 products people think about when they want that all super powerful addon that's supposed to quadruple their productivity. I think people just look at the feature chart and say wow, ReSharper and CodeRush have hundreds of features!!! I'm going to be so much more productive now! I've tried both and there are features I like from both products. For example, ReSharper's find references is extremely useful because you can filter by read-only or writeable. I'm also a big fan of all the visual cues that CodeRush adds to the editor. But...neither of them have the 1 feature Visual AssistX I like most: Partial Names IntelliSense. What's that? Let's say you have SomeMethodWithAReallyLongName. If you type "really" with VAX's intellisense, that method appears in the list. Originally I thought that was a nice feature, but it can't be that useful. But then I was fortunate (or unfortunate is probably more accurate) to get to work with MSHTML. Now I have 1 humongous namespace with around 100 different types. Sometimes it's called TextRange and other times it's called TxtRange. Some have the HTML prefix, and others don't. When was the last time you could only remember a part of a method name?

Tuesday, May 5, 2009

MetaTrader Indicators

One of my hobbies is forex trading, and a sub-hobby of that is writing custom indicators. MetaTrader is a trading platform that is very popular in the forex community because it allows traders to write custom indicators. It's pretty easy to get started if you have programming experience because the language is very similar to C. And yes, you can also write your own bot if you wanted, called an Expert Advisor. Personally I don't spend too much time trying to write a winning bot simply because there are way too many variables to take into account, and eventually the market's going to yell "in your face" and your winning bot will end up losing. I just write simple scripts that help me identify entry points, and filter out all the non-interesting price movements. Recently I've been looking at breakout strategies because they are simple, yet effective. The result is a custom indicator I wrote which simply put calculates the high and low for a time period, and then draws pretty rectangles. Like so: Hosted by imgur.com The above is simply the high/low of 2 hours before the opening London session, and the opening US session. Isn't it puurrrrrrty?

I'm doing the VI challenge!

One of my time-wasting hobbies is running Linux on virtual machines. I play games from time to time so Windows is still my main OS. But otherwise, I do use my Linux virtual machines for many things like my personal Mercurial repository is running on VirtualBox. For the curious, my distro of choice is Archlinux. Anyone who's needed to tinker with a Linux distro knows that you spend a lot of time in a text editor changing configuration files. Typically, the choices are pico/nano for its simplicity, or vi because it's on any POSIX compliant operating system. I decided, ah what the heck, I'm gonna learn vi and use that for editing files instead of nano. I got decent with vi. I could move around (albeit I still relied on home/end and arrow keys), save, search, etc., but on Windows I'd still use Notepad2. In an attempt to improve my productivity in text editing, I'm doing the VIM challenge! All my text editing I will be using WinVI (I even replaced the notepad.exe with it), and I even got the demo of ViEmu running on Visual Studio. For the next 2 weeks I will be using vi for anything text related. I've been going through tutorials and trying to remember all the new things I'm learning. Most importantly, I'll be trying to avoid the home/end/arrow keys as well. BTW, '*' is an awesome feature in vi. It highlights all instances of the current word in the document, and subsequently you can use n or N to back or to the next instance. Like, take a look at the following screen shot: Hosted by imgur.com Boom! All instances of "total" highlighted with just a Shift+8. Let's see you do the same thing with Ctrl+F, mouse-click "Find next" (or worse, reverse your search).

Tuesday, April 28, 2009

A Trick Question With Closures

Given the following code, what do you expect the output to be?
for (int i = 0; i < 100; ++i)
{
    ThreadPool.QueueUserWorkItem(delegate
    {
        Console.WriteLine(i);
    });
}
Keep that answer in your head! Now...what do you expect the output of the following?
int i;
for (i = 0; i < 100; ++i)
{
    ThreadPool.QueueUserWorkItem(delegate
    {
        Console.WriteLine(i);
    });
}
Were your answers the same? Different? Why?

Monday, April 27, 2009

A Simple Thread Pool Implementation (Part 2)

I said last time I'd compare the performance of my super duper simple implementation against the .NET ThreadPool, so here it is! Here's the test code:
ManualResetEvent evt = new ManualResetEvent(false);
int total = 100000;
int count = 0;
DateTime start = DateTime.Now;
for (int i = 0; i < total; ++i)
{
//  ThreadQueue.QueueUserWorkItem(delegate(object obj)
    ThreadPool.QueueUserWorkItem(delegate(object obj)
    {
        Console.WriteLine(obj);
        if (Interlocked.Increment(ref count) == total)
            evt.Set();
    }, i);
}
evt.WaitOne();

Console.WriteLine("Time: {0}ms", (DateTime.Now - start).TotalMilliseconds);
Console.ReadLine();
Here are the initial tests. My CPU is a Core2Duo clocked at 3.6GHz. I also ran a release build outside of the debugger. I set my ThreadQueue to have 25 worker threads. The results were pretty surprising:
ThreadPool: 1515.6444ms ThreadQueue: 5093.8152ms Well, that's interesting. The .NET ThreadPool whooped my butt! How can that be? Let's dig deeper and find out how many threads actually got used. I used ThreadPool.GetMinThreads and ThreadPool.GetMaxThreads, and I got 2-500 for worker threads, and 2-1000 for completion IO threads. Then, I tracked how many threads were actually used, like this:
Dictionary threads = new Dictionary();
// ...snip
    ThreadPool.QueueUserWorkItem(delegate(object obj)
    {
        Console.WriteLine(obj);
        threads[Thread.CurrentThread.ManagedThreadId] = 0;
        if (Interlocked.Increment(ref count) == total)
            evt.Set();
    }, i);
// ...snip
Console.WriteLine("Threads used: {0}", threads.Count);
Surprisingly, only 2 threads were used. So, I set the number of worker threads of my ThreadQueue to 2. Viola! 1437.5184ms, which is just a little under 100ms faster than the ThreadPool. I guess this shows that more threads does not mean better or faster! For fun I set the number of worker threads to 200 and it took 27906.6072ms! There was clearly a lot of locking overhead here...