Joel Hainley : San Francisco Bay Area Software Consultant

  • rss
  • Home
  • About
  • Reading Lists
    • 2008 Reading List
    • 2007 Reading List
    • 2006 Reading List
    • 2005 Reading List
    • 2004 Reading List
    • 2003 Reading List
    • 2002 Reading List
    • 2001 Reading List
    • 2000 Reading List
    • 1999 Reading List
    • 1998 Reading List
  • Software
    • mcalc - a tournament poker utility

PHPUnit and global variables

April 30, 2008

The first time I did some work with PHPUnit I was writing out tests in preparation for some refactoring. So I put together a bunch of tests for my utility classes, and pure logic classes etc, database access classes, etc. So everything was trucking along wonderfully, things are falling into place and then I run a test that tests some database stuff.

The application was setup with a configuration file that defined all of the database connection information, this file is included by the database library providing a single set of database routines to be shared by the application. Pretty standard stuff here. Perhaps in a nod to the rails/java crowds it could have been put in a non-code file and read in by the application but as far as I’m concerned that’s just personal preference. At some point you’ve got to have some configuration information somewhere and this works as well as anything.

So now that you know how things were setup, we’re back to the PHPUnit test…it bombed, failed, crashed, whatever. After digging into it a bit I discovered that PHPUnit has a problem with munging the global namespace when it runs. Things that are global variables can’t be pulled down into local scope because they don’t exist there anymore. I did some reading and found numerous complaints about this in the PHPUnit bug system, all of them closed with “this issue resolved” when the issue most assuredly had not been solved. I even found a conversation where one of the developers stated that it was not a bug, that the user simply needed to modify their code to work with PHPUnit. Huh? Modify the architecture of your code to work with a testing framework?!?

I spent some time thinking about this and I think it could be said that he’s right, but for the wrong reasons. First off, if this is a technical limitation of PHP then just say so and let it go. If it’s an implementation problem with PHPUnit then say so and try to put together plans to fix it. However, I think in this particular instance the problems that I was experiencing with PHPUnit could indicate a larger problem in my application.

Here’s how..One of the things that I hated when I first started using PHP was having to pull things out of the global namespace into the local namespace to use them. When PHP5 came out and the object model finally had a good amount of maturity I moved to utilizing classes for most of my code and this allowed me to start using the $this operator for accessing a lot of variables that I needed to deal with. In fact, the only place I was really using globals was in my library routines. After thinking about this for a bit I realized that it was much more convenient to wrap all of the configuration data into Singleton then I’d have a single object that I could use to get all data instead of having to pull in everything I needed one variable at a time.

It was at that moment that I realized if I had a singleton that housed all of the configuration data I could instantiate it in all of the routines where I needed to access configuration data and it would avoid the problems that PHPUnit had with the global namespace. When I looked in the todo’s for the configuration file there was an item at the top “Maybe I should turn this into a singleton”…two birds with one stone? Ten minutes of work and I had everything setup and ran my tests and “bingo” everything worked.

So in the end, modifying the way that configuration information is disseminated throughout the system by using a singleton has been “the right thing”. I had some heartache over the responses from the PHPUnit developers about the “solution” to the problem their users were having mostly because the solution involved a lot of extra crap with addressing the global namespace in a particular way. Using a singleton ended up providing the needed functionality, didn’t do anything retarded with variable access, and ended up making the application much cleaner to work with.

Comments
No Comments »
Categories
php, programming
Tags
global variables, php, php5, PHPUnit, testing
Comments rss Comments rss
Trackback Trackback

Flex Development

November 6, 2007

I’ve been reading through a bunch of books on Flex development. It’s pretty interesting stuff. It is probably most easily understood as a programmatic interface into Flash with a bunch of widgets such that you can develop very sophisticated applications that talk asynchronously to a backend server. The upside of this being that wherever you can get a Flash player installed you can have your application and it works the same everywhere, something that can’t be said for AJAX.

I wrote up a quick app yesterday afternoon to look at a customized RSS feed for this blog and allow a user to navigate through the results. It’s just something I was fiddling with, it’s not complete and probably never will be. I’m going to investigate a different approach to some things I was doing to see if it makes things cleaner. Check it out here

Comments
No Comments »
Categories
flex, programming
Comments rss Comments rss
Trackback Trackback

picprep bug fix and other stuff.

January 15, 2007

I was doing some work for a client this weekend and needed to resize a photo. I dusted off the picprep source, built the app, and did what I needed to do, but i noticed an odd problem in the way it worked. So i earmarked that for further investigation when my work was done. Sure enough, when I got some time to look at it in detail there was a bug in picprep, and it was a kinda major bug.

I know how it made it this long without being discovered, the bug only shows up when you are working against single files, and not doing appending of characters to the output filenames, but i should have caught this a lot sooner. I fixed the bug, and then started looking through the rest of the source and found some interesting things in my notes about this project. I had mentioned that I would port this to linux at some point. However when i looked at the source, it had been ported to linux ( i think i did this one morning when shad needed it, and forgot to package it up for general release ) So the application now runs natively on windows/osx/linux, pretty cool on that deal.

I thought of a couple of features that I could use in this product so perhaps when time allows i’ll add them they include :

  • rotate process : the ability to rotate images would be nice
  • collage process : the ability to point picprep at a bunch of image files and have it turn them into a single large file would be nice. this would include the ability to generate an image map coordinates file back to the original pictures such that you could drop the resulting image and coords onto a webpage and have a interface into the pictures
  • command files : it would be convenient to be able to save a set of operations into a text file such that if you were always wanting to process files in a particular manner you could just drop them into some directory and then say “run these commands on this directory”. The thing about picprep is that its processes are discrete operations such that if you want to rotate, add border and then create a collage that is three seperate commands that must be issued to picprep in series, having the ability to create command files for multi-step operations would obviate the need of ways to chain the operations together internally.
  • border process : i haven’t looked into this but i’ve seen a bunch of images on the web where people add a little white border around their photos, I guess to give it the polaroid instamatic touch. It might be kinda nice to be able to say “add border to these photos”

I’ll probably spend some time on this when I need a little bit of sanity in my life. The myndkryme website has been down for a while not sure why, haven’t talked to the ISP about it yet. So if you want new builds of picprep ask me directly here, and i’ll get them posted somewhere where you can download them.

Comments
No Comments »
Categories
coding, picprep, programming
Comments rss Comments rss
Trackback Trackback

Knuth!!!

January 12, 2007

Went to Stanford in December to watch a Knuth lecture. It was interesting, he kept the heavy math bits out of the lecture so i could understand it. ( well not so I myself could understand it, but as a consequence of him keeping the math bits out, i was able to understand it. )

I’ve been slowly reading through his MMIX paper, it has been interesting, although i have had to take detours to understand some things that he takes for granted.

Shad sent me this today :

KNUTH!!!

Comments
No Comments »
Categories
knuth, lectures, programming
Comments rss Comments rss
Trackback Trackback

Proof of my lack of brilliance

October 27, 2006

I’ve been reading through Structure And Interpretation Of Computer Programs in the evenings, and have started going through the exercises as well. I figure it’ll help bring home the finer points of the text, and it’s sorta like little puzzles to work on, plus i’m learning scheme, and since I have a well documented affection for lisp it works out well.

One of the first exercises that they recommended was given three numbers square the sums of the two greater numbers. Pretty straight-forward right? Yea..pretty much. I implemented the usual if (and (> a b) (> b c)) then A is the biggest number. Pretty easy to find the largest and smallest numbers with that approach. However, start coding up for the middle number and you end up with this mess of unreadable garbage, well not unreadable but you end up wishing there was something elegant you could like the above statements.

After a bit of thought I hit upon it it goes something like this (- (+ a b c) (+ (get-max-num(a b c)) (get-min-num(a b c))) which works out rather well because the both the max and min of three numbers are very easy found using just a couple of lines of code. This is obvious when you think about it, but when you’re coming from a bunch of >, <, and, cond statements it’s not the thing you would consider at first..i guess…well i didn’t.

I mentioned this to shad and his response was something along the lines of “duh, obviously b = (a + b + c) - (a + c) it’s algebra”. This response further reinforces the theory that i’m in over my head most of the time.

Comments
2 Comments »
Categories
programming, scheme
Comments rss Comments rss
Trackback Trackback

« Previous Entries

Navigation

  • Amateur Radio
  • bicycle
  • books/reading
  • business
  • cackl
  • coding
  • flex
  • friends
  • hamtesting.com
  • knuth
  • lectures
  • life
  • observations
  • people watching
  • personal finance
  • philosophy
  • php
  • picprep
  • programming
  • reading
  • santa barbara
  • scheme
  • school
  • seo
  • uISV
  • website

Search

Archives

  • August 2008
  • July 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006

Site Pages

  • About
  • Reading Lists
    • 1998 Reading List
    • 1999 Reading List
    • 2000 Reading List
    • 2001 Reading List
    • 2002 Reading List
    • 2003 Reading List
    • 2004 Reading List
    • 2005 Reading List
    • 2006 Reading List
    • 2007 Reading List
    • 2008 Reading List
  • Software
    • mcalc - a tournament poker utility
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox