{ Code: Impossible } | this = HowI.Roll();

Jul/10

16

Comments stink!

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%:

  1. add a sourcecontrol commit comment
  2. do a simple refactor (Compose Method ftw people),
  3. 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.

· · ·

The Asp.net Mvc 2.0 RTM came out last month and a lot of people are converting their projects over. If you’re just starting to manually move your projects over then stop what you are doing, download and run the Mvc Converter. It will save you eons of time and frustration.

If you are like me, however, and started porting your project over manually and are now knee deep in WTFBBQ sauce then follow the steps below and your project should be up and running in no time.

1. Back up your project. Just in case.

2. Open your project file(s) inside your favorite text editor (one with a decent find/replace system). Open the Find & Replace dialog and find "603c0e0b-db56-11dc-be95-000d561079b0", replacing it with "F85E285D-A4E0-4152-9332-AB1D724D3325". My project turned up 1 result.

3. Open the Web.Config files in the root of the project and the root of the /Views folder. Open the Find & Replace dialog again, this time searching for "System.Web.Mvc, Version=1.0.0.0" and replacing it with "System.Web.Mvc, Version=2.0.0.0".

4. Add the following BindingRedirect to the bottom of the root Web.Config, just before the </Configuration> node.


<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
      <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

5. Open the solution in Visual Studio and replace the references to System.Web.Mvc 1.0 with the 2.0 assembly.
6. Finally, and only if you really need them, open a new MVC 2.0 project and copy all the files in the /Scripts folder to your project.

Enjoy your freshly migrated project!

· · ·

Apr/10

2

PAX East in Pictures

· ·

Mar/10

10

Lessons Learned

lessons learned this morning:
a) Even if it’s simple code you “know” will work you must test it. No excuses.
b) In programming, nothing happens by “magic”. There is a tangible reason that that thing that should be working isn’t.
c) Don’t assume something couldn’t be true simply because it doesn’t make any sense.

·

Mar/10

7

W.O.M.M. #6 – enumerateOver()

works-on-my-machine-starburst

I’ve been focusing more and more on my port of Ling-to-Objects, Jsoq the past few weeks. It’s still in really early stages and I’m not quite sure about it’s actual usefulness but I’m learning a lot about JavaScript and having a ton of fun along the way!

Jsoq deals with arrays a lot. About 95% of it’s use cases involve either looping through, altering, or creating arrays. Having a ton of for loops in my code just seems so… not right. For loops have always seemed dirty to me. They just aren’t elegant enough.

Here is your normal, average, everyday for loop in Javascript.


var array = "1,2,3,4,5,6,7,8,9,10".split(',');

for(var i = -1, l = array.length; ++i < l;) {
    alert(array[i]);
}

This works. It’s reliable and gets the message across. But what if we needed to loop over an array and get all the items that matched a condition? Using a for loop the code could look something like:


var array = [ 1, 2, 3, 4, 5, 1, 2, 3, 4 ];
var results = [];
var condition = function(i) { return i%2 == 0; };

for(var itt = -1, len = array.length; ++itt < len;) {
	if( condition(array[itt]) ) {
		results.push(array[itt]);
	}
}

This does work, I’ve written code like this many times before, and while technically there isn’t anything wrong with it I think there is still room for improvement.

Jsoq is going to be handling arrays all over the place so the solution to this problem needs to be simple.

Here’s what I need:

  • to loop over an entire collection and perform an action on each item.
  • If that action produces a result, the item is to be pushed into an array and returned to after the loop is done
  • .

  • Also: it needs to be readable, someone else coming along should be able to determine what this thing is doing without too much difficulty. So I had my work cut out for me.

A few hours later I had a decent function that I could use to replace a lot of the for loops I had. After some more refactoring I was able to wipe them all out and replace them with calls to enumerateOver(). Here is the latest version from source control:


function enumerateOver(collection, work) {
	var result = [], val = [];

	if (isArray(collection)) {
		try {
			for (var i = -1, l = collection.length; ++i < l;) {
				result = work(collection[i], i);
				if (typeof result !== "undefined" && result != null) {
					val.push(result);
				}
			}
		}catch (e) {
			if (e != jsoq.$break) throw(e);
		}

		if (val.length > 0) {
			return val;
		}
	}else {
		try {
			val = work(collection, 0);
		}
		catch (e) {
			if (e != jsoq.$break) throw(e);
		}
		if (typeof val !== 'undefined') {
			return val;
		}
	}
	return result == null ? [] : result;
}

And here is the code to replace the for loops above, re-written to use enumerateOver()


var results2 = enumerateOver(array, function(i, c) {
     return i%2 == 0;
});

So by implmenting this function I was able to come up with a more readable, testable and streamlined codebase. Is this suitable for everyone? Definitely not, but I did it for a few reasons:

  1. Like I mentioned before. I’ve never been at ease with for loops and being able to replace them all with calls to a single function was a huge win for me
  2. The normal use-case didn’t fit right. I needed to not only iterate over arrays but also return the results of work performed on those items. Doing this the “regular” way just wouldn’t work (see previous reason)
  3. I thought this was a fun problem to solve

If you have any feedback, good, bad, or indifferent add a comment!

· · ·

<< Latest posts

Older posts >>

Theme Design by devolux.nh2.me