Friday, March 23, 2007

Declarative vs. Imperative

Recently i was talking to Michael Silver about declarative vs. imperative programming; he found an article on the topic saying that declarative programming is overrated and that "languages like Ruby" allow writing imperative code that "looks" and "feels" declarative. The article didn't give any examples, but Michael sent me an example using ActiveRecord to define a database schema.

   1: ActiveRecord::Schema.define(:version => 2) do
   2:  
   3:   create_table "comments", :force => true do |t|
   4:     t.column "body", :text
   5:     t.column "post_id", :integer
   6:   end
   7:  
   8:   create_table "posts", :force => true do |t|
   9:     t.column "title", :string
  10:     t.column "body", :text
  11:     t.column "created_at", :datetime
  12:     t.column "author_name", :string
  13:     t.column "comments_count", :integer, :default => 0
  14:   end
  15:  
  16: end

After a while of figuring out the semantics of the Ruby syntax, i realized that it isn't even a *language* feature that the article was touting - it's simply a "fluent API" which is apparently fairly common in Ruby libraries.



So I thought I would try to create a library in .Net that would "feel" very similar to the Ruby style.



So, here's the same code in VB.Net...




   1: With ActiveRecord.DefineSchema().AsVersion(2)
   2:  
   3:     With .WithTable("comments").UseForce()
   4:         .WithColumn("body").AsType(Of String)()
   5:         .WithColumn("post_id").AsType(Of Integer)()
   6:     End With
   7:  
   8:     With .WithTable("posts").UseForce()
   9:         .WithColumn("title").AsType(Of String)()
  10:         .WithColumn("body").AsType(Of String)()
  11:         .WithColumn("Created_at").AsType(Of Date)()
  12:         .WithColumn("author_name").AsType(Of String)()
  13:         .WithColumn("comments_count").AsType(Of Integer).UseDefault(0)
  14:     End With
  15:  
  16: End With


...and here's the same code in C#...




   1: ActiveRecord.DefineSchema(delegate(ARSchema s)
   2: {
   3:  
   4:     s.WithTable("comments", delegate(ARTable t)
   5:     {
   6:         t.WithColumn("body").AsType<string>();
   7:         t.WithColumn("post_id").AsType<int>();
   8:     }).UseForce();
   9:  
  10:     s.WithTable("posts").UseForce(delegate(ARTable t) 
  11:     {
  12:         t.WithColumn("title").AsType<string>();
  13:         t.WithColumn("body").AsType<string>();
  14:         t.WithColumn("Created_at").AsType<DateTime>();
  15:         t.WithColumn("author_name").AsType<string>();
  16:         t.WithColumn("comments_count").AsType<int>().UseDefault(0);
  17:     });
  18:  
  19: }).AsVersion(2);

The VB and the C# versions both take advantage of the language features.  VB supports the With statement, while C# supports anonymous delegates.  Both of these scenarios were enabled with overloaded methods within the same library.

Thursday, March 15, 2007

How To Post Code From Visual Studio 2005 To Your Blog

You would not believe how much time we've spent trying to find an easy way to post code samples to this blog. Research (via Google) has proven that we're not the only ones who have spent countless hours trying to do what should be very simple.

Now that we've figured out an excellent way to do it, we thought we'd share it with everyone else.

Step-By-Step Instructions:

1. Download, install, and run Windows Live Writer. Setup your blog following the wizard.

2. Download and install the Code Snippet Plugin for Windows Live Writer.

3. Open Visual Studio 2005.

4. Copy the code from Visual Studio 2005 that you want to post to your blog.

5. Switch to Windows Live Writer. Make sure you're in Web Layout mode. Click the link on the sidebar that says Insert Code Snippet.



6. When the Code Snippet window pops up, paste your code into the top window.




7. Be sure Embed Styles is highlighted (it's between the top and bottom windows). This will make it so that the code will retain its formatting on your blog.

The end result will look something like this:

public string SayHello(string Name)
{
return "Hello: " + Name + "\n\nWe are glad you have decided to read posts on the Develocity blog. We are trying to bring you quality, helpful stuff on here.";
}



You can adjust the width and height of the code box from within Windows Live Writer.




That's it! It wasn't too painful, I hope.

Tuesday, March 13, 2007

The Ten Commandments

(from http://builder.com.com/5100-6404-1045782.html)

What we need is a set of rules or guidelines to help developers keep themselves (their egos, actually) separate from their code. Hence our Ten Commandments for Egoless Programming, which you can also download in handy "stone tablet" format:

  1. Understand and accept that you will make mistakes. The point is to find them early, before they make it into production. Fortunately, except for the few of us developing rocket guidance software at JPL, mistakes are rarely fatal in our industry, so we can, and should, learn, laugh, and move on.
  2. You are not your code. Remember that the entire point of a review is to find problems, and problems will be found. Don't take it personally when one is uncovered.
  3. No matter how much "karate" you know, someone else will always know more. Such an individual can teach you some new moves if you ask. Seek and accept input from others, especially when you think it's not needed.
  4. Don't rewrite code without consultation. There's a fine line between "fixing code" and "rewriting code." Know the difference, and pursue stylistic changes within the framework of a code review, not as a lone enforcer.
  5. Treat people who know less than you with respect, deference, and patience. Nontechnical people who deal with developers on a regular basis almost universally hold the opinion that we are prima donnas at best and crybabies at worst. Don't reinforce this stereotype with anger and impatience.
  6. The only constant in the world is change. Be open to it and accept it with a smile. Look at each change to your requirements, platform, or tool as a new challenge, not as some serious inconvenience to be fought.
  7. The only true authority stems from knowledge, not from position. Knowledge engenders authority, and authority engenders respect—so if you want respect in an egoless environment, cultivate knowledge.
  8. Fight for what you believe, but gracefully accept defeat. Understand that sometimes your ideas will be overruled. Even if you do turn out to be right, don't take revenge or say, "I told you so" more than a few times at most, and don't make your dearly departed idea a martyr or rallying cry.
  9. Don't be "the guy in the room." Don't be the guy coding in the dark office emerging only to buy cola. The guy in the room is out of touch, out of sight, and out of control and has no place in an open, collaborative environment.
  10. Critique code instead of people—be kind to the coder, not to the code. As much as possible, make all of your comments positive and oriented to improving the code. Relate comments to local standards, program specs, increased performance, etc.

PasteBin - easy way to share source code

If you've ever wanted to share source code with someone, here's a better way than IM or email. It allows longer snippets than IM normally does and includes syntax highlighting.

http://pastebin.com/

I'm going to try using it to post source code on this blog using a "private" PasteBin at http://develocity.pastebin.com.

(btw, this was posted with Word 2007)