Taswar Bhatti
The synonyms of software simplicity
linq

Here is some poorly written code that I had to review today and modify (i.e refactor). 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.

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

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.

Finally lets remove the entire foreach loop and the count if statement. Since if we have 0 items this code will still not fail.

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.

dotnet C#

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.

Enjoy.

eclipse_indigo

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 🙂

techchange

Driving Technical Change Book cover
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

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

goliath

What the ???? goliath ERROR: While executing gem.
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.

Really helpful (scarcasim) , then what I had to do to fix this was update my gem

After doing so I was able to

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.

delegate

Here is a nice little code I did recently, where the Command Pattern is used with a notification observer like pattern.

First of the Command Pattern, a simple interface for task with an execute method

Then the notification interface, with 2 methods, one when it started and one when its complete and the event delegate

Now the class that implements the interface, I used an abstract class so that I can just use a subclass to implement the simple task I wish to have

Now I can write a simple class that inherits from AbstractTask like this

Now to consume the task

This is more or less the simple form of creating a command pattern to run task that includes a notification to send back. It makes sense to have multiple task like creating users and running database scripts etc etc. This allows one to have flexible design and have an IEnumerable<AbstractTask> and run through each one with an execute method.

Hope this helps 🙂 Happy coding.

ASP.NET-MVC

Here is a simple trick on how to disable browser cache in asp mvc application with an attribute.
If you have a base controller just add this to your base and all your request would have Pragma No-Cache

Simply add this to your base controller, and you are done 🙂

ASP.NET-MVC

To enable https on asp.net mvc, one can use the [RequireHttps] attribute on the base controller if you are using a base controller

But what if you want to give an option to the installer or the client to enable it in web.config, the easy way to do it for asp mvc2 is to create your own property like code below: (Please note: I am using protected virtual to override stuff for testing, its not one of the best things but it works in breaking dependency and its quick, so that I dont have to mock httpcontext, configuration manager etc etc, I could use an IoC but would make it more complicated for the reader but feel free to use one)

Here is the unit test for it, I am using a stub class to override the things I want for testing

Now in your web.config you can add a key value pair like

And last if you are using Asp.Net Mvc3 life is way easier, one can just use in their Global.ascx file to add the filter

Lastly you would add it to your BaseController

jquery

Was debugging some code that was broken due to jquery 1.6 update, and found this call for Jquery dropdown.val

Not working anymore but a simple fix of changing it to

This does the trick, for more on prop take a look at the api of jquery http://api.jquery.com/prop/

UA-4524639-2