The blog of Tobin

Tobins nerd blog on .NET, Software, Tech and Nice Shiny Gadgets.

Friday, August 11, 2006

200Mbs Network Through Your Mains Sockets

I'm one of the unfortunate people who is useless at D.I.Y. I'm also unfortunate because my ADSL line was installed in the most useless place in the house and I'm incapable of sorting it out. (think hallway, near none of the rooms where I use a computer).

Rather than rip up floors, drill holes, or trail CAT5 cables throughout the house, I decided to invest in WiFi gear. It's pretty good, I just keep my router/wifi bridge and NAS gear in a small cabinet near the ADSL point. Then, all my PCs etc are wi-fi enabled, so no wires needed.

However, sometimes the WiFi is a bit slow, and I was thinking that I'd like my office computers to at least be able to talk to each other very quickly, rather than having to bounce singals through the WiFi access point.

My first thought was to stick another hub in the office, and have all the computers wired through the hub in that room. That way they can all talk to each other quickly. However, they'd be iscolated from the WiFi network, so to get around this I could also plug a WiFi bridge into the hub to relay the signal from the main WiFi access point. Would this work? Can't see why not, but then 4 computers in my office would be sharing one 54Mbs WiFi link.

I was reading around, and stumbled upon an option I'd not considered. Basically, technology exists that allows you to use your mains electricity wiring for network traffic. This stuff isn't new, but it looks like the manufacturers have been busy and started to really make the technology usable. Apparently you can get a 200Mbs unit these days, which is fairly fantastico! Actually, these things only realistically transfer around 60Mbs, but this is still a tad faster than your average WiFi network according to some folk and a review!

If you ever trust Amazon.co.uk reviews, these products got 5 stars from all users!

In case you want to look further into these products, try googling for BPL or Broadband over Power Line products.

Netgear claim to have one that gives a whopping 200Mbs! I think I might have to invest, sounds great...

Sunday, August 06, 2006

Write Cleaner and Clearer ASP.NET Session Code

I recently worked on an ASP.NET project where the original developer had made quite a lot of use of the session. There was lots of code like this:

if not Session("userid") is nothing then ...

if not Session("userid") = "" then ...

if Session("customertype") = 2 then
discountedPrice = price - (price * Session("discount")/100)
end if

The project was reasonably small and I wasn't hired to clean code. However, there's a number of problems with this code that make life difficult, so I wanted to improve things a little.

Don't Repeat Yourself
Firstly, we've got some business logic repeated all over the site. The developer is using the

if Session("userid") = "" then ...

to check to see if the user is logged in. He's also used different variations of this rule on different pages, such as

if Session("userid") is Nothing then ...

This is messy, inconsistant, and in violation of the great Don't Repeat Yourself (DRY) principle. A better way is this:

if not SessionHelper.IsUserLoggedIn then ....

To make this work you need to make a class called SessionHelper that contains static methods for all the logic you need.

'''
''' Creates a handy wrapper for accessing session variables in a clear way.
'''

Public Class SessionHelper
public shared readonly property get IsLoggedIn as Boolean
get
if Session("userid") is nothing
return false
else if Session("userid") = String.Empty
return false
end if
end get
end property
End Class

I think that it's much clearer, and if we want to change our "IsLoggedIn" rule, we only have to do it in one place.

You may be wandering why the developer didn't use .NETs Forms Authentication for this application, rather than the session. I'm not quite sure why to be honest!

Purpose of Code is Not Clear

Consider this line:

if Session("customertype") = 2 then ...

We've got a couple of problems here. The first is a magic number - 2. Any new developer to the project will have to dig around to find out what "2" means, which wastes his or her time. A cleaner way is this:

Dim TRADE_CUSTOMER as long = 2
if Session("customertype") = TRADE_CUSTOMER then ...

You could even define it as a const somewhere so that it's available to all pages and code-behinds.

Note that in the Refactoring book (Martin Fowler), this refactoring is called "Introduce Explaining Variable". In case you've not come across refactorings, they're basically little rules you can use to clean up smelly code. See www.refactoring.com

An further improvement to that might be this

if Session("customertype") = CustomerTypes.TradeCustomer then ...

Here an Enum is used to represent customer types:

Public Enum CustomerTypes
UnknownCustomer = 0
RetailCustomer = 1
TradeCustomer = 2
End Enum

Using the SessionHelper, we could do one final improvement which is this.

if SessionHelper.CustomerType = CustomerTypes.TradeCustomer then ...

which is clearer still.

You may have guessed that I subscribe to the belief that code is something that should usually be expressive and clear. I've not come across many projects where the code doesn't require modifying and changing over time, therefore clarity can drastically affect how easy it is to change.

Think of it this way, every time you write some code you can chose how much time and money it should cost to maintain. If the block of code is going to be thrown away soon, then it may make sense not to invest too much time making it maintainable. However, if you anticipate returning to a block of code a lot then it would save you much time to make it highly maintainable. Also, once you practice writing cleaner code it tends to come more naturally (that said, I still make the odd dogs dinner despite best intentions!)

Some other thoughts
One thing I considered when working on this project was weather to stop storing so much stuff in the session. The session is great because it's easy to access, but there are a couple of downfalls.

The longer some data is in the session, the more stale it gets. If some back office staff update the customer record whilst the customer is browsing the site, the customer won't see these changes until he next logs in. Not the end of the world, but I'd be inclined to work around these problems by default.

One such work around would be to only load the customer data from the database when it's needed. It means more database hits, but that's better than some of the hazards that can arise by having stale data sat around. The principle of caching also states that you should cached data to get cheap access to data that requires frequent access. If we needed to show a username on every page, then caching it in the session may make more sense than pulling it out of the database on every page.

Saturday, August 05, 2006

A Year with the iMate Jam (or MDA Compact)

My MDA Compact (iMate Jam) is beaten, bashed, scratched and dog-eared. The reflective surface of the self-portrait mirror and the flawless shiny paint have faded away through 18 months of constant use. This has been a brilliant PDA phone.

I'm now on the market for a new one, so I thought I'd share my opinion of this thing before it get's retired.

The Good
Small Size - It's just about small enough to go anywhere. I routinely take it to the pub in my jeans pocket, and have never felt like it's a real burdon. However, as I mention in "The Bad", I'd love em to shave .5 cm off it's dimenions.

Runs Great Applications - I've installed all sorts on it over the last year, from Guitar Tuning software to Tom Tom GPS software. The fact that I can use it with a GPS receive and Tom Tom is simply fantastic. This is one of my primary uses for the phone. I've even got the Lanzarote maps so I could get to my dads house from the airport.

Touch Screen - Love it, wish that all phones had one.

Synchronises with Exchange - I host my email with 1and1.co.uk, using their £5 a month Exchange hosting that means all my contacts and calendar are backed up constantly. The fact that my Jam connects to this is great. The other day I dropped my Jam and the battery disconnected. By the time I'd put it back in, the memory was totally reset meaning all my contacts and stuff had been lost. No problemo. I just turned the phone on, entered the Exchange server details, and it was all synced back up in no time.


The Bad

Size - It needs to be a tiny tiny bit smaller. 0.5 cm would do of width, height and length. I find that my jeans pockets are slightly full when going out with a packet of ciggies, some keys, card wallet and the Jam. That said, the sensible thing to do would be to give up the smokes to make more room for technology.

Small Screen - I think that the screen size needs to be bigger to make it better or reading documents and stuff. One day they'll be able to lose the "border" around the screen and give it an extra 1cm in width and height, that would be great. An improved screen res would also solve this problem.

Camera - Ask anyone, the Jam camera is a bit sucky, and there's no flash.

Failed Trials
When I first bought the phone I also bought some accessories. None of them got used for more than a month.

The first was one of those skins to protect the phone. Despite being very snug, it just attracted pocket fuzz and kept falling off. I decided the phone was gonna get beaten up in the long run, therefore gave in to it and threw the skin away.

The 2nd was the Sandisk WiFi card. It was too much of a faff carrying it around, mainly because it protrueded out of the top of the phone. And besides, the phone didn't make a great web browser in my opinion.

Conclusion
This phone is 5 things to me, in this order. It does all of them very well.

#1 - A phone
#2 - A contact & calendar database
#3 - A list of reference material (I keep all my reference data in Outlook/Exchange Notes)
#4 - A GPS navigator

I occassionaly use the other features, such as web browser and document reader, but I still find the thing to small to be or real use in these areas.

I think that I'll replace it with 3 devices. The first will be a nice small smart phone with Exchange capabilities, possibly the Orange SPV C600 (another HTC model), and I'll get one that comes with a free NavMan or something to get the GPS functionality. The 2nd will be an UltraMobile Tablet PC such as Samsung Q1 or the Sony UX180P. Ideally there'd be a cheap one of these that I could use simply as a portable web browser/document reader for under £200, we'll see what I find...

Note that I had considered the Jamin and the Orange SPV M3600i, however, having looked at how I've used the Jam over the last year, the features that I like the most are available on a smartphone (except the touch screen :-( )