Pimple Free

I recently started a new web app project at work. Silex was chosen as the PHP framework we'd be using. Silex makes heavy use of Pimple, a very small dependency injection container that allows you to map factories or values to string keys on an object that implements PHP ... Read full article

When in Rome

There is something about programming conventions that gets everyone riled up. Everyone has an opinion and no one agrees. Today, I got a little riled up. I'd like to lay on your couch and tell you about it while you sit attentively taking notes with a quizzical expression on ... Read full article

Be the worst at something

Emotionally its easy to be a young moderately talented programmer. Your boss's expectations are probably low to average, so when you knock it out of the park it hits the goddamn moon. You're probably a cowboy, and business people adore cowboys. You hack code at lightning speed because ... Read full article

Backbone for Invertebrates

Its 2013 and I'm about to talk about Backbone.js, a four year old library that isn't on the bleeding edge of anything. This isn't a post about Backbone.js so fans of AngularJS, Knockout, or new M-V-yadda-yadda-yadda framework can settle down. I want to focus on ... Read full article

Template Method Trap

Inheritance is often a trap. Its starts off like an adorable lion cub but grows up to be a man-eater. One common use of inheritance is within the Template Method Pattern. It is the Jekyll and Hyde of patterns; half pattern, half anti-pattern. What is Template Method? Let me step ... Read full article

On the outside looking in

Programmers tend to look at software design from the inside out, but that approach has many pitfalls. Looking at software development from the outside in provides us with some real advantages including ease of integration and ease of concurrent development. Read full article

Homegrown programmers

I never had the traditional computer science training in college that most of my programmer colleagues had. How does a homegrown programmer like myself differ from a programmer with that experience? Read full article

If Programming is an Art

If you accept the premise "programming is an art" where does that leave you? I believe we are better served by defining our goals by objective measures instead of subjective measures such as beauty. Read full article

PCI DSS compliance and spaghetti code, Part 3

(Be sure to read Part 1 and Part 2 first!) When writing new code I find its a good idea to start at your integration point and then write that client code, whether just by psuedo-coding or writing some test cases. For my payment gateway code I need code that ... Read full article

PCI DSS compliance and spaghetti code, Part 2

(Be sure to read Part 1 first!) The first iteration of my Command objects could continue to use our existing payment gateway code, but this would not be acceptable by the end of this project. This old payment gateway code relied heavily on global state to get configuration options and ... Read full article

PCI DSS compliance and spaghetti code, Part 1

Anyone taking a substantial amount of credit card payments will eventually stumble onto the Payment Card Industry Data Security Standard (PCI DSS). Meeting this standard likely adds significant complexity to your application, comes at fairly great expense, and will leave your developers and sysadmins with at least a few head-scratching ... Read full article

Exceptions as part of "regular" control flow

I've heard this rule a lot: "Never use exception for control flow." Its an interesting statement to parse. We know what an exception is, but the definition of control flow is a little fuzzy, so lets clarify things a bit. In computer science, control flow (or alternatively, flow of ... Read full article

Categorizing exceptions: Subtypes vs Error codes

Today's problematic pattern: Catching a single, usually per-package, exception and then using an associated error code in a conditional statement to determine what else to do. I've seen several colleague programmers do it and they never seemed to think about doing it any other way. Its an oddly ... Read full article

Exceptions vs Null

Plenty of developers agree that returning mixed-type results is not a good practice. It leads to conditional statements wherever the method returning the result is used. Everbody agrees thats bad, and then Null walks in, and we're not sure anymore. Null is suppose to be magical value which can ... Read full article

Testing with string objects in PHP

As shown in the last post there are benefits to instantiating the class under test in setUp(). With objects its simple to create them and maintain reference to them so you can change them, but what about things like strings? Unlike in many other modern languages, strings are not objects ... Read full article

Durable, readable tests

Anyone who has written tests is familiar with our friend the setUp() fixture method. We use setUp() to do all that work that's common across all our tests. It can be used to reduce repetition within our tests, making them simpler, and more readable. Unfortunately testing can get a ... Read full article

Faking PHP functions

Usually when I want to fake some kind of functionality provided by PHP, I create a new library that maps all that crazy API stuff baked into PHP to something that makes more sense. Maybe it even uses exceptions rather than -1/0/false/null. Holy shit. I almost never ... Read full article

Code smell: Too many private methods

I mentioned this in my last post so I figured I'd add some examples to clarify. Consider the following code: class Cache { public function set($key, $data) { if($this->_useMemcache()) { $this->_setInMemcache($key, $data); } elseif($this->_useFilesystem()) { $this->_setInFilesystem($key, $data); } else { $this->_setInMemory($key, $data); } } } Its part of ... Read full article

Private visibility in open-source

The statement in question: "Private has absolutely no useful role in open-source code." There's already lots of drawn out discussion about this so I'll keep it short. Lots of private methods are bad, but not because of OSS. Its a code smell that usually means too many responsibilities ... Read full article

Configurable return types kill puppies

Here's some code you could see using PHP's Adodb library. The library isn't the point but its demonstrates a pattern I see a fair amount. $oldFetchMode = $conn->setFetchMode(ADODB_FETCH_ASSOC); $recordSet = $conn->execute("SELECT * FROM table"); $objectThatDoesWork->work($recordSet); $conn->setFetchMode($oldFetchMode); To me this is a multi-fail ... Read full article