Thursday, October 28, 2010

Code Generation in Ruby

While doing the code generation exercise (chapter 3, code generators) in the book The Pragmatic Programmer, I realized it quickly that C# isn't the best language to do code generation. The main block was that it is too verbose to do regular expression on strings. I had to use Regex.IsMatch and then Regex.Split to get the the tokens. Thats when I looked into ruby.

Regular expressions is a built in feature of Ruby, put between two forward slashes (/). You can use =~ operator for a regex match which sets the special variables $~.  $& holds the text matched by the whole regular expression. $1, $2, etc. hold the text matched by the first, second, and following capturing groups. Some good info here.

class CSharpCode
 def BlankLine
  print "\n"
 end
 
 def Comment comment 
  print "// #{comment}\n"
 end 
 
 def StartMsg name
  print "public struct #{name} {\n"
 end
 
 def EndMsg
  print "}\n"
 end
 
 def SimpleType name, type
  print "\t#{type} #{name};\n"
 end
 
 def ComplexType name, type, size
  if(type == "char[")
   type = "string"
   print "\t#{type} #{name};\n"
  end
 end

end

if __FILE__ == $0
 unless ARGV[0]
  print "cml usage: cml Input.txt\n"
  exit
 end

 if(File.exist?(ARGV[0]))
  CG = CSharpCode.new
  File.open(ARGV[0]).each_line { |line|
   line.chomp!;
   
   if(line =~ /^\s*S/)
    CG.BlankLine
   elsif line =~ /^\#(.*)/
    CG.Comment $1
   elsif line =~ /^M\s*(.+)/
    CG.StartMsg $1
   elsif line =~ /^E/
    CG.EndMsg
   elsif line =~ /F\s*(\w+)\s*(\w+\[)(\d+)\]/
    CG.ComplexType $1, $2, $3
   elsif line =~ /^F\s+(\w+)\s*(\w+)/
    CG.SimpleType $1, $2
   else
    print "Invalid line"
   end
  }
 end
end

Input File
 # Add a product
 # to the 'on-order' list
 M AddProduct
 F id   int
 F name   char[30]
 F order_code int
 E
Output
//  Add a product
//  to the 'on-order' list
public struct AddProduct {
 int id;
 string name;
 int order_code;
}

Wednesday, October 27, 2010

Playing with Google Charts

I wanted to insert a quick chart in my blog using charts from Google Docs after reading about new editor features. It turned out to be very easy. The best thing is that the editor automatically suggests a chart for you when you select data. And with charts, it is easy to visualize that my blog postings are on rise!

Tuesday, October 26, 2010

Hello to RubyMine

As part of starting my adventures in Ruby, I thought an IDE is essential and so settled with RubyMine (on a 30 day trial.) As my first program in ruby I just wanted to print "Hello World" from a static method.


The trick to use was keyword self. The scope of self varies depending on the context. Inside an instance method, self resolves to the receiver object. Outside an instance method, but inside a class definition, self resolves to the class.

class Hello
  def self.SayHello()
    puts "Hello World"
  end
end

Hello.SayHello;

And self is not the only way to create class methods. Here is a list of more.

Thursday, October 21, 2010

Code Contracts

I recently downloaded Code Contracts from Microsoft. I got introduced to Code Contracts in Pragmatic Programmer and then got a better understanding of it from Jon Skeet's C# In Depth while the chapter was freely available (unfortunately, it is not now.) Code Contracts is another tool which should have been here a long while ago in .net world.

In Code Contracts, Preconditions are specified using Contract.Requires to specify constraints on the input to a method. Postconditions are used to specify express constraints on the output of a method using Contract.Ensures. Here is a code snippet from C# in Depth, Chapter 15, which shows code contract in action

static int CountWhitespace(string text) 
{ 
    Contract.Requires(text != null, "text"); 
    Contract.Ensures(Contract.Result() >= 0); 
    return text.Count(char.IsWhiteSpace); 
}

Sunday, October 10, 2010

From Nu to NuPack

At last Microsoft gave some good news in the series of bad technologies they rolled out recently - WebMatrix, Lightswitch, NuPack. NuPack is good. It will be used - both by newbies and the veterans. Why? Because it makes life easier for developers and doesn't dumb you down. Inspired from Ruby gems, Nu had gotten some traction as package management system. I have used it and loved it but its new avatar as NuPack is even awesome. Integrated into Visual studio as power shell, it is addictive and I bet, as a positive side effect, we will start using shell more. When you add a package, it adds a reference and merges everything it needs in web.config. For a detail Introduction please follow the scott hanselman's Introducing NuPack Package Management for .NET - Another piece of the Web Stack

NuPack arrived with an interesting debugging tale by Tatham Oddie. It was a great post the way he narrowed down the exception using WinDbg and Reflector.

When I downloaded NuPack, the first I wanted to download was castle ActiveRecord, which unfortunately is not in the package list but there are ELMAH, Castle.Core, Catle.Ioc etc. So start using it and start saving your time.

Scott Gu's Announcing NuPack, ASP.NET MVC 3 Beta, and WebMatrix Beta 2 gives some good links for NuPack and Rob Reynold’s “Evolution of Package Management on .NET” Post (Rob is one of the leaders of the Nu project and is on the NuPack team) gives a good account of whats happening with Nu and NuPack.

Tuesday, August 10, 2010

Microsoft Matrix

My experience with Ruby is that of porting some code to C#. One thing I learned during porting is ruby code seemed much shorter! The other experience came when I tried ruby gems like project management called "Nu" and was immediately sold. This is how easy things should be in .net as well.

When I read about the Jimmy Schementi and the news of lack of commitment to IronRuby, it was again an feeling of abandonment by Microsoft. Microsoft is mostly interested in getting new people to .net world rather developing the ones already on it to higher levels. This article, though related to Microsoft.Data, has what I want to describe. Read the paragraph Thought 3: Shakespeare had it right.
Every Shakespeare play operates on three levels. The first level is embodied in a jester, who entertains the onlookers (mostly children) who are ill-equipped to understand what's going on in the plot. The second level is the plot, which entertains the onlookers who are capable of longer spurts of self-guided attention. And the third level is metaphorical, which entertains the onlookers who are capable of extracting a bottomless stream of interesting, non-explicit symbolic themes from the play's unraveling. Now, here's the kicker of it all: each level entices the onlooker to "upgrade" to the next level.
Microsoft is interested in level one. And it wants its developers to be at level one. While researching for this article found this post by Ayende
John Lam, the guy writing IronRuby, cannot look at the Ruby source code. That is the way Microsoft works. This is setting yourself up for failure, hard.
The post is more than 2 years old and it seems some things never change in Redmond (like Steve Ballmer). On the whole, it seems community wants it to be part of CodePlex Foundation.  JB Evain has some of his thoughts on IronRuby. But the sad reality is, as one of comments say,
If Microsoft doesn’t want the Iron* to have a success for .NET it just won’t happen.
I am curious as to what will be Microsoft's response. Perhaps it is time to leave Microsoft's matrix. Perhaps Google's App Engine is calling. If only they had C# and Visual Studio Integration!

Tuesday, August 3, 2010

10 Blogs to follow for .net web developers

Recently a friend asked which blogs I follow and to answer his question I thought of writing this post. Say "Ahey" if you like the list.

1. Ajaxian - I would look at this first thing in my Google reader. Sadly "Dion and Ben" are not its editors anymore.
2. Ayende @ Rahien - One of the charismatic .net developers, involved in NHibernate, RhinoMocks etc. Must for C# developers.
3. ScottGu's Blog is a good blog for .net developments.
4. Scott Hanselman is another Microsoft guy you need to follow. His ultimate developers tools list is still awesome to find stuff.
5. nettuts+ is good for its articles on web, CSS, jQuery, Html5 etc. - good for the web part.
6. Googland [dev] gathers posts from all official Google development blogs. A great resource to keep track of what's happening in Google world.
7. I like Jon Skeet from his book C# in Depth. He is not regular in blogging but I like his approach.
8. Karl Seguin seems tired of Microsoft technologies but does a good job of opening your mind.
9. High Scalability has good articles if you are interested in, well, high scalability.
10. devnetfx is my journey to become a better (10x) developer.

After compiling my list I thought it would be a good idea to share with others. So here is a link to the bundle so that you can subscribe easily - "DevNetfx" bundle

The bundle has 11 feeds as I added "dion" as well. Ahey! 

Edit: I have a lot more "Subscriptions" which I follow but the above ones covers a lot of ground from different perspectives, and yes, I do follow Coding Horror, Joel on Software but you already had them, didn't you :)