Sohan's Blog

Things I'm Learning

Testing the SyntaxHighlighter Scripts

For the last few months, I am on the lookout for a really good syntax highlighter that helps me to post blogs on all the programming languages in a pretty html with syntax coloring/highlighting. This one is taken from http://alexgorbatchev.com/wiki/SyntaxHighlighter

Ruby/Rails
class TestHighlighter
def my_highlighter
return "SyntaxHighlighter"
end
end

CSharp

public class TestHighlighter
{
public string MyHighlighter
{
get {return "SyntaxHighlighter";}
}
}


What did I add to my blog?



Open Edit Html and add this just before in the blog’s template



<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" type="text/css" rel="stylesheet" />
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" type="text/css" rel="stylesheet" />
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script type='text/javascript'>
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
</script>


Thanks to Ashraf for informing me about the Windows Live Writer Plugin! It will be useful for sure.

Comments

Ashif Manjur
Thank you for posting such a helpful post. The code snippet posting has been much easier with this.
Ashrafuzzaman
This is a nice plugin too
http://sourcecodeplugin.codeplex.com/
Ashrafuzzaman
Here is an alternate plugin for live writer.
http://precode.codeplex.com/
Hope you like it :)
Sohan
@Ashraf, thanks! Good to know that it helps you.
Ashrafuzzaman
This is really a nice post. I was finding difficulties implementing it in blogger. But the post helped a lot. Thanks.

Pair Programming: How Am I Feeling?

For an explanation of Pair Programming, please visit this wiki entry.

Good

I am pair programming on the ScrumPad project now and the feeling in general is good. I have this perception is the work quality is better than doing alone. Also, the amount of time wasted to find small issues are now much reduced. Overall, its good.

Productivity Increased

I definitely found the productivity boosted by a factor of 1.5 at least. May be this is specific to my case, as I am working on a mid scale refactoring task. We worked on two user stories so far and completed the stories in 1.5 times faster compared to the individual estimates. We are doing estimation on a regular basis and the variance between estimation and actual hours are less than 5% as we see in the last 17 sprints in this project.

Disclaimer

I am yet new to this practice and not in a position to draw a conclusion yet. However, I believe our open space set-up and strong team culture helped us a lot to start this pair programming.

Are you pairing? Please, share your feelings and suggestions.

Comments

Sohan
@Arif, Thanks for the comment. Yes, you are right. I also believe for certain routine (at least regular) jobs, pair programming will be more of an overhead than an advantage. But, when it comes to significant parts with high impact on issues/opportunities, Pair programming is surely worth.
Arif
Pair programming can play a vital role in reducing the bug rates and improving the overall quality of the code.I think every software project team should practice pair programming at least in the architecturally significant parts of the project.Pairing reduces defect rates during the final stage (deadline hours) of the project.Thanks for raising this significant issue.

Assert_equal_float : Assert_equal Between Two Float Objects With Precision

In doing rails unit testing, sometime you need to assert the equality between two float objects in the following fashion

assert_equal 32.8, object_under_test.method_returning_float

The internal storage of float objects sometimes will contain 32.8 with additional lower precision decimal points and hence the assert may fail although the significant digits may be same. For example, the value of object_under_test.method_returning_float may be stored as 32.80000001 in memory and the above call may fail.

To solve this problem, you may wish to use the round method of Float class in the following way

assert_equal 32.8, object_under_test.method_returning_float.round(1)

The above line will evaluate true since it will round up to one decimal points. However, be careful about cases where you may have a value greater than 5 in the second decimal place! because (32.86).to_f.round(1) will become 32.9!

However, when you are talking about insignificant digits, this rounding should work fine for you.

Next step is to define a new assert_equal_float method that takes the precision as an argument. Adding this method will save you from code duplication and will improve readability.

def assert_equal_float(expected, actual, precision)
assert_equal expected.to_f.round(precision), actual.to_f.round(precision)
end

Happy float asserting!

Comments

tommy
correction: round(precision) is available in ruby 1.9 but not in ruby 1.87 :(
Tommy
note:
(32.86).to_f.round(1) only works in Rails test environment because of active support

.round(precision) isn't available in regular ruby code.

Looking at ASP.NET MVC

Being over-burdened by all the blog posts on MVC from the ASP.Net for the last few months, I am now looking to ASP.Net MVC. Still seeing the tutorials on asp.net/mvc site.

I have worked on a few rails projects and the ASP.Net MVC implementation is very much like Rails. Anyway, still early for me to comment!

Comments

Sohan
@Tanvir,
Yes, I read the first chapter. My finding is ASP.Net MVC is still far from being real! The implementation of AJAX does not seem to be smart yet.
I would like to wait and see if the community really takes this framework in the coming days.
Tanvir Anowar
There is also a short book by ScottGu’s on MVC. You may find the download link from his blog.

Named_scope : Real Life Examples From ScrumPad

I am working on ScrumPad chat refactoring now. In rails 2, the named_scope feature works really great for ActiveRecord models. Lets start with the code first-

The Source Code:

class ChatUser < ActiveRecord::Base
belongs_to :chat
belongs_to :user
belongs_to :chat_status, :foreign_key => “status_id”

named_scope :closed, lambda {|chat_id| {:conditions => [“status_id = #{ChatStatus::CLOSED} AND chat_id = ? “, chat_id]}} do
def invite()
each { |chat_user| chat_user.update_attribute(:status_id, ChatStatus::INVITED) }
end
end

named_scope :active_chats, lambda {|user_id| {:conditions => [“user_id = ? AND status_id not in (#{ChatStatus::CLOSED}, #{ChatStatus::LEFT})”, user_id]}}
named_scope :with_chat, :include => :chat
end

So, you get an idea about the ChatUser class. Its a relation object between user, chat and chat_status with some additional fields.

How am I Using My named_scope?

ChatUser.closed(chat_id)

Gives me all the chat users who closed their chat windows for the chat having chat_id.

ChatUser.closed(chat_id).with_chat

Gives me the same objects as previous but eager loads the chat object. So, it saves plenty of database calls when I need to iterate through the chats!

ChatUser.closed(chat_id).invite()

Invites all users to a chat with chat_id if the buddy closed the chat window.

Why named_scope?

  1. named_scope is not just a syntactic sugar. It generates much less query using WHERE IN clause instead of WHERE = clause. So, reduces the number of calls to a single call.
  2. Chaining the named_scope will generate only a single query for the chain. So, ChatUser.closed(chat_id).with_chat will produce only one query.
  3. You get rid of looping in many cases! It will help you to write concise and clear codes.
  4. When you can divide the objects in your classes in partitions, then you may wish to write the named_scope for partitioning the data. In my example, the :closed named_scope does this. So, I can add methods to this named_scope which actually works for the closed chats. It is kind of dividing your objects into named groups. So, it increases readability.

Let me know if you have another good example of a real life named_scope in your project.

Design/Code Review: Now, It’s Time to Realize!

Are you doing code reviews?

Thank God, you are.

Not?

Well, you are not alone. You know, most of your bugs could be killed by reviews. Most of your technical debts could be paid by reviews. Your team could collectively learn more by reviews…

Now, It’s time to realize!

Start now! It’s simple. I have found the following approach to be useful-

  1. Pair up with one developer. Make it explicit to the team. So, you know who reviews your code.
  2. Mix up a junior team member with a senior one, if possible.
  3. Have a fixed time, at least 30 minutes for each two weeks to do a team design/code review.
  4. Do it in small increments. We did the following-
    • Sprint#1: Review variable/method/class/file/folder names only.
    • Sprint#2: #1 + Review if/else blocks, loops.
    • Sprint#3: #2 + Review class responsibilities.
    • Sprint#4: #3 + Review test codes only.

The result is really worth

This first level feedback will help you to eliminate a lot of bugs, help in knowledge transfer and collective learning. Roughly, It takes only 10-15 minutes to review a code written in 3 days. I suggest, keep it simple.

One interesting thing is, many times I have found the developer him/herself actually found bugs/mistakes in the code before the reviewer at the time or reviewing.

A reviewer’s eye will save lot of your time and money.

Update: Be constructive in your feedback! Example:-

Worst: This code is a piece of …

Worse: This code is bad

Bad: This code is not good

Good: A better way is..

Better: We can also do it this way…

Best ?

Comments

Sohan
Yes, the positiveness or being constructive is very important. Thanks for reminding.
Arif
Nice and summarized post on a extremely useful topic like “Code Review”.It can be very effective if done regularly.

One attention i would like to draw regarding this.Code reviewers should be constructive and positive about the comments made on the code written by others.If done with a TEAM mentality and positive attitude,Code Review is a great process for the overall quality improvement of code.
Ashrafuzzaman
I agree.
Code review should be part of life.
It increases quality of code, the programmer and the reviewer himself.

Juggernaut on Linux

I just installed Juggernaut on my linux box. The installation is simpler than windows. At least it gave me less pain.

Just run the following commands-

  1. gem install json
  2. gem install activemachine
  3. gem install juggernaut
  4. juggernaut –g juggernaut.yml
  5. juggernaut –c juggernaut.yml

I got a little pain regarding the g++ compiler that is required to build the activemachine. I did not have it installed in my Amazon EC2 instance and after some searches, I could install it using the following-

apt-get install g++

IMPORTANT!!

Well, it was fine if I could end here! However, I found that this installation stopped my MySQL service and changed the /etc/mysql/my.cnf file. So, an attempt to start the MySQL was successful only after I removed the # comment from the line log_bin = blah blah blah…

So, if you are using Redhat Enterprise 5.0, you may find it useful. If you are using Amazon EC2, you will find it useful too. I do not know about other distros, but may be something similar will be there (at least hopefully)!

Comet and Server Push (Reverse Ajax) Technology

I am re-implementing an ajax based chat system. Presently an ajax request is polled continuously to look for any updates/messages from the user’s browser. However, the plan is to use the server to push the messages to the clients to off-load the server from a huge number of useless ajax calls.

I learned about Comet and found it interesting. You may read about comet here at WIKI.

Juggernaut is a rails plug-in that uses Flash to implement the server push technology. It is simple and it produces RJS script for the client. So, it can be used many other scenarios where a live update is required. of course, chat system is a good place for this.

The installation required a few tweaks as follows:-

  1. 1. Installed json_pure gem instead of json. (gem install json_pure)
  2. 2. Installed EventMachine gem (gem install eventmachine)
  3. 3. Installed juggernaut gem (gem install juggernaut –-ignore-dependencies)
  4. 4. Modified the C:\ruby\lib\ruby\gems\1.8\specifications\juggernaut-0.5.7.gemspec in places to the following

s.add_runtime_dependency(%q<json_pure>, [“>= 1.1.1”])
s.add_development_dependency(%q<hoe>, [“>= 1.3.1”])

s.add_dependency(%q<json_pure>, [“>= 1.1.1”])
s.add_dependency(%q<hoe>, [“>= 1.3.1”])

s.add_dependency(%q<json_pure>, [“>= 1.1.1”])
s.add_dependency(%q<hoe>, [“>= 1.3.1”])

since, you do not find the required versions of all the gems, you need to change this checks to get a way.

Then, this readme helped me to get started.

Will follow up this post as I work more on this. Have a good day!

The RANK() Function for Numbering on Groups/Partitions in SQL Server

Previously, I used the ROW_NUMBER() function to get the monotonically increasing row number of a result set from a SQL query. But, this time the requirement was a bit different as follows.

I have many combinations of Year, Make, Model and Trim of vehicles in my database. I also have potential profit on sale of each of the combinations. I need to produce, the top 5 trims for a Year, Make, Model combination that yields maximum profits.

So, to solve this problem, I cannot simply use ROW_NUMBER(). Basically, I need to find the row numbers starting from 1 for each group of Year, Make and Model. Then, take only those rows having a rank <= 5. Ranking based on such groups/partitions can be easily done by using the RANK() function. I am quoting the syntax from MSDN here-

RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )

So, in my case the query is something like the following-


RANK ( ) OVER ( PARTITION BY Year, MAKE, MODEL ORDER BY Profit DESC, Trim )


So, the RANK() is similar to ROW_NUMBER() in the context of the PARTITION. As if, the RANK() restarts counting from 1 for each partition. I think, like me, many of you may find it new and useful in similar requirements.


Without using this function, it will be much difficult to produce the desired result set. However, if you have a nicer/alternative solution, please teach me!

Working on a Data Warehouse Project

I have been busy for the last month as we, at Code71, rolled out the first release of the Business Intelligence project for Vuneu Media. I will give you a brief on my project here so that you can follow my coming posts.

The used vehicle dealers buy their vehicles from different Auctions across the country. When making a buy decision, they try to find answer to these questions-

  1. What vehicles are customers buying?
  2. How much it costs to get those from Auctions?
  3. How many of these are receiving cash advances and by which lenders?
  4. What is my potential gross profit if I deal a vehicle?
  5. Where can I find the vehicles in Auctions?

So, we are getting the information from data vendors. Since, the amount of data is really large (nearly 10 million rows per month in total) for human manipulation, we are adding real value to the business by providing software intelligence. We developed the technology to put the pieces together and make it handy for the dealers.

We are using the following software/technologies:-

  1. SQL Server 2008, 64 Bit, Standard Edition.
  2. SQL Server Integration Service.
  3. SQL Server Reporting Service.
  4. ASP.Net, C#, WCF.
  5. SQL Server Analysis Service (is in the pipeline, not used yet!)
  6. Windows Server 2008, 64 Bit.

I came across a number of innovations as well as issues at each step of the project. The database schema design was itself a challenge since data come from different vendors in various formats and granularities.

Here, I will start with an issue resulting from the “64 bit” SQL Server limitations-

SQL Server 64 bit versions include Integration Services, but it lacks support for some components. One such is, Microsoft Jet Driver. We use this driver to load data from Excel files.

To load Excel data using SSIS, you have the following options:-

1. Locate Program Files (x86)\Microsoft SQL Server\90\DTS\Binn\DTExec.exe. Then use this with your options. You can take a look at the command arguments by invoking DTExec –? This way you can run the SSIS packages using command line.

2. Using the SQL Server Agent Job is rather simple. Just Create/edit your job step having the Excel, then select Use 32 Bit in the Options/Execution Options tab. It will automatically use the 32 bit version of DTExec.

3. If you are using the IDE, you can turn of 64 bit by un-checking the Use 64 Bit Runtime at the Project Properties > Debugging.

More information on 64 bit SQL Server related issues are here at MSDN.

Also, I got help from Blog N-Technologies.

Comments

Tanvir Anowar
good post.keep it up.