TAG | Programming
I came across this issue just at quitting time yesterday and was blown away when I realized what was happening.
The UsersController Index View (pre submit)

The UsersController Code
public class UsersController : Controller
{
List<string> users = new List<string> ()
{
"mace.windu",
"yoda",
"senator.amidala",
"anakin.skywalker",
"obiwan.kenobi"
};
public ActionResult Index()
{
return View( users );
}
[HttpPost]
public ActionResult Delete ( string[] userstodelete )
{
if ( userstodelete == null || userstodelete.Length == 0 )
{
throw new ArgumentException (
"argument must contain at least one entry",
"userstodelete" );
}
// code could go here to
// call out to some service to
// delete these users
TempData["deletedUsers"] = userstodelete;
foreach ( var user in userstodelete )
users.Remove ( user );
return View ("Index", users);
}
}
Problem
Looks like it should all work perfectly right? That’s what I thought. However, clicking “Delete Users” will only “delete” our pre-darth user “anakin.skywalker”.
Why?
Hint: Everything here is working exactly as it should.
code · debugging · html · MVC · Programming
I’ve been thinking about the whole Comments-as-a-code-smell argument for a very long time. When I first heard this idea, I was in the comments are definitely needed side of the argument.
“Who could hate comments so much that they would label them as a code smell?” I thought.
Well, I had an epiphany the other day.
I’ve never seen a block of code that actually needed a comment. Ever.
If you have a section of code that feels too complex, instead of adding a comment do any/all of the following and you can, in almost every case, reduce the need for comments by 100%:
- add a sourcecontrol commit comment
- do a simple refactor (Compose Method ftw people),
- use more descriptive variable/function/class/whatever names.
If you do all of these things and the code you are working on still seems too complex, you may have bigger problems with your overall architecture/design and adding a comment isn’t going to help. In this case it’s time to go back to the drawing board.
Also, everytime you commit a changeset into sourcecontrol that contains a section of code commented out god kills a kitten. Please think of the kittens.
code · code-style · opinionated · Programming
I’ve wrapped up work on v0.4 of the Jsoq Console, and the insanely strenuous release cycle for v0.1 of WhatDidIJustEat.com so I’m starting another side project tonight.
Ever been working with a programming language you’re not 100% familiar with and find yourself wondering:
Is this function a built-in function or in some included library?
What assembly is this class in again?
Can I name my class XXXX without conflicting with another class?
I’ve had these questions recently and found myself annoyed and frustrated to no end (PHP, I’m looking at you) so I’ve decided to build a system to keep track of this stuff for me :D.
The first thing on my list (because it was the easiest) was to do a data dump of all the public classes in the .Net Framework, including the ones in my GAC, and store some metadata for each one in a database table. I’ve just finished this step and thought that this data might be useful, so I’m posting it here.
Currently the fields included in the data dump are:
[namespace] = The namespace that the class exists in (ex: System.Collections.Generic)
[class_name] = The name of the class (ex: StringBuilder)
[assembly_fullname] = The display name of the assembly (ex: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
[assembly_file] = The full file path to the assembly (ex: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll)
[framework_name] = The framework that uses this class, for this dump they will all be 'Microsoft .Net'
[framework_version] = The framework version that uses this class (ex: v.2.0.50727)
The .sql file takes about 18 seconds to run to completion on my AMD Athlon X2 2.53ghz machine with 4gb of RAM.
If you’re curious about how I generated the .sql file, below is the code I used to find all the classes. It’s not pretty but then again it was just meant to get the data into the database. Just paste the code into a new console app and run it in a command window like so:
[application_name].exe > c:\classes.sql
Or, you can download the .sql file I’ve generated (it will also create the table you need to store the data).
static void Main(string[] args)
{
var dictionary = new Dictionary(){
{ "v2.0.50727", @"C:\Windows\Microsoft.NET\Framework\v2.0.50727" },
{ "v3.0", @"C:\Windows\Microsoft.NET\Framework\v3.0" },
{ "v3.5", @"C:\Windows\Microsoft.NET\Framework\v3.5" },
{ "v???", @"c:\windows\assembly\gac" }
};
var types = new List();
foreach (var pair in dictionary)
{
var path = pair.Value;
var assemblies = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
foreach (string file in assemblies)
{
try
{
Assembly asm = Assembly.LoadFile(file);
foreach (Type t in asm.GetTypes())
{
if (t.IsPublic)
{
Console.WriteLine(String.Format(
"INSERT INTO Classes (" +
"[namespace], " +
"class_name, " +
"assembly_fullname, " +
"assembly_file, " +
"framework_name, " +
"framework_version" +
") " +
"VALUES (" +
"'{0}', " +
"'{1}', " +
"'{2}', " +
"'{3}', " +
"'Microsoft .Net', " +
"'{4}'" +
")",
String.IsNullOrEmpty(t.Namespace) ?
"GLOBAL" :
t.Namespace,
t.Name
.Replace("`1", "")
.Replace("`2", "")
.Replace("`3", ""),
t.Assembly.FullName,
t.Assembly.Location,
pair.Key == "v???" ?
t.Assembly.GetName().Version.ToString() :
pair.Key
));
}
}
}
catch { }
}
}
}
If you found this useful, let me know in the comments!
.NET · Open Source · Programming · SQL
During the first round of development for JSOQ I needed a faster way to test than unit-tests alone could provide. What I needed was a dirt-simple page that I could run in a browser, enter some JavaScript code, click a button and see it execute.
I built the initial JSOQ Console (which is still included with the JSOQ source code) in about 20 minutes and it increased my productivity by 10x.
It was much faster to test JSOQ functions with the JSOQ Console than it had been with unit-tests. As a result I was able to prototype new functionality in about 1/2 the time.
I’ve finally moved v0.2 of the Jsoq Console to my blog here so you can use it to quickly test out your own JavaScript code!
How it works is pretty straight forward. If you’ve got a snippet of JavaScript, be it jQuery (for the moment jQuery is the only external library that is supported but that will change soon), JSOQ or any other javascript code, you can run it with the JSOQ Console.
Just navigate to http://tinyurl.com/jsoqconsole or http://codeimpossible.com/jsoq for those of you that like to type, paste your code into the textarea at the top of the page, press “Run” and your code will run!
So enjoy! If you get confused there are also samples that will show you how to use the more “advanced” features. Also, drop a comment or share the console with others if you thought it was useful!
Earlier today I had to debug a function in our code that was calling an external webservice at a clients’ site. The webservice returns a list of items and the code on our end is supposed to place them in ascending order based on each items Order property.
The client pointed that the our order wasn’t matching up with what they were seeing internally so I spoke with their developer who suggested that we make sure that the request wasn’t being cached on my machine.
I opened up Fiddler locally and was surprised to see that the none of the request/response data for the connection to the webservice was showing up.
Fiddler, for those that don’t know is a really excellent Http tracing application that allows you to see what kind of http traffic is going in and out over your network connection. You can download fiddler here.
Thankfully, Fiddler runs a simple proxy service which you can point your service requests to in .net using:
if (System.Environment.MachineName.ToLower().Equals("MyMachineName"))
{
service.Proxy = new System.Net.WebProxy("http://localhost:8888");
}
After adding that code we were off and debugging our .Net service requests in Fiddler!
asp. · asp.net · c# · Programming · webservices
