Stop yelling at the lettuce for not growing – Software Teams
Posted in Agile, Misc on February 14th, 2013 by taswar
I remember watching a talk on infoq by Michael Feathers. He said something about growing your teams organically like a garden. An example he gave is, you don’t yell at the lettuce for not growing, you have to nurture it and help it, try different things so that it grows better.
If you have a co-worker in your team not doing very well, you need to help them to become stronger, guide them, try different things, show them different ideas, pair with them.
The reality is every software project will have weeds (bad code), you need to pull the weeds out as the garden grows, clean it up, so that overall the software is growing more organically. By having a stronger team you will have less weeds.
For your process rather than just focusing on task base feature, one should also focus on code quality features. Don’t just focus on hammering the new feature out, have the time to go over items that need care. Refactor the code that you see lots of bug and write test in those areas.
From my experience, I have seen when a coworker tries to explain their problem to you they already have a solution in their mind and when they try to verbally explain to you they have in the back of their mind solved the issue already. And if its not the case give them ideas of how you perceive the problem and show them your take on it.
But by yelling and not helping the lettuce, the lettuce is not going to grow any better.
C# Perf improvement data types or using Linq is faster?
Posted in .NET, Perf Testing on February 13th, 2013 by taswar
Recently I was asked a simple code to improve the performance, and at first I couldn’t see it but as you know your brain starts working when you aren’t thinking of the problem anymore. (It happen to me when I was driving)
Anyway the problem on hand was there is a for loop and when there are 10000 clients it performs bad.
Something along the line of.
1 2 3 4 5 6 7 8 9 10 11 12 | List<Client> _client; public void SendMessage(int id) { foreach(var c in _client) { if(c.Id == id) { c.SendMessage(); break; } } |
So I went and wrote some code to see the perf using Linq and at the end using a Dictionary rather, since a dictionary is way faster in lookup, O(1) in this case.
Here is the code using a List and Linq together
1 2 3 4 | foreach (var client in _clients.Where(x => x.Id == id)) { client.SendMessage(); } |
and if we changed the datatype to Dictionary this was the code used
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //using tryget if (_clients.TryGetValue(id, out c)) { c.SendMessage(); } //using linq var c = _clients.FirstOrDefault(x => x.Key == id).Value; if (c != null) { c.SendMessage(); } //using just an index Client c = _clients[id]; c.SendMessage(); |
Here is the end result of using it on an i7 8G of ram machine.
And the results show that using an index as a dictionary is definitely faster, but one also has to look at the number of data.
Where there are only few data entry using linq is quite insignificant in performance but where there are millions of data, it really shows that it slows down.
Here is the rest of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | using System; using System.Collections.Generic; using System.Linq; namespace PerfMeasureApp { class Program { static void Main(string[] args) { var queue = new MessageQueue {Clients = GenerateListClients()}; var fastQueue = new MessageQueueSmart {Clients = GenerateDictClients()}; queue.DoStuff(1); Console.WriteLine("********************************************"); queue.DoStuffLinq(1); Console.WriteLine("********************************************"); fastQueue.DoStuff(9999999); Console.WriteLine("********************************************"); fastQueue.DoStuffLinq(9999999); Console.WriteLine("********************************************"); fastQueue.DoStuffIndex(9999999); Console.WriteLine("********************************************"); Console.Read(); } // Define other methods and classes here public static List<Client> GenerateListClients() { var list = new List<Client>(); for (int i = 0; i < 10000000; i++) { if (i == 9999999) list.Add(new Client(1)); else list.Add(new Client(0)); } return list; } public static Dictionary<int, Client> GenerateDictClients() { var dict = new Dictionary<int, Client>(); for (int i = 0; i < 10000000; i++) { dict.Add(i, new Client(0)); } return dict; } } public class MessageQueueSmart { private IDictionary<int, Client> _clients; public IDictionary<int, Client> Clients { set { _clients = value; } } public void DoStuff(int id) { Console.WriteLine("MessageQueueSmart DoStuffTryGet"); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); Client c; if (_clients.TryGetValue(id, out c)) { c.SendMessage(); } stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } public void DoStuffLinq(int id) { Console.WriteLine("MessageQueueSmart DoStuffLinq"); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); var c = _clients.FirstOrDefault(x => x.Key == id).Value; if (c != null) { c.SendMessage(); } stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } public void DoStuffIndex(int id) { Console.WriteLine("MessageQueueSmart DoStuffIndex"); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); Client c = _clients[id]; c.SendMessage(); stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } } public class MessageQueue { private List<Client> _clients; public List<Client> Clients { set { _clients = value; } } public void DoStuff(int id) { Console.WriteLine("MessageQueue DoStuff"); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); foreach (var client in _clients) { if (client.Id == id) { client.SendMessage(); break; } } stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } public void DoStuffLinq(int id) { Console.WriteLine("MessageQueue DoStuffLinq"); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); foreach (var client in _clients.Where(x => x.Id == id)) { client.SendMessage(); } stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } } public class Client { public Client(int id) { Id = id; } public int Id { get; set; } public void SendMessage() { Console.WriteLine("SendMessage called " + Id); } } } |
Ubunutu 12.10 installing Scala 2.10 and Play Framework 2.1.0
Posted in jvm, playframework, scala on February 10th, 2013 by taswar
First one has to get their java working.
To install java
1 2 3 4 5 6 7 8 9 10 | $sudo add-apt-repository ppa:webupd8team/java $sudo apt-get update $sudo apt-get install oracle-java7-installer $sudo update-java-alternatives -s java-7-oracle $which java /usr/bin/java $java -version java version "1.7.0_13" Java(TM) SE Runtime Environment (build 1.7.0_13-b20) Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode) |
Once java is installed we will install scala, I like using stow for my package management for non apt-get stuff.
1 2 3 4 5 6 7 8 | $cd /usr/local/stow $sudo wget http://www.scala-lang.org/downloads/distrib/files/scala-2.10.0.tgz $sudo tar zxvf scala-2.10.0.tgz $sudo stow scala-2.10.0 $which scalac /usr/local/bin/scalac $scalac -version Scala compiler version 2.10.0 -- Copyright 2002-2012, LAMP/EPFL |
Now that we have scala is installed lets get play framework installed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $cd /usr/local/ $sudo wget http://downloads.typesafe.com/play/2.1.0/play-2.1.0.zip $sudo unzip play-2.1.0.zip $cd framework/sbt/boot $sudo chmod -R o+w boot $vi ~/.bashrc (let add to PATH=$PATH:/usr/local/play-2.1.0:. ) $source ~/.bashrc $play _ _ _ __ | | __ _ _ _| | | '_ \| |/ _' | || |_| | __/|_|\____|\__ (_) |_| |__/ play! 2.1.0 (using Java 1.7.0_13 and Scala 2.10.0), http://www.playframework.org This is not a play application! Use `play new` to create a new Play application in the current directory, or go to an existing application and launch the development console using `play`. You can also browse the complete documentation at http://www.playframework.org. |
If you get an error when you launch play that means you havent given the rights to the boot folder, check the commands I have listed above again.
Using LINQ to refactoring code into single responsibility principle SRP
Posted in .NET, Design on January 24th, 2013 by taswar
Was going through some legacy code recently and found a big for loop that had multiple switch statement inside. So being that I hate something that is fairly large, every method should be small and concise, I went along to refactor it.
This code is generic so you might see something like this in your code base also.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | IEnumerable<Customer> allCustomers = Respository.GetAllCustomers(); IDictionary<string, string> payingCustomers = new Dictionary<string, string>(); IDictionary<string, string> nonPayingCustomers = new Dictionary<string, string>(); IDictionary<string, string> deadBeatCustomers = new Dictionary<string, string>(); foreach(var cust in allCustomers) { if(cust.Type == IGNORE) continue; switch(type) { case A_TYPE: payingCustomer.Add(cust.Key, cust.Name); break; case B_TYPE: nonPayingCustomer.Add(cust.Key, cust.Name); break; case C_TYPE: deadBeatCustomer.Add(cust.Key, cust.Name); break; } } |
As you will notice one can go on and keep adding types to it (so did the legacy code).
But if you think about it what this code is basically doing is that its just filtering things out to seperate collections.
Let see how we can answer things with LINQ on this one
First lets remove the if statement for customer IGNORE.
1 2 3 4 5 6 7 8 9 10 11 12 13 | foreach(var cust in allCustomers.Where(x => x.Type != IGNORE) { switch(type) { case A_TYPE: payingCustomer.Add(cust.Key, cust.Name); break; case B_TYPE: nonPayingCustomer.Add(cust.Key, cust.Name); break; case C_TYPE: deadBeatCustomer.Add(cust.Key, cust.Name); break; } } |
Ok one step done thats better, now lets see that the loop is basically a filter, so lets just take the payingCustomer example and break it down first. Its is going through a loop and finding the A_TYPE customer. Therefore a linq query like
collection.Where(x => x.Type == A_TYPE).toDictionary(t => t.Key, t.Name);
can answer that one.
So lets create a function/method that does this.
1 2 3 4 5 | private static IDictionary<string, string> FilterCustomers(IEnumerable<Customer> customers, Type type) { if(customers == null) throw ArgumentNullException(); return customers.Where(x => x.Type == type).toDictionary(t => t.Key, t.Name); } |
So here we have a single responsibility function, that just does one thing and does it well.
Its more of a functional style of programming (data in and data out).
No side effects of any sort. No saving of data to database or file etc.
Now lets go back to our original code, we can now remove the loop and switch statement all together.
1 2 3 4 5 6 7 8 9 | IEnumerable<Customer> allCustomers = Respository.GetAllCustomers(); var filterCustomer = allCustomers.Where(x => x.Type != IGNORE); var payingCustomers = FilterCustomers(filterCustomer, A_TYPE); var nonPayingCustomers = FilterCustomers(filterCustomer, B_TYPE); var deadBeatCustomers = FilterCustomers(filterCustomer, C_TYPE); |
Note: we now have a filterCustomer collection, which filters out the ignore customers first.
By doing so we can write a unit test to test the method/function FilterCustomers and we can be 100% sure that all it does is a single responsibility task.
We now have removed complexity in our codebase and the test-ability goes up.
Using Linq to refactor code
Posted in .NET, Design on December 17th, 2012 by taswar
Here is some poorly written code that I had to review today and modify. I have removed the domain of the code and renamed the variables, so that it remains anonymous, but the just of the code is still intact.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var itemCount = 0; if (users.ShoppingCart != null && user.ShoppingCart > 0) { foreach (var cart in user.ShoppingCart) { if (cart.Items != null && cart.Items.Count > 0) { foreach (var item in cart.Items) { itemCount++; } } } } |
Lets go through the inner loop of the code first.
First the loop is really unneccesary one can find out how many items there are using the
count of if since it is an IEnumerable
Thus it becomes
1 2 3 4 5 6 7 8 9 10 11 | var itemCount = 0; if (users.ShoppingCart != null && user.ShoppingCart > 0) { foreach (var cart in user.ShoppingCart) { if (cart.Items != null && cart.Items.Count > 0) { itemCount += cart.Items.Count(); } } } |
Now we go to the outer foreach we can put a Where clause to remove the if statement we don’t really need to check for the “0″ condition since 1 + 0 is still 1 so why filter for those.
1 2 3 4 | foreach (var cart in user.ShoppingCart.Where(x => x.Items != null) { itemCount += cart.Items.Count(); } |
Finally lets remove the entire foreach loop and the count if statement. Since if we have 0 items this code will still not fail.
1 2 3 4 | if (users.ShoppingCart != null) { itemCount = users.ShoppingCart.Where(x => x.Items != null).Sum(y => y.Items.Count()); } |
Now we have replaced all this code with a linq query that would tell us how many items there are with one line of linq. I believe this makes is easier on the eye and readability of it rather than looping and branching of the code. The more branches you have the harder it is to read code.
C# detect inuktitut characters Canadian Aboriginal Syllabics
Posted in .NET on November 6th, 2012 by taswar
Here is a quick way to detect if a text is in Canadian Aboriginal Syllabic in C#.
It is just an extension method that would detect if your string is in that range by using regex.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static bool IsInuktitut(this string input) { if (string.IsNullOrEmpty(input)) { return false; } //http://msdn.microsoft.com/en-us/library/20bw873z.aspx //look at section Supported Named Blocks const string pattern = @"\p{IsUnifiedCanadianAboriginalSyllabics}"; Match match = Regex.Match(input, pattern); return match.Success; } |
Enjoy.
Eclipse Indigo hangs on splash screen workspace
Posted in Misc on December 15th, 2011 by taswar
So today I came in to work and saw that my wonderful windows machine was restarted due to Windows Update, and yesterday I was working on some java stuff and did not close out my eclipse, thank god that I do regular code check-in
Anyways tried to relaunch eclipse and it kept hanging on workspace.
To fix that I had to go into my workspace folder C:\workspace\.metadata\.plugins
Then I copied the org.eclipse.core.resources to a backup directory and deleted it from the plugins folder.
I restarted Eclipse and then did a File->Import->Existing Project into Workspace and browse my workspace/project directory
And voila I was back in action
Driving Technical Change Book Review
Posted in Books on October 30th, 2011 by taswar

Have wanted to post the review of this book for a while, which I read somewhere beginning of this year.
This book does a very good job in classification of the “typical” stereotypes of people in IT.
- The Uninformed
- The Herd
- The Cynic
- The Burned
- The Time Crunched
- The Boss
- The Irrational
After the first section we go into what kind of techniques we can use to drive technical changes.
- Gain Expertise
- Deliver Your Message
- Demonstrate Your Technique
- Propose Compromise
- Create Trust
- Get Publicity
- Focus on Synergy
- Build a Bridge
- Create Something Compelling
Lastly the author goes into Strategies one can use to drive technical changes
- Simple, Not Easy
- Ignore the Irrational
- Target the Willing
- Harness the Converted
- Sway Management
The book is a short read with only 125 pages, it is a good read but at the same time one should always remember there are people whom you have to deal with that are emotional, and us human’s are not predictable, the classification helps but if we really want to drive the change we have to be the change agent. I would recommend this book for a beginner who wishes to drive technical changes.
JRuby install and build eventmachine from github
Posted in ruby on October 21st, 2011 by taswar
Have been giving eventmachine a try but I do have issues with it thus wanted to try out the beta version on github.
Here is how you get it to install on your local machine.
- First clone the repo
- Build the gem
- Install the java version
1 2 3 4 5 6 7 8 9 | $ git clone git://github.com/eventmachine/eventmachine $ cd eventmachine $ jruby -S gem build eventmachine.gemspec In this step you might be missing some gems, I was missing yard and rake-compiler $ jruby -S gem install yard $ jruby -S gem install rake-compiler $ /c/jruby-1.6.4/bin/rake java gem Once the build is done use the pkg version $ jruby -S gem install pkg/eventmachine-1.0.0.beta-4.java.gem |
ERROR: While executing gem … (NameError) uninitialized constant Psych::Syck
Posted in ruby on October 16th, 2011 by taswar
What the ????
I was wanting to try out goliath.io (Goliath: Non-blocking, Ruby 1.9 Web Server) for more info look at http://www.igvita.com/2011/03/08/goliath-non-blocking-ruby-19-web-server/
In any case I was not able to get it to install on JRuby so went and get me 1.9.2 MRI.
Once installed MRI 1.9.2, I went into the gem dir to gem install goliath and guess what this message pops up.
1 2 | ERROR: While executing gem ... (NameError) uninitialized constant Psych::Syck |
Really helpful (scarcasim) , then what I had to do to fix this was update my gem
1 | gem update --system |
After doing so I was able to
1 | gem install goliath |
Now its time to try out goliath, wish me luck and if anyone knows how to install it with Jruby please let me know cuz jgem install goliath didnt work for me.

