davidrickard.net

Random stuff, randomly updated.

Bad code. Just bad, bad, bad

I’m no code guru, but I do like messing around with languages. I’ve managed to write a whole ‘application’ in the form of a little php app to manage a DHCP server. The ISC DHCPD is one of the main DHCP servers out there, but being a unixy app, it has zero in the way of a management interface. It’s plain-text config files or nothing. I’ve got nothing against hacking text files, as I do it all the time, but at work we have a need to get stuff done quickly, and having to show all and sundry how to hack the files isn’t easy. Plus you’d need a copy of PuTTY to access the server, and if you manage to bungle the config file, DHCPD gets very upset and refuses to start. Then you have to go into the system logs and find out why. Explaining this to people who aren’t experienced with Linux is fair enough, but expecting people to remember this stuff (especially if they do it once in a blue moon) isn’t. So I thought I’d have a whack at writing a web interface to enable some simple management of the config files.

My method was simple: a simple web interface, store the information in a database, then use a command to flush it out to a config file. Simple!

I wrote the thing, and it all worked OK. You could add, delete and change MAC addresses. It would happily do this stuff. I’ve recently come back to trying to use it, and the damn thing just doesn’t work! My mess of bad code and even worse regular expressions has resulted in a mishmash of useless code that just doesn’t really work. I tried to change a MAC address for a PC it had in the datase, and it told me it already existed! I know that, hence clicking ‘change’ instead of ‘add’.

I’ve resolved to do something about it though and completely re-write it. I’ve got two options; one is to use a pre-existing framework, such as Cake PHP, OR write it all from scratch as an object-orientated application. I’m leaning towards the latter, as it would be a good learning experience. I do plan on, well, planning things this time! It grew a bit like topsy last time. The core feature of being able to add a node started, and from there bits just got woven in the sides. I was constantly going back to existing functions to add in bits, and breaking stuff in the process. Using OO tends to force you into planning and designing things better anyway, so I’m sure it’ll be possible. I’ve toyed with the notion of trying something like Python or maybe even Perl. I’m used to PHP though, and Perl is just plain wierd as far as I’m concerned (PHP is syntaxically very similar, but more straight-forward).

It’s something I want to work on and get up as a proper GPL’d project on Sourceforge. That’s my ultimate goal anyway!

Comments are closed.