I am eagerly looking forward to attending SunshinePHP in February. I wasn’t planning to go for the longest time (cost related), but then, among other things, the community attacked.
I’m looking forward to so many things. I have made many friends over the years, in the Symfony community specifically and the PHP community at large. I want to greet old friends, and new ones. Seriously — come shake my hand and say “HI!” Beware — I will do the same to you!
The Talks
There are SO many good talks. Seriously:
“Symfony2 + EmberJS for fun and profit” and “PHPUnit Best Practices” — I have no idea which talk I will attend. I know Dustin is an amazing presenter, but I’m curious to see if Sebastian shows underutilized parts of PHPUnit I have yet to discover.
“Silex, the Microframework” — I am already quite experienced with Silex, and will not attend, but if you’re curious, having talked to Ben on IRC, you will enjoy this.
“From POX to HATEOAS, Our Companies Journey to Build a Hypermedia API” — I’ve talked to Luke in IRC very rarely, and am interested in building stronger API systems, so I’m curious to see information on how his company has evolved their platform. If you have ever heard me talk about improving as a developer, I always say it happens a day at a time. I’ll bet Luke has a great story about his company’s evolution.
“Replication with MySQL” — I’m hoping I will have a need for the knowledge bomb Ligaya will drop in this presentation.
“DOs and DON’Ts of MongoDB” — I’m looking forward to getting some great use-cases out of Jeremy’s talk. I don’t have any experience with MongoDB, and I’m hoping that will change.
“How Kris Writes Symfony Apps“, “Introducing Tests in Legacy PHP Applications“, and “Bringing Good Design to the Table” — I honestly have NO IDEA where I will end up here. Seeing Kris’s process when developing a Symfony2 app has the potential to be quite interesting, as other people always do things differently. However Jeff’s presentation on introducing testing and Brian’s talk about database design also sound excellent. So torn.
“Cryptography for the Average Developer” and “Symfony and Javascript: Combining the Best of Two Worlds” — Anthony and I have tweeted back and forth on occasion, and I’m curious if he has added any more to the talk beyond what I have read from him online. On the other hand, Nacho is going to talk about JavaScript frameworks and Symfony2. Sorry Jordi — I use composer every day. However, if you like Chocolate, tell Jordi that Jacob said he had some for you.
And that’s ignoring Rasmus, Cal, and Keith’s keynotes — those will be excellent.
The People
It adds an entirely different dimension to your community involvement (on twitter, on IRC, in mailing lists, on GitHub) when you have met the people you are interacting with. Heck — at Symfony Live 2012 in San Francisco, I used Fabien Potencier as my Rubber Duck! Having that context, having met them in person, deepens the interactions you have with people online, and helps to make them more than just an internet handle. I’m looking forward to seeing so many of my friends again, while also taking the opportunity to meet some new ones as well.
The Venue
Seriously. Florida. In February. Do I really need to say more?
And Finally
If I find out you came to SunshinePHP and didn’t shake my hand, I will personally convince GitHub to delete all of your code be very grumpy.
So I wanted to take a little time and document what happened in 2012 for myself.
I think the biggest things I want t take away from 2012 are a few key lessons:
Don’t Be Afraid of What Other’s Think
If you spend your life mired in worries about what others will think of what you are doing, you will never get anything done.
Just do.
Too much of our lives (collectively) are spent wondering about what others will think about what we are doing? So what. Who CARES if you are wrong. People are wrong ALL OF THE TIME. What matters is that you are not so attached to your wrong opinion that you don’t change it when presented a valid argument otherwise.
It’s said the most ‘thought to to be smart’ people aren’t smart because they’re right but because they are quick to change their minds.
I, of course, have the luxury of never particularly caring what others have thought of what I say or do. It’s lead to some pretty awful predicaments — but some pretty awesome ones too. Who, among my close friends, would have pictured me as being one of the ones to immediately volunteer for Symfony Jeopardy at Symfony Live? Especially when I am culturally dumb and (at time time) was quite technically deficient. That didn’t stop me from answering the only then-to unanswered question though!
Just do.
Go up in front of everyone, be a goof ball, have fun, laugh, be silly. Anyone who actually cares in a bad way isn’t anyone you want to associate with anyway.
Just do.
Don’t be afraid of what others will think. Seriously. Most commonly, what most other people will be saying is “Gee, I wish I could do that!” And you know why they can’t? BECAUSE THEY ARE AFRAID! 99% of the people who would say anything negative would never have the nerve to do anything like what you are doing. It’s 100000x easier to criticize than to produce. Be sure to check the background of those who give you negative feedback and take their input with the appropriate level of expertise it warrants.
Just do.
Get out in the community
The PHP Community in general is AMAZING. It’s full of brilliantly smart people you can talk to, and gives you external input to your internal processes. If your local community isn’t as as deep into the things you are? Learn to teach them! Learn about how to talk to other developers, how to share your skills and techniques. You’d be surprised what you’re capable of.
If you haven’t spoken to groups before, consider giving a few talks to a local user group. Most likely they’re running their regular speaker lineup through the wringer, week after week, and offering to talk about something (anything!) that you know even passingly well enough to talk about (I once essentially winged a talk about design patterns — shhhh!).
The other thing being in the community will do is give you a feel for the pulse of where things are heading. There are currents and drafts in the community which you don’t catch unless you’re paying attention. See what’s on the horizon, and pay attention. You’ll never know what you’ll miss if you’re not paying attention.
Prove you can ship
I can’t reiterate this enough. The value of someone who can actually ship a product is waaaaay above someone who can only point to minor contributions in a larger project. If you’re one of those nameless cogs in a giant organization, it’s time to join an open source project or build your own thing on the side. Seriously. Nothing shows your value more than being able to to actually ship something. In the world of business, ideas are cheap, and execution is everything. Show people you can execute, and you will open doors.
Know your stuff
Read up on the important things core to being a high quality software developer: Patterns, Anti-Pattens, and Code Smells, among other things. These will teach you the do’s and don’ts of our industry, our trade. They will show you what to do, and often more importantly, what not to do. It’s the same as new inductees into Martial Arts learn Kata’s, new developers need to learn these patterns, anti-patterns, and code smells. It gives you the tools to reflexively approach similar problems in the future, and tools to expand on when you need to create something out of thin air.
Don’t be afraid to be wrong
Look at my blog, or the advice I give out in #symfony — I’m “wrong” all of the time. There’s usually a much more efficient way to do something than I recommend — but I don’t know it! I help people get stuff done. The academic stuff can be left for those Ivory Tower folk. I’m much more interested in what gets stuff done in the trenches. Getting something done is more often much more important than doing it in the best possible way. Working implementations refactor much quicker than potential implementations grow while wandering in the sea of WTF I HAVE NO IDEA WHAT I AM DOING?!. Being wrong is not a bad thing. It means both that you were strong enough in your opinion that you could voice it, and that when presented with solid contrary evidence, you changed your wrong opinion into a right one. How awesome is that?!
Enjoy what you do
We’re in this because we have a passion for either building better code, or building things people love to use, or hell, even just building things. But if you’re not enjoying your work — seriously the door is right over there. Life is FAR TOO SHORT to spend your days doing things you loathe. If you’re going to do something for your career, make sure it’s something you enjoy. I avoided doing development as an actual full career for many many many many many many many many years, because I was afraid that if I took what I loved and made it a job, it would never be the same.Interestingly, I was right — it was never the same. Now I love going to “work” almost every single day. Seriously, it’s that simple. Do what you love and you never work a day in your life. Honest.
Now with that out of the way
You may be asking yourself “But this is a recap — what do YOU do during this year?” Well, let’s see:
I went to Symfony Live.
I started blogging more regularly.
I was more active in #symfony, #silex, #phpmentoring, #protalk, and #symfony-dev (yikes, right?).
I have given three people technical feedback on their technical products which have (I feel) reasonably improved the quality of their product (I think I have a knack for this — have sent an email to php|arch to be a technical reviewer — look for this in 2013’s recap!).
The Decorator Pattern lets you to easily extend a given set of objects (either grouped via class inheritance or interface) and extend their functionality in a way that avoids needlessly duplicating code.
Our example today will hopefully be pretty close to form, and while generally contrived, will simply illustrate the use of the Decorator Pattern.
Let’s say for instance, you run a video rental service. Let’s call it… SmedRocks. Now your crazy programmers have already built your inventory tracking system as a web service, and you have no control over how they have implemented it, but you have to fight the good fight and soldier on. Features must be implemented. To make this even simpler, our API has one function:
getVideos – This returns all of the titles we have, no matter if they are currently rented or not.
I’m sure you can already see the problem with how it’s returning data. Rented movies and available movies are all mixed in! What a pain!
Ok, so let’s look at what we need to do to get started. Let’s make our API. This isn’t how you would ACTUALLY do this, it’s just an example to get us moving along:
class MovieApi
{publicfunction getVideos(){returnjson_decode(file_get_contents('http://example.org/rest/getVideos.json'));}}
Excellent! We can now get our videos, but wait — we’re in the middle of building this and example.org seems to have gone offline. This isn’t going to help us get this going! So let’s refactor real quick so we can keep going on other things.
Look at that! We’ve now defined a central MovieApiInterface which both implementations conform to, and have both an online and offline implementation which will also ultimately make writing tests easier.
Ok, now for the juicy part: we need to be able to ask this system for just rented or available movies. We could extend each API implementation, but that’s going to be some duplication we can live without.
Enter the Decorator Pattern.
With the decorator pattern, we can build an object that accepts a MovieApiInterface object in it’s constructor, and provides a uniform higher-level way to interact with our lower-level API.
Again, this is basic code to get you thinking, not optimized production-ready code.
class SpecificMovieFinder implements MovieApiInterface
{protected$movie_api;publicfunction __construct(MovieApiInterface $movie_api){$this->movie_api=$movie_api;}publicfunction getVideos(){return$this->movie_api->getVideos();}publicfunction getRentedMovies(){$movies=$this->getVideos();$rented_movies=array();foreach($moviesas$movie){if($movie->status=='rented'){$rented_movies[]=$movie;}}return$rented_movies;}publicfunction getAvailableMovies(){$movies=$this->getVideos();$available_movies=array();foreach($moviesas$movie){if($movie->status=='available'){$available_movies[]=$movie;}}return$available_movies;}}
Now we have a simpler API that actually conforms to how we will use it in practice instead of how the API designers built the service. This is great! They get their way, and we get ours. Everyone wins.
So how do we use it?
// development configuration$offline_api=new SpecificMovieFinder(new OfflineMovieApi());// live configuration$online_api=new SpecificMovieFinder(new LiveMovieApi());
Now we get the same functionality on multiple implementations, and as an added bonus, our SpecificMovieFinder also still implements the MovieApiInterface allowing us to use it interchangeably with any other service that may need our api down the line!
My good friend Beau Simensen pointed out that unfortunately, my example is a little deficient. I’ll let his gist do the talking:
Thanks Beau!
What are some other places you can think of using this pattern?
Now, Dan had mentioned that this could be done through simple inheritance instead of via interface contract, and after some thought, I realized that I disagree.
Typically with an API object, you will initialize with a remote URL, and perhaps a token:
publicfunction __construct($api_url,$auth_token)
But remember our new Cache API proxy only takes an API object and a Cache object.
publicfunction __construct($cache,$api)
This fundamentally changes the contract that the initial API object creates, and as such, should then NOT extend the original API implementation.
Now, you COULD subclass the MyApi class, and add the Cache control via setter injection, but personally I don’t think that implementation is nearly as clean as providing a proxy object to add the caching functionality.
p.s. I’ll leave QuickTime running but I think it did eat the recording of the presentation — sorry guys! I’ll put the slides up soon but I don’t know how much context they will provide given how light they were compared to the commentary that went along with it.
Update (12/07/2012):
It has been pointed out that my example was perhaps a little too contrived, so I think I found a better one.
Let’s say you’re building a system out, and you know you want logging, but you don’t know what sort of implementation you want to do for said logging. Given that instance, let’s start with our basic interface:
class MyMonologLogger implements Logger
{protected$monolog;publicfunction __construct($monolog){$this->monolog=$monolog;}publicfunction info($message){$this->monolog->addInfo($message);}publicfunction debug($message){$this->monolog->addDebug($message);}publicfunction fatal($message){$this->monolog->addCritical($message);}}
Now we have two ways of logging messages. Now comes the constraint that we want to be emailed when fatal errors are logged. Your first instinct would be to extend MyMonologLogger and add it by overloading fatal(), but then we can’t use that functionality on any other logger we ever build. How do we build this functionality in a way that we can reuse over and over again? The Proxy pattern.
class MyEmailingLoggerProxy implements Logger
{protected$logger;publicfunction __construct(Logger $logger){$this->logger=$logger;}publicfunction fatal($message){$this->logger->fatal($message);mail('admin@example.com','A fatal error has occurred',$message);}publicfunction info($message){$this->logger->info($message);}publicfunction debug($message){$this->logger->debug($message);}}
And now, no matter what we choose as our logging backend, now or in the future, we will always easily be able to have those fatal errors emailed to us by simply putting our chosen logging system inside an instance of MyEmailingLoggerProxy.