Back in Leicester

I’ve moved back to Leicester, for my final year of my degree!

This may well be the last dedicated chunk of my life I’ll be able to spend learning. Sure I’ll be learning as I work, but I’ll only be able to spend as much time as I can whilst still having a full time job. This year is my last year where learning is my full time job.

And I fully intend to take advantage of that.

I’m living with two friends who I lived with in second year, and I’m very happy with that. The first time I’ve rented an actual house! Paying bills and all that. It’s all very exciting.

Quirks With Javascript Scope and Hoisting

Whilst drowning myself in JavaScript today, I learnt a couple of things.

First was that there’s such a technique as “hoisting“. I’ve taken for granted that defining a function anywhere in my code (whilst in scope) will be available for use. Even if I’m calling a function before it’s actually been declared (converse to what you’d consider to be procedural).

workOutSum (2, 3); // returns 5

function workOutSum (n, m) {
	return (n + m);
}

That’s thanks to hoisting. Declarations of functions get bumped up to the top of the scope. The same goes to variables declared with the var construct.

Continue reading

Money Format in Javascript

I have (amongst others) two functions in most of my websites’ common.js file, which is included on each page. The .round(precision) method just rounds the number to a certain precision, but it won’t be padded up to that precision, so it’s not helpful when you’re trying to output currency.

The second function though is .moneyFormat, which will handle that situation.

i = 11.2333;

i.moneyFormat (); // returns 11.23

i = 120;
i.moneyFormat (); // 120.00

i = 56.4;
i.moneyFormat (); // 56.40

It converts the number to a string, since a float won’t store the trailing zeros.

Continue reading

Storing Addresses (in the United Kingdom) in a Database

Storing postal addresses along with a shop order, or a customer, is a pretty common place thing and often a really important piece of data. So it’s a bit weird that people seem to put so little thought into it, and the same amount of time devoted to it.

The standard way of doing it for me has been storing the lines of the address along side the item it’s associated with. There’d be two tables like this:

customer (customer_id, name, address1, address2, town, county, postcode, ..)
order (order_id, customer_id, address1, address2, town, county, postcode, ..)

Although you could get the order’s address via the customer_id, I wouldn’t consider this to be redundant data. A customer might change their address later on, but we would want the old address to stay with the order.

It is redundant in the way that the data is still duplicated, when it could be stored somewhere else and then referred to with a foreign key. So the above pseudo-table type could be changed like this:

customer (customer_id, name, address_id, ..)
order (order_id, customer_id, address_id, ..)
address (address_id, address1, address2, town, county, postcode)

Now each property in the country will have its own row, its own ID.

This means you need to change the flow of your application though. Whereas before you accessors and setters could just change the address attributes easily, you now you need to think about if you’re editing, or adding a new address. Though you can handle this by making an Address object (which you could have done before, but would have felt more hacked together).

Having an address table like this means you can hold more data about the property too; the longitude and latitude data, or image or the property. You’d have no place to put this data before, and in the case of long/lat you’d have to have looked it up each time from an external source.

The table still isn’t optimised though – you’ll notice that you’re recording “Birmingham” thousands of times, using around 10 bytes each time. Not awful, but if you’re looking to safe space then turning 20 megabytes of “Birmingham” into 10bytes might look attractive. To overcome this you could have the following:

address (address_id, line1, line2, lin3, a_town_id, a_county_id)
address_town (a_town_id, name)
address_count (a_county_id, name);

If you’re that into saving storage data though, you might consider putting the tables into their own database, and using that database across all of your websites and services. No use in collecting it hundreds of times.

Another problem which crops up is that users never enter helpful data. Instead of gathering the data from users though, use a service like PostCoder. Only ask the user for their postcode and let them select an address, rather than entering it. That way you get a somewhat standardised address format which every user will be using.

The problem with PostCoder is that they don’t have incredibly helpful data sometimes. A search on my home road returns “Janes, Hair Salon” over the first two address lines. Sometimes the business name is apart of the returned address, and other times not. I’m really not sure where to get reliable data.

Usernames and hashtag links in Twitter API

You’ll probably have noticed if you’re using the Twitter API that when you get the text of the tweet you don’t get any of the @usernames or #hashtags, or even URLs converted to actual links. It doesn’t look like a lot of the libraries (for PHP at least) bother with doing that either.

Here’s the code to get it to do that though:

<?php
$twitter_user = "shamess";

$json = file_get_contents ("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$twitter_user."&count=3&trim_user=true");
$tweets = json_decode ($json);

foreach ($tweets as $tweet) {
 // change all the urls to links
 $tweet_text = preg_replace ("/http://(S+)/i", "<a href="http://$1">http://$1</a>", $tweet->text);
 // @usernames
 $tweet_text = preg_replace ("/@(w+)/", "<a href="http://twitter.com/$1">@$1</a>", $tweet_text);
 // #hashtags too
 $tweet_text = preg_replace ("/#(w+)/", "<a href="http://twitter.com/search?q=$1">#$1</a>", $tweet_text);

 echo "<li>".$tweet_text." - <em><a href="http://twitter.com/shamess/status/".$tweet->id_str."">".date ('l at H:i', strtotime ($tweet->created_at))."</a></em></li>n";
}

Some Notes About PHP and MVC

I want to give a better explanation on why the short form echo syntax in PHP annoys me so much.

A lot of people argue that PHP is a template language perfectly designed for injecting variables into themes. WordPress themes definitely are the best implementation of the model-view-controller architecture in PHP. You can see that just by looking at any template you have lying around, or just the example one on the codex. Functions return strings which can be injected into other pieces of strings pretty seamlessly. Even inside “The Loop” it’s still really easy to read what’s going on.

There’s no logic in any of those pages. Variables aren’t being defined and set. There’s no if(){}else{} in there because that’s all dealt with on the controller level, the way it should be. When you mix the view and controller you end up with ugly and unmaintainable code. It’s no longer MVC.

That’s why code like that I mentioned in my ranty blog post -

<td style="padding:0 5px 5px 0;">
 <input name="Gas" type="radio" id="Gas" value="0"  <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="0")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','none');" checked/>No
 <input name="Gas" type="radio" id="Gas" value="1" <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="1")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','block');"/>Yes
</td>

- makes me so angry. This is from a project BEA Solutions has inherited from a developer who doesn’t seem to have thought this through very well. In the code above you can’t set a default “what’s checked if $PropertyDetail->Gas isn’t set?” case without making the ternary operator even more ugly than it currently is.

This project has two folders. One called /front_script/, and one called /front_form/. The controller (and accessing the model) is handled in _script/ whilst the view is handled in form/. (Both are included by index.php, which mod_rewrites every URL.) That’s a pretty decent separation.

But when you’re doing logic – complex ternary logic at that – on the view then you’ve destroyed most the benefits of developing with MVC. Which radio button should be checked needs to be decided in _script/ – the controller – not on the view.

If You Do This, I Hate You

I know you’re trying to do MVC architecture, but you’re doing it wrong and it’s entirely unmaintainable. Longer rant later, possibly.

<td style="padding:0 5px 5px 0;">
 <input name="Gas" type="radio" id="Gas" value="0"  <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="0")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','none');" checked/>No
 <input name="Gas" type="radio" id="Gas" value="1" <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="1")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','block');"/>Yes
</td>

How to set up a remote Git server

I’ve been using github for a while now, which is fine for my public projects. There are obviously some that I want kept private though. I could pay for a github account, or I could make use of the VPS I’m already paying for and install git on there.

I was going to blog about this process from the start, but then I found a really decent article by Bradley Wright describing exactly what I wanted and figured I’d be wasting my time. I’ve run into a few problems though that the article didn’t seem to come across.

As I said, first I used Wright’s article, How to set up your own private Git server on Linux. I did pretty much everything on there, and everything worked brilliantly.

I came back to my local machine and added the remote host

git remote add vps git@shamess.info:~git/todoapp.git

Here’s where I ran into another problem when I tried to push to it.

fatal: The remote end hung up unexpectedly

That took an embarrassingly long time time to realise that I had just forgotten to fire up Pageant – this error just means that you couldn’t be authorised, so the server booted you off. This made me happy because it meant that my repos are private by default, I guess.

Even using the git:// url (which apparently only has read access), I still get an error. I’m not entirely sure this is expected, but if not it’s broken in my favour since I’ll only be accessing via ssh.

Back to my first push attempt though. After starting up Pageant I still ran into an error trying to push.

fatal: 'git/todoapp.git': unable to chdir or not a git archive
fatal: The remote end hung up unexpectedly

This took a little longer to track down, but I eventually found that it was an issue with the path I was giving it. This actually has nothing to do with git, and was just that I was giving SSH a bad URL to head to.

SSH doesn’t follow relative URLs when it’s connecting, and so you need to list the full path. I went with git remote add vps git@shamess.info:/home/git/todoapp.git, however I think just git remote add vps git@shamess.info:todoapp.git would have sufficed (since SSH would login at the git user’s home directory anyway).

That has me pretty much set up now! I hope someone finds this post once they fall into the same problems as I did, and find my resources helpful.

Playing with Java sockets and streams

I’m playing around with sockets and streams in Java in an attempt to get a conversation going between the client (my laptop) and the server (shamess.info). I decided to come here and talk about what I’m doing, because I’m a little confused and maybe having it written down will clear that up.

I have most the bones set up from the server side sockets tutorial on the Oracle site, and that works fine. The problem is though that that’s just entering data using System.in and sending it, which is fairly useless.

Since it’s in a while(true) loop I can’t do anything out of that loop – I can’t make GUI or handle anything at all that can’t be done inside that loop (which is run continuously).

So I figured I’d look into threading, which turns out to be pretty integral to Java programming (or just programming in general, I expect). That’s a new thing for me coming from a procedural language.

I figured I need three loops:

  • reading from the server
  • sending to the server
  • creating UI

One for creating UI is apparently just good practice (Event Dispatch Thread).

That’s worrying though because it means that “conversations” would be asynchronous. But that could work fine by just adding a reference ID to each message sent.

client: [10001] what's the user's current map coordinates?
client: [10002] what's the user's inventory?
client: [10003] what's the user's health?
server: [10002] two health packs, and a mana potion
server: [10001] 10,2
client: [10004] use a mana potion
server: [10003] 22
server: [10004] done

Giving each query or command a ticket, and returning with it, would make sure both sides always know what we’re talking about regardless of the order things get sent back.

Lets get that working for now then. Bbiab.