A polyglot “C#”er

My professional career until now (spans barely 8 years), has been mostly “C#”ish. I liked the language when I started working with it, and really enjoyed staying within the Visual Studio editor. While I was working earlier in my career, I did find interest in reading several blogs about being a better programmer as a whole. The word “polyglot” reverberated in many such well thought blogs. I did understand at the point that having exposure to many languages will equip me better in my work. But here is my honest take on it now that I have spent some reasonably sizable amount of time with a language.

You got to know a particular language well first. For me it is undoubtedly C# as it stands. Once you have that under your sleeves, when you start exploring other languages you learn them in a different way than you would have learnt if you had not mastered a language. It becomes just involuntary that you start comparing features of this new language to the language of your forte. And with this the benefits are two-fold, you learn the new language faster, and you find better ways of solving problems in your mastered language. I think the second point is really exciting for me as I venture on learning new languages and paradigms. For some limited time now, I have worked on Java professionally. I have taken a stab at Scala and Haskell during my personal time although it is not usual to find these 2 languages easily in mainstream application development. The best way to learn languages is to solve some really common problems in computer science. I like the Producer-Consumer problem as it highlights usages of collections and threads which form the biggest chunk of the stuff we use. Below are C# snippet juxtaposed to Java snippet. Spot 6 differences to win a free trip to Goa Winking smile.





public class PCQueueCharp
{
private Queue queue = new Queue();
private static readonly object SyncLock = new object();

public PCQueue(int capacity)
{
Capacity = capacity;
}

public int Capacity { get; private set; }

public void AddTask(ITask task)
{
lock(SyncLock)
{
if (queue.Count == Capacity)
Monitor.Wait(SyncLock);
queue.Enqueue(task);
Monitor.Pulse(SyncLock);
}
}

public ITask RemoveTask()
{
lock(SyncLock)
{
if (queue.Count == 0)
Monitor.Wait(SyncLock);
var task = queue.Dequeue();
Monitor.Pulse(SyncLock);
return task;
}
}
}


public class PCQueueJava
{
private int capacity;
private final Queue queue;
private static final Object SyncLock = new Object();

public PCQueue(int capacity)
{
this.capacity = capacity;
queue = new ArrayDeque(capacity);
}

public void AddTask(ITask task) throws InterruptedException
{
synchronized (SyncLock)
{
if(queue.size() == capacity)
SyncLock.wait();

queue.add(task);
SyncLock.notify();
}
}
public ITask RemoveTask() throws InterruptedException
{
synchronized (SyncLock)
{
if(queue.size() == 0)
SyncLock.wait();
ITask task = queue.remove();
SyncLock.notify();
return task;
}
}
}



And here is a beginners implementation in Scala



class PCQueue(capacity:Int)
{
val queue = new mutable.Queue[ITask]()
val syncObject = new AnyRef

def addTask(task : ITask) : Unit =
{
synchronized
{
if(queue.length == capacity)
wait()

queue += task
notify()
}
}

def removeTask() : ITask =
{
var task : ITask = null
synchronized
{
if(queue.length == 0)
wait()

task = queue.dequeue()
notify()
}
return task
}
}

The idea is to improve this class in Scala to make it more “Scala”ble in a later post.

Abhang Rane


No comments :

Post a Comment

Leave a Comment...