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.

Debugging a blank PHP page

I’m writing this post because I’ve had this problem three times in the past two weeks. Each time it’s soaked up more hours of my life than it should.

The problem is that you load you script and nothing is output. Not even an error to tell you where you’ve gone wrong. This must be a PHP bug, but I guess we’ll just have to live with it for now, and try debug around it.

The weird thing is the error that’s causing the page to be blank might not even be a fatal error. Twice for me it was just a warning, which should definitely not halt the processing of the script.

Without a line number or even which include the error is on it’s pretty hard to find out what the problem is to start fixing it.

Ultimately what I end up doing each time is echoing a string, and on the next line exiting. Put this right after the opening <?php on the first line.

echo "got to here.";
exit;

You should see the string output now. If you don’t it indicates that the hidden error is a fatal error, which will probably mean something to do with the syntax of your code, not the logic an undefined method or some such.

To fix that just carefully look through your code. If you don’t see any obvious problems then unindent everything. Now start indenting again if-by-if (or while-by-while, for-by-for, etc). Hopefully before the end of the file you’ll notice that you’ve had to indent twice, or you have left over braces. Delete or add as appropriate.

If you did see the “got to here.” text though, you can start moving those two lines down line by line. When you stop seeing the text you know the line above is the problem line. If you’re unfortunate enough to get to an include and it stops outputting, you’ll have to go through that too.

Once you’ve found the problem line, start debugging the relevant variables. You’ll likely find the problem there. (90% of all programming errors are variable related.)

The PHP-MySQL programming loop…

Seriously. I hate writing applications that consist entirely of

// get result set with mysql_query
//  check if there are any results
     //  loop through each result
          //  output its data
//  no results?
     //  output message saying so

Once you’ve mastered that you can get hundreds of freelance jobs and securely say you have the prerequisite skills. It’s just so dull after writing it a hundred thousand times. Oh, that and

//  has this form field been set?
     //  yes, update database

That’s everything. I want something more interesting. I expect this is why Rails was made because someone got sick of doing this.

Round up to decimal precision (Javascript and PHP)

I needed to round up to two decimal places for currency (you always round up when using money), but for some reason the internet didn’t want to share that information with me, so here it is.

PHP:

//  Takes a decimal and rounds up (never down)
function round_up($val, $precision) {
  return round($val + pow(10,-$precision-1), $precision);
}

Javascript:

function round_up (val, precision) {
    power = Math.pow (10, precision);
    poweredVal = Math.ceil (val * power);
    result = poweredVal / power;

    return result;
}

So, using round_up (1.432, 1); would return 1.5. For currency you’d want to set the precision (the number of decimal places you want) to two.

Zoom To Fit All Markers on Google Maps API v3

I’d like a problem that for once hasn’t already been solved by someone else.

I had a set of markers which needed to be all on screen, and for some reason there’s no .zoomToShow method. Fortunately it’s pretty simple to create yourself.

//  Make an array of the LatLng's of the markers you want to show
var LatLngList = array (new google.maps.LatLng (52.537,-2.061), new google.maps.LatLng (52.564,-2.017));
//  Create a new viewpoint bound
var bounds = new google.maps.LatLngBounds ();
//  Go through each...
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
  //  And increase the bounds to take this point
  bounds.extend (LatLngList[i]);
}
//  Fit these bounds to the map
map.fitBounds (bounds);

And that’s pretty much it. Then, of course, I found that someone had already done this before, but for version two of the API.

There is nothing wrong with using frameworks

After lurking in #javascript for a while I noticed that a lot of the developers there look down on people using jQuery and Prototype. Their view is that you’re just getting one step away from the actual language, and you’re leaving key programming skills behind.

I acknowledge that that’s true, but I don’t see why it matters. All languages are essentially frameworks of a lower level language. PHP is basically a framework for a load of C libraries. C is just a framework for assembly code. Assembly is just a framework for binary programming. Yes, by using PHP you lose a lot of functionality of C, but that’s just because PHP is filling a market with no need for hardware manipulation and the like.

Same with jQuery. If you use $.get() you lose the ability to do the request synchronously (I’m aware that you could just use $.ajax(), but I’m making a point), which isn’t really a big loss to most people when compared to the benefits.

Javascript isn’t exactly the most uniform language around, which IE not supporting half the stuff Opera does, and Safari doing things differently to Firefox it’s a really hard language to code for. You spend more of your time finding work-arounds for each browser than actual logic. The frameworks available all do that for you, making sure that there’re no compatibility issues between browsers, and that $(‘#element’).slideUp() does exactly the same on every browser.

I’d go as far as to say don’t even bother learning about document.getElementById means. It’s so clumsy and awkward when compared to $().

Due to those functionality additions though it’s obviously a little bit slower. I’ve not noticed any speed decrease, but in an “every microsecond counts” environment there would be a noticable difference. But Javascript engines are getting faster and faster so does it really matter? The average person doesn’t even notice a difference.

It’s just programming evolution. It happens to every language. I wouldn’t be surprised if a few forward thinking browsers decided to just store a copy of the latest framework versions locally, so they’re instantly available to every website without having to download the same file hundreds of times from different servers.

Game Boy resolution problem has no real resolution.

I was looking up information about the resolution of Game Boy Colour, and found it to be a few different sizes depending on what version you own. But I don’t remember playing any games on a 640 x 350 screen. The smallest resolution available is 160 x 140 which may have made more sense, but I was still unsure.

Ah, I remember it well.

Ah, I remember it well.

I decided to look around my house to find my Game Boy and my Link’s Awakening cartridge so I could just count the pixels, but apparently my sister is holding them hostage somewhere. I had to settle for a screen shot and found the image to the right.

That’s pretty much how I remember it, to scale and everything. But the resolution of 320 x 288 doesn’t fit any of the resolutions for any figures I can find. And even then each pseudo-pixel is 4 x 4.

That must mean that developers were able to set the resolution they wanted to work with, and the Game Boy just scaled it up or down to fit the screen.

Come to think of it, that makes absolute sense, being as we don’t have four different versions of the game for four different consoles. Although, I’m not exactly sure how at the moment, since by just looking at the numbers I can’t see how they’d all factor to the same scale. Maybe some games just have a border around them. I can’t remember that though.