January P2P Journal links

Latest issue of P2P Journal is out, you can download the PDF here.

Remaindered links from the article:

Some Specific Projects in Collaborative Computing: http://dsonline.computer.org/collaborative/projects/projects.html

Peer To Peer Working Group, “Bidirectional Peer-to-Peer Communication with Interposing Firewalls and NATs”: http://www.peer-to-peerwg.org/tech/nat/Docs/NATWhitePaperv095.pdf

P2PWall – IPTables blocking of P2P traffic: http://www.lowth.com/p2pwall/

CTCP (Client To Client Protocol): http://www.irchelp.org/irchelp/rfc/ctcps

JOSE (stands for Java Open Source Exchange) is envisioned as a peer-to-peer network that allows developers to search, view and download source code made available by other peers: http://jose.jxta.org

New P2P applications and betas released:
· Skype
· Kappie

Neural Network Markup Language: http://www.nnml.alt.ru/index.shtml

Accessing ASP.NET DropDownList Items from Code behind

Couple days ago I was writing an ASP.NET webform and I wanted to programmatically inspect the items in the drop down. The drop down list in the ASP.NET page looks something like this:

<asp:dropdownlist id="mydropdown" class="dropbox" runat="server">
  <asp:listitem text=" -- select -- " value="" />
  <asp:listitem text="Text 1" value="1" />
  <asp:listitem text="Text 2" value="2" />
  <asp:listitem text="Text 3" value="3" />

and then in the code behind (like I mentioned before) you can do a bunch of useful things. First, if I wanted to set the default selected item in the form, I’d write this:

public void Page_Load(Object sender, EventArgs e) {
  // initialize form
  if (!IsPostBack) {
   mydropdown.SelectedValue = "1";

That’s relatively trivial, it gets more interesting when you consider that you can access the DropDownList items programatically:

foreach (ListItem l in mydropdown.Items) {
  if (l.Value == 1) {
   l.Selected = true;

In the above example I use the foreach statement to iterate through the collection of ListItems in the Items property. If the Value property of the ListItem is 1, then I set that to be selected. You can also access the Text property of the List Item.

The C# foreach construct jogged my memory about Java 1.5 (Tiger) which is supposed to have enhanced ‘for’ loops (also read this); for example this:

for (Iterator i = c.iterator(); i.hasNext(); ) {
  // do stuff with 'i'

becomes this:

for (Object o : c) {
  // do stuff w/ 'o'

When is 1.5 gonna arrive?

Northeast December Storm

Two weeks ago we got dumped on here in Boston (we got about 28 inches, although some parts of MA got more than 36 inches). Karen and I just moved down to Canton and we’re living right next to a pond, so I snapped a couple pictures of what it looked like after the storm:

Picture of the front of our new place after the big storm in December

Picture of the pond in our backyard during the day after the big storm

Picture of the pond in our backyard during the evening after the the big storm

The first is a picture of our front lawn, the ‘bumps’ on the right side of the picture are cars. The second two are pictures of the pond in our backyard, one taken in the morning and one snapped in the evening. I’m working out of our house right now, so I look right out onto this pond while I’m hacking. It’s beautiful. Click on the pictures for high-res shots if you’re one of those types.

ASP.NET TextBox MultiLine Incorrect Documentation

[Update: The link to the TextBox class from the ASP.NET TextBox Web Server Control points to the Windows Forms TextBox, not the Web Controls TextBox. Balls!]

If you’re running into trouble when using the TextBox class in ASP.NET, the MultiLine property and the Microsoft .NET Framework SDK Documentation, it’s probably because the documentation is wrong. The documentation that ships with v1.1 says that you can use the ‘Multiline’ property of the TextBox class (which is actually a property of the TextBoxBase class) to toggle the TextMode property like this:

private void CreateMyMultilineTextBox() {
  // Create an instance of a TextBox control.
  TextBox textBox1 = new TextBox();

  // Set the Multiline property to true.
  textBox1.Multiline = true;

If you do what the documentation says, you’ll get a compiler error that says this:

error CS0117: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Multiline'

So instead, use the the TextBoxMode enumeration and the TextMode property:

private void CreateMyMultilineTextBox() {
  // Create an instance of a TextBox control
  TextBox textBox1 = new TextBox();

  // use the TextMode property and the TextBoxMode enumeration
  textBox1.TextMode = TextBoxMode.MultiLine;

All of this is documented correctly online.

Indexing Database Content with Lucene & ColdFusion

Terry emailed me a couple days ago wondering how he could use ColdFusion and Lucene to index and then search a database table. Since we’re completely socked in here in Boston, I had nothing better to do today that hack together a quick snippet that does just that:

<cfset an = CreateObject("java", "org.apache.lucene.analysis.StopAnalyzer")>
<cfset an.init()>
<cfset writer = CreateObject("java", "org.apache.lucene.index.IndexWriter")>
<cfset writer.init("C:\mysite\index\", an, "true")>
<cfquery name="contentIndex" datasource="sample">
select label, description, id
FROM product
<cfloop query="contentIndex">
  <cfset d = CreateObject("java", "org.apache.lucene.document.Document")>
  <cfset fld = CreateObject("java", "org.apache.lucene.document.Field")>
  <cfset content = contentIndex.description>
  <cfset title = contentIndex.label>
  <cfset urlpath = "/products/detail.cfm?id=" & contentIndex.id>
  <cfset d.add(fld.Keyword("url", urlpath))>
  <cfset d.add(fld.Text("title", title))>
  <cfset d.add(fld.UnIndexed("summary", content))>
  <cfset d.add(fld.UnStored("body", content))>
  <cfset writer.addDocument(doc)>
<cfset writer.close()>

The only real change from the code that I wrote previously to index a document was that instead of looping over the file system looking for documents, I loop over a query and then indexed the text of a column from the database rather than the text of a document. (I would have written in in CFScript, but you can’t do queries from CFScript yet, unless you use a UDF to do the query)

You can download the source here, if you’re so inclined.

Optimal number of arguments to a method or function

Bryan Hogan posted a thought provoking blurb a couple weeks ago where he postulated that passing a data structure as the parameter to a method is better than passing numerous parameters. Specifically, he said: “If you have more than say two arguments that are needed to be passed into a function, use argumentCollection!” In his opinion this is better:

email = structnew();
email["subject"] = "test subject";
email["to"] = "to@test.com";
email["from"] = "from@test.com";
email["content"] = "check this out!";
email["type"] = "text";
// send the email using the SendEmail() function

than writing this:

SendEmail("test subject", "to@test.com", "from@test.com", "check this out", "text");

His hypothesis didn’t sit right with me so I picked up my Code Complete book and went to see what Steve had to say about the subject. Chapter 5, “Characteristics of High-Quality Routines”, mentions that there are 5 levels of coupling between routines: simple-data coupling, data-structure coupling, control coupling, global-data coupling, and pathological coupling. Bryan’s code sample seemed to me to be an example of data-structure coupling, which is defined as “.. two routines are data-structure-coupled if the data passed between them is structured and is passed through a parameter list.” (it’s also called stamp coupling for anyone who cares). The example that uses multiple parameters is an example of simple-data coupling: “… two routines are simple-data-coupled if all the data passed between them is nonstructured and it’s all passed through a parameter list. This is often called ‘normal coupling’ and is the best kind.’

These definitions certainly don’t prove Bryan’s hypothesis, but they don’t disprove him either. It seems then that the real juice of his argument is that one or two parameters is better than four or five. Chapter 5, section 7 of Code Complete, titled “How to Use Routine Parameters” has this to say: “Limit the number of a routine’s parameters to about seven… If you find yourself passing more than a few arguments, the coupling amoung your routines is too tight. Design the routine or group of routines to reduce the coupling.

Stepping outside of academia for a second, I’d personally much rather use a method where either:

a) I can read the parameter list and know exactly what is required. For example:

sendEmail(string to, string from, string subject, string body, string type)

is concise and explicit. It leaves nothing for me to mess up.

or b) I can pass an object (which presumably has either defined getters or publicly accessible instance variables). For example, I might define an email package:

EmailPackage email = new EmailPackage(to, from, subject, body, type);

and then send that package to the method:


Conclusion? Using more than 2 parameters is definitely not a bad practice, using a data structure instead of a number of parameters is sometimes a good practice, but not the best. Thinking about you should best structure your code? Priceless. Keep up the great thoughts Bryan.

Why doesn’t ASP.NET include an <asp:query> tag?

Ray asked a great question a couple weeks ago that I meant to reply to but just never got around to it. He asked why ASP.NET doesn’t include an <asp:query> tag to enable developers to quickly embed SQL queries into an ASP.NET page. The responses seemed to agree with Ray’s assessment that the lack of an <asp:query> tag was shortcoming of ASP.NET, but I think it’s something more than that. If you look closely at the tags (oops! ‘controls’) that Microsoft chose to include in the System.Web.UI.WebControls namespace, you’ll notice that *none* of them give you the ability to declare variables, read a file, query a database, or send email. In fact, all of the controls are about outputting either form components (buttons, drop down lists, checkboxes, etc..) or regular HTML (like images, table cells, table rows, etc.) Microsoft didn’t leave it out because they didn’t have time or money to do it, they left it out because they don’t think you should be writing web applications using tags/controls. Using a code-behind class and controls leaves you with a much cleaner page, one that contains very little, if any scripting code and one where the design is truly separated from the logic. Not one to shy away from an example, compare these two blocks of code that create a drop down list in a form from a query. First in ColdFusion:

<cfquery name="users" datasource="#mydatasource#">
select id,username FROM users
<select name="userid">
<cfloop query="users">
<cfoutput><option value="#userid#">#username#</option></cfoutput>

and then in ASP.NET:

// in code behind class, MyDBWrapper.Query returns a DataSet object
DataSet users = MyDBWrapper.Query("select id, username FROM users");
userid.DataSource = users;

<!--- asp.net page --->
<asp:dropdownlist id="userid" runat="server">

That’s a pretty lame example, but the main takeaway from it is that all of the ASP.NET controls can be manipulated from the code behind class, which is something you cannot do in ColdFusion. You can declare a drop down list in your code behind, bind it to a datasource, modify the selected value, toggle it’s visibility, or modify any of the other 20 or so properties that exist for a drop down list. To me this provides true separation of presentation from logic. Page designers can drop in ASP.NET controls and I can sit in a dark room and modify anything and everything on those controls from my codebehind. You can’t do that in ColdFusion.

Does this mean that ASP.NET is better than ColdFusion? Nope. Notice that I have to create a code behind (technically I could write my code in a <script runat=”server”> block) and that I have to create my own database wrapper class. ASP.NET takes a little bit more work. ColdFusion is much faster to develop in but arguably encourages you to intermingle code with display. ASP.NET takes a bit longer, but developed correctly, encourages you to separate your logic and display code.