The Weekly Source Code 12 - Back in Black Edition
It's been a while since the last Weekly Source Code, but I have the holidays and the preponderance of baby poop as a fine formal excuse. I'm back from Paternity now and the Source Code will keep coming weekly as originally promised.
If you're new to this, each week I post some snippets of particularly interesting (read: beautiful, ugly, clever, obscene) source and the project it came from. This started from a belief that reading source is as important (or more so) as writing it. We read computer books to become better programmers, but unless you're reading books like Programming Pearls, you ought to peruse some Open Source projects for inspiration.
And so, Dear Reader, I present to you the twelfth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading while changing diapers.
- Looking for applications that use WPF? They are out there and they don't always have "juicy buttons" and scream "I was written in WPF." Sometimes they are just clean, simple and functional. Witty is a Twitter (chat-esque) client written in WPF, a technology I have yet to fully grok.
This little app has a simple Twitter Client Library that includes inside it a TinyUrl maker. (Google better buy TinyUrl or the whole web might get link rot) Sometimes it's fun to read source for the comments, but it's also cathartic to read code that you didn't write and say to yourself, "hey, this could use some refactoring." Perhaps a good time for you to offer to help an Open Source Project.
This example is the most trivial chunk of code in Witty, but somehow it made me smile. Regardless, the MainWindow of Witty also had some interesting stuff, particularly the background fetching of data. It's definitely easier than WinForms.public void UpdateRelativeTime() { DateTime StatusCreatedDate = (DateTime)dateCreated; TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - StatusCreatedDate.Ticks); double delta = ts.TotalSeconds; string relativeTime = string.Empty; if (delta == 1) { relativeTime = "a second ago"; } else if (delta < 60) { relativeTime = ts.Seconds + " seconds ago"; } else if (delta < 120) { relativeTime = "about a minute ago"; } else if (delta < (45 * 60)) { relativeTime = ts.Minutes + " minutes ago"; } else if (delta < (90 * 60)) { relativeTime = "about an hour ago"; } else if (delta < (24 * 60 * 60)) { relativeTime = "about " + ts.Hours + " hours ago"; } else if (delta < (48 * 60 * 60)) { relativeTime = "1 day ago"; } else { relativeTime = ts.Days + " days ago"; } RelativeTime = relativeTime; }
- Folks are poking at C# 3.0 trying to get lambdas and anonymous type declarations to feel and act like Ruby hash table declarations. It'll be interesting to see if Anders is already on top of this. I wonder if he and Matz hang out? If I invented a language, I'd make a really exclusive clubhouse. ;)
Eilon shows the difference between Dictionaries and anonymous types in his proposal:
- This is some ugly code:
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("key1", "value1");
values.Add("key2", "value2");
values.Add("key3", "value3");
GetHtmlLink("Click me", values);My proposal: Have the method accept a parameter of type object. Callers could pass in a type that has properties with the appropriate names and values. They can use C#'s object initializer syntax to save some space:
MyParams myParams = new MyParams { Key1 = "value1", Key2 = "value2", Key3 = "value3" };
GetHtmlLink("Click me", myParams);However, there was the added work of defining the MyParams type. Admittedly, it wasn't that hard with C# 3.0's automatic properties, but I hate defining types that are used in only one place. If the user can pass in an arbitrary object with properties, why not let that object be of an anonymous type? Here's the final code:
GetHtmlLink("Click me", new { Key1 = "value1", Key2 = "value2", Key3 = "value3" });
Woah! We went from five lines of code with dictionaries to two lines of code with object initializers (minus the type definition), to just one line of code with anonymous types!
- Bill asks for better Dictionary Initializers, and Alex does it with lambdas in his post, giving him this clean syntax, and PhilHa weighs in with his thoughts. Bill says "Your inner Rubyist is nodding with approval."
Dictionary<string, object> items = Hash<object>(Name => "alex", TargetType => typeof(Uri), Id => 10);
Assert.AreEqual(10, items["Id"]); - If you're looking to do small GUI applications, you might take a moment to look at "Shoes" the tiny Ruby UI Tookit. Why? Well, if you're an old WinForms or new WPF guy like myself, (or an old Java guy, also like myself) it's helpful to other perspectives on what a Domain Specific Language for UI might look like. The sample source is here.
Here are four different snippets in four different language that show a single button with a click event handler that shows a message.
Here's the same thing in Shoes. Note that the documentation for Shoes is available as a Ransom Note. The creator never uses the acronym "GUI" in the docs or materials, but prefers to think of Shoes as a "toy." It's a pretty near toy, take a look at the 2D animation examples.Shoes.app { button("Press Me") { alert("You pressed me") } }
-
Rob (and lots of other folks) are exploring what UserControls look like in an ASP.NET MVC world. He says "Your UserControl can be one of two things: A granular bit of UI that renders information passed from a Controller [or] a granular bit of UI that renders information from an application-wide data source." He calls them a "Viewlet" and offers these helper methods:
Rendering a ViewUserControl
The MVC Toolkit has a nice method called “RenderUserControl()” that allows you to process your ViewUserControl and output it’s result inline:<%=Html.RenderUserControl(“~/UserControls/UserList.ascx”)%>
If the ViewUserControl is typed (say ViewUserControl<MyControllerData>), then it’s ViewData object will be filled for you, and your ViewPage and rendered control will share the same data.
If you want to be explicit about it, you can do that as well, specifying the data to pass:
<%=Html.RenderUserControl(“~/UserControls/UserList.ascx”,ViewData.Users)%>
Finally, if you need to set properties on the ViewUserControl, you can do that as well by passing in an anonymous type:
<%=Html.RenderUserControl(“~/UserControls/UserList.ascx”,ViewData.Users, new {GroupID=2})%>
Enjoy, and keep reading code!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
You haven't blogged about DLR and IronRuby / IronPython so far.
Kindly blog at length covering few codes, with the above dynamic languages that are coming our way in near future.
We do not see any discussion on this powerful languages, which is really needed.
Few words on Dynamic VB shall be welcomed too. I was happy to see the discussions on PHP and DLR have started. The DLR seems to be the future, and all dynamic languages shall one by one step into DLR.
Pl. spread the requested news and current stage of their progress.
Thanks
Can you cover an interesting topic like.... " Asp.Net MVC on the DLR with IronRuby. "
Such a topic is not seen anywhere discussed, and will bring few smiles on Rubyists, who are working on other platforms and are just tied up with ROR as an only solution.
Let the Ruby Community know, what coming their way soon.
BTW... are we getting any Beta versions of dynamic languages in near future. DLR and Dynamic lamguages were announced at last MIX 2007 and the New MIX 2008 is round the corner ( March 2008 )
Thanks
I recently wrote about how I like to do Dictionaries in C#3.0. I created an extension method which converts an anonymous type to a dictionary, so you can do this:
var myObject = new { Name = “Frank”, Age = 5 };
var dict = myObject.ToPropertyHash();
Here, dict would be a dictionary containing keys "Name" and "Age".
The full post is on my blog at http://richardbushnell.net.
Love the series, btw.
I like how you state this is an infinite series. I've learned from & enjoyed these posts.
HanselMinutes Fan,
Catto
I didn't spend much time on that particular method. It is a quick modification of http://twitter.pbwiki.com/RelativeTimeScripts to match how Twitter display the relative time on the twitter.com when they deprecated the relative_created_at attribute in the API.
GetHtmlLink("Click me", myParams); is not something new, no need to go far to see very elegant code like this, just look at the code produced by the form designer whenever say a combobox is used with items populated by hand from the property pages (F4).
this.comboBox1.Items.AddRange(new object[] {
"dsfs",
"sdfsdf",
"sdfsf"});
I thought you might be able to shorten the Array initialization a bit more now. I know you can do this (note the missing "new string[]"):
string[] myArray = {"one", "two", "three"};
this.comboBox1.Items.AddRange( myArray );
But you can't do this:
this.comboBox1.Items.AddRange( {"one", "two", "three"} );
But you can do this:
this.comboBox1.Items.AddRange(
new[] {"one", "two", "three"}
);
At least it's a little nicer than before.
var dictionary = D<string, string>()
["Key1", "value1"]
["Key2", "value2"]
["Key3", "value3"];
Implementation is left as a weekend exercise for the reader. ;)
Nice article, actually i got this link on Start page of Visual studio - and it sounds great idea and column.
one small suggestion
I am from India, and y`day we celebrated Republic day (26th Jan) in India and when i opened this link and went down i got the same day !!!!!!! (26 jan 2008)...
may be they can embad the feature of proper time zone rendering
--- thank
Pradeep
I'm no Ruby person, in fact never even touched it so I can't say much about the yearn to program in the style of a dynamic language, but looking at it from a .net perspetive I can see how dangerously wrong this is. Unless someone can prove that the reflection engine has improved its performance greatly in 3.5 to cater for its potential more frequent due to the introduction of these C# 3.0 features, then I don't see how anyone could fathom using it so loosely. Perhaps it's not so much of a problem for desktop applications, but I'd stay away from freely using reflection like this in an asp.net app for instance.
We all want clearer code, but I think performance should never take a hit just so that we can have five lines of code transformed into one with no apparent gain. Unless there is something specific about the code that will indeed become a critical maintenance issue down the road and there is no other way, but to refactor it using extreme measures that affect the application in one way or another.
There is always a trade-off and that's what everyone should keep in mind when trying to do pretty things. (although I must admit it is quite pretty!)
Sir;
i want to create one my own small site on my home. Such that from that page i can send mails to another account.
thre should be option of
from id :- textbox
to id :- textbox
subject :- textbox
message :- textbox
send
thnks u
raman singla
Comments are closed.
Right now the biggest need in terms of refactoring is the way Witty makes web requests. There's a lot of !DRY there. We're also trying to work around some of the quirks of WPF.