How do I write code that doesn’t suck?

A Stack Overflow question popped onto my Interesting tags list today: How do I write code that doesn’t suck?

Great question, and it’s one I struggle with daily.

When you find the answer, let me know.

Advertisements

Making Pages a First Class Citizen

I’ve been designing a blog engine, and one of the tough parts I’ve been dealing with is how to make pages a first class citizen.  In WordPress, you can either have 1 level of pages, or you can have dynamic content.  You can’t do both on the same page.

This is a problem. What if a customer wants a fully functional website built so that the static pages don’t really look or feel like static pages? What if they want the navigation for the pages to be in line with the rest of the site?

While sleeping last night, I had a dream about this, and the answer (weirdly enough) came to me in the dream.

Why not use:

{category}/{page}/{id} OR

{id}/{category}/{page}

That way pages like ‘About’ and ‘Contact Us’ can be placed in a CSS menu like system.

The routing for such a distinction is simple:

routes.Add(“PagesRoute”,

“{id}/{category}/{page}”,

new { controller = page, action = show, category = “”, page = “”, id = 1 },

new { category = @”[A-Z][a-z]”, page = @”[A-Z][a-z]”, id = @”[d+]” }

);

 

The page controller then contains CRUD Methods (perhaps Delete, Edit, Create, and Show), and the Show Action takes in two parameters “category”, “page”, and “id”.

Flipping a Coin

coin toss

I read The Adventures of Accordion Guy in the 21st Century regularly.  It’s not a book or anything, it’s just the blog of a really cool guy.

Anyway, in one of his recent blog entries, he talked about Flipping a Coin as a means to make a decision. I had never thought of the coin flip as a viable way of making a decision — and indeed this method is in line with that tradition by making the decision before the coin lands.

 

 

Getting out of the weeds

weeds

I’ve been struggling to ship a few of my ideas.  It turns out that I’m not alone; plenty of developers go through this problem.  The good ones get over it, and quickly. I’ve been analzying my behavior, what I spend my personal coding time on (at work I have no trouble shipping), and I’ve come to realize that I spend a lot of time in the weeds.

For software, being in the weeds means dealing with details that don’t matter to shipping. My newest example is fighting with JQuery AutoComplete functionality in my ASP.NET MVC application. Even though everything is correct, it simply won’t autocomplete.  I’ve spent the last three coding sessions futzing with it, only to not get anything done. 

I spent 6 hours trying to get autocomplete to work when I could have spent five minutes on a dropdownlist and been done with it.

All during that time, I was just thinking about how to get it to work, or what could have gone wrong (maybe my routes weren’t defined correctly? Maybe I don’t have the path correct for JQuery?) instead of just implementing the simplest feature and going about my business.  The entire rest of the application took about 2 hours to spec and write, so to spend 6 hours on a tiny polish feature is asinine.

So here’s what I’m going to do: Post my code here, see if there are any takers to figuring out where I went wrong, and move on.  I’ve got a deadline to meet, and I won’t meet it by staying in the weeds.