Tuesday, July 31, 2012

A Test Story for Those Who Don't Like Writing Tests

Without tests, eventually you'll fall down.
My first experience with unit tests sucked. It involved a Java ecommerce bohemoth with good "test coverage" along with more bugs than should be ethically allowed in a commercial product. The company admitted it was one of the worst versions they had ever released (and supposedly some people lost their jobs over it), but the net result was me thinking unit tests were useless. If the same team who wrote the crappy code also wrote the tests, how helpful could they be?

So I decided to just write production code and leave the tests to people with too much time on their hands and no customers to serve.

Yes, that was me being arrogant, ignorant, and stupid. The tests weren't the problem. They just didn't have tests to cover the issues we were finding.
With tests, you have a chance to stay standing.

Years went by, and I got along fairly well. But then I started building something complex. Really complex. A fully RESTful API. It quickly got out of hand. I needed the ability to refactor a major controller for the whole system and know with confidence a change in one place wasn't breaking something else.

Going with Silex and their Symphony WebTestCase pushed me over the edge. Their createClient method helped me quickly test my whole API from end to end. I had no excuses not to write tests. 803 tests and 2,289 assertions later, I'm completely sold on testing.

The story I'm about to tell would have helped the earlier version of me get on board faster and may help you or a colleague finally write those neglected tests.

During what had to be a "Gee, I wonder if this quick hack will work" coding session, I added an attributes table to our system for transactions, customers and subscriptions. I was lazy, so I made a dumb decision and created a name/value table with transaction_id, subscription_id and customer_id fields. Why would we ever need more attributes, right? ;-)

I usually make long-lasting database design decisions, but this was admittedly pretty lame.

So the time came to add more attributes to products, shipments, users, clients, and stores. I knew to do things "right" would involve a database schema change, and it could impact live stores if I screwed up. Most everything we do is versioned in FoxyCart, so what you get today is what you'll have tomorrow. The catch is, all of the versions we support use the same data model.

Testing to the rescue!

The updated table was going to use foreign_key_id and type fields to keep track of the various attributes. The existing versions expect attribute data to be organized in a very specific way. Here's how I went about making the changes without screwing anyone up:

Step 1: Write tests for how it works today.

Ideally you're all hard-core with test driven development, and you wrote your tests before you even started building anything. But if that's you, this isn't your story. You're the guy who is "too busy" to write tests and would rather write code that "meets real needs" in the marketplace.

Am I right?

So I wrote 27 tests with 35 assertions on everything the current class object handled. This included outputting to XML for our API, updating and saving objects, and the like. Keep in mind, this was a real time investment.

Step 2: Break it all.

Once I had everything working, I went ahead and made my database changes in development and refactored all the code. Now almost all of my wonderful new tests failed. Some methods had been renamed or completely removed, others were slightly re-purposed or adjusted. I made a copy of the test file and went to work on it.

Step 3: Mindless coding for speed and profit.

It wasn't exactly test driven development, but this approach made my job so easy. The tests literally told me which line to fix next. Going through and tweaking the code was almost mindless. I can't tell you how many times I said, "Oh, yeah. That doesn't work that way anymore. That's a quick fix."

Soon I had things green across the board again with my new tests against the new class with a new table structure. I was stoked.

Things clicked for me. I got a greater understanding for the power of proper test coverage. I could now confidently roll out a significant change (with a required data migration step) and know our users wouldn't be impacted because the inputs and outputs of the class were verified to be exactly the same as they were prior to the change.

So here's my challenge to you, like the me-of-old, who isn't writing test code:

Start today.

Start with the very next thing you have to code. At first it will be painful, and you'll think you're "wasting time." Trust me, you aren't. Ask anyone who's worked on a lot of real code for more than a few years, and they will most likely back me up on this. You may have gotten by this long because you haven't been faced with a real challenge or you haven't had to fully realize the extent of your current technical debt.

Once you get a taste of the confidence you can have in your system and future changes to it, you'll wonder (as I have) how you got by for so long without writing tests.

Do you write tests (unit, functional, PHPUnit, Selenium, etc)? Why or why not?

P. S.: I know the pictures in the post really have nothing to do with it, but I needed excuse to post them somewhere. :) I got a bunch of cool shots from surfing last week. Check 'em out.

Saturday, July 14, 2012

What If You're Not a Writer?

My baby girl, Aria.
She's one of the reasons I need to stay focused on what I'm called to do.
First off, Jeff Goins' book, You Are a Writer, is a really great book. You should buy a copy and read it. I thoroughly enjoyed it and recommend it to anyone who's ever even thought about writing.

But what if you aren't a writer?

This post may offend my writer friends, but hopefully they'll hear me out (and offer some input).

I've been thinking about blogging lately. That's kind of what you do when you have one of these things. I've already talked about why I blog (to encourage others), but lately I've been thinking about how I blog.

I'm not one to do things half way. If I'm in, I'm all in. So where is this blog going? I'm a business owner and an entrepreneur, not an aspiring writer. I don't have a life long dream to write my own book. I don't take this blog as seriously as some suggest I should.

I haven't bothered to upgrade to Wordpress or buy a cool theme. I don't spend as much time as I should editing posts or creating new content. I don't comment on or read other blogs every single day. I'm not out building new relationships with great authors. I don't seek out people to follow on Twitter in the hope they will follow me back and grow my "audience." I'm not looking for opportunities to guest post or asking people to guest post here.

In short, I'm not trying to be one of the cool kids in the blogosphere.

All of those activities are things I know I should be doing as a "blogger" or a "writer." If I'm honest, I may have felt guilty now and again for not doing this work. A lot of my friends are on top of it. They have amazing blogs with loyal readers and tons of inspiring comments. It seems like almost every day I see tweets about the top ten ways to grow your blog, or the five things you can do today to improve your posts. Basically, I'm doing it wrong. And I know it.

One sure-fire way to be successful at nothing is to try and be successful at everything.

I've been told by a few that this blog could turn into something really good. Am I willing to do the work to make it good?

Don't be deceived. It takes work.

The answer, for now, is no. I'm not a writer. I am, however, striving to be a better communicator. This blog helps me tremendously. It helps me better understand myself and encourage others. I might be a writer in the future, but today I'm a business owner.

I don't have to be like everyone else. I still get value out of what it is I am doing. I'll keep writing because I want to encourage others with my story, and I want to hear their story in the comments and via email.

So how can this post encourage you?

I want to give you permission to write for your own reasons. Focus on the most important priorities for your purpose and remove the other distractions. Don't feel bogged down by all the things you should be doing as a blogger. Just get something done. Do the work, but do it for you. Stay focused on being successful at that which matters most for your life.

I hope you have a blog, and you share your thoughts with others. Your words are powerful and unique. You are the only one who can distribute them.

Are you a blogger who has priorities other than your blog and do you feel guilty about what you think you should be doing?

P.S. I'm super excited because my wife finally started blogging. Do me a favor and check out her blog at Simply Stokes. Leave her a comment to encourage her to keep going.

Sunday, July 08, 2012

Your Art Should Be Selfish

No, that's not me, nor is it Costa Rica. It's actually Mavricks. © jdegenhardt 

The previous two times I went surfing were horrible. During one, I was just getting pitched over the falls and drilled left and right. The other ended with lightning and a thunderstorm throwing rain so hard it hurt my face.

But tonight...

Oh tonight...

I had one of the best surf sessions I've ever had. I surfed from 5pm to 6:30pm. By the time I got out of the water it was almost dark, and I was definitely the last guy out there. The sunset was amazing, and the waves were incredible.

I caught some overhead monsters and even though the lineup was full, I was catching waves all over the place. When I close my eyes, I can still see some of the rides I had. One barrel seemed to go on forever. Another huge take off turned into a screaming face with the lip right over my head. As I started paddling back out, one of the locals actually turned to face me and clapped with a smile. Another gave me huge smile and said, "Nice wave."

I was already beyond stoked before these kudos from the cool kids, but something about it struck me.

What was so different today than the previous two times out in the water?

Other than the thunderstorm hindering things a bit, I felt like I could do nothing right just a few days ago. One wipeout had me through the spin cycle. When I pushed off the bottom, my head ran straight into the fins of my board. Ouch.

One of the reasons I had such a bad session in contrast to tonight's had to do with who I was surfing for. After getting tossed by a couple waves, I think I started surfing for the approval of those in the water instead of my own enjoyment. I also started getting desperate. I convinced myself they were all thinking, "Who is this kook wasting our waves?"

I tried to take off on some waves even though I was already too far inside. I didn't want others in the water to think I was letting a good one go by. I had a scarcity mindset.

Now, I'm not what I'd consider an artistic person. I can't draw worth a damn. I blog a little bit. I've tinkered with guitar. I write code. If I were to think about "my art," I might consider including surfing. Granted, I'm not very good at it, but a wave is like an empty canvas either way.

To be better at my art, I needed to be selfish with it. It was for me, not for the locals in the water.

Your art is for you.

I'm not talking about when you turn your art into your business. I'm talking about when it's still just pure, uncommissioned, do-it-because-you-love-it art. It's an intimate expression of your inner self. It introduces you to parts of yourself you didn't know.

If you just want to create art, don't write for someone else. Don't create a song you think others will like. Don't approach a canvas thinking about what will sell.

Do what you love just for you.

And you know what? Your art may not be that good. That's OK. You don't have a scarcity mindset because there will always be another chord progression, another blank page, another empty canvas, another perfect wave. Art is what you have inside. Sometimes it just needs to come out as is.

Let others worry about if it's good. You never know, you might get a hand clap now and again.

Wednesday, July 04, 2012

Life on Purpose

Why were you born? Why do you exist? Does the world care that you are here?

These are some serious questions, and I believe they are worth asking and answering. Depending on your worldview, you may have very different opinions than mine. That's OK, as long as you don't turn off your brain and rely on stereotypes.

According to my worldview, there is a Creator. Just as a carpenter builds a chair, the created thing is given purpose by the will of the author.

In short: You matter.

No one in all of history will have your exact genetic, social, emotional, spiritual, or mental signatures. You are completely unique, and, as a created being, you have a completely unique purpose.

Do you know what your purpose is? Do you know how to find out?

I often find myself having conversations about living life on purpose. The more I think about it, the more I believe it's a pretty good approximation of my personal brand (if I have such a thing). I'm all about living life on purpose and being stoked every single day. The idea of being "stoked" isn't just a play on my last name. It's well known to surfers and others who live life with passion and exitement.

I like the urban dictionary's definition best:
"stoked" - adjective - to be "stoked" is to be completely and intensely enthusiastic, exhilarated, or excited about something.
I want people to be stoked on life. The only way I've found for life to make sense is when I'm living it according to the unique design created for me. I've blogged about purpose and significance before, but I want to give some back story to why I believe these things.

Story Time


I grew up in a Christian home with very loving parents. I didn't know what I wanted to be when I grew up, but towards the end of high school I started tinkering with computers and building web pages. In college, I majored in computer science and was a pole vaulter on the track team.

I had so many injuries, it was ridiculous. I sprained an ankle I had previously broken, tore an ab muscle, and eventually broke a small bone in my back. After eight weeks in a space-age back brace that healed line-backers with ease, I was still hurting.

What happened next was, I believe, one of the most important moments in my life. I actually asked God, "If I'm not here to have a pole vaulting career, why am I here?" I only had one semester left, and I felt very clearly the answer was to start a Bible study.

I said OK.

Every college senior is asked, "What are you going to do when you graduate?" at least 3 times a day. My answer was always the same, "Move back to California, start my own company, and make a lot of money."

And then I read this verse in James 4:13-17:
Come now, you who say, "Today or tomorrow we will go to such and such a city, and spend a year there and engage in business and make a profit." Yet you do not know what your life will be like tomorrow. You are just a vapor that appears for a little while and then vanishes away. Instead, you ought to say, "If the Lord wills, we will live and also do this or that." But as it is, you boast in your arrogance; all such boasting is evil. Therefore, to one who knows the right thing to do and does not do it, to him it is sin.
Again I asked, "OK, you're the Author... what do YOU want me to do?" I was presented with an opportunity to raise support and go into full-time ministry. I remember what can only be described as a conversation with God that went something like this:
Papa: Sure, you can go start your own company but eventually I'll ask you for it. You'll have to sell it all and go into ministry. Or, you can go into ministry now (when you have nothing to lose) and later in life you can start your company. 
Me (thinking ministry always involves poverty): Huh. Well, it sure is simpler to give up what you don't even have then to give away everything once you get it. Easy decision.
I said OK.

From there I worked in ministry for 6 years, then for a "for profit" ministry for almost another 4. Slowly, almost without realizing it, "my company" started to take shape as a friend and I began to build FoxyCart on the side.

As I write this, I'm enjoying 10 weeks in Costa Rica (the photo above was last night's sunset view) with my family, running a business remotely, and enjoying every minute of it. What I thought would be decades later was less than 10 years. I haven't made a ton of money yet, but I believe that will come also as I'm prepared to steward it.

God knew the desires of my heart because He designed them. What has become my favorite life verse explains this well:
Trust in the Lord and do good;
Dwell in the land and cultivate faithfulness.
Delight yourself in the Lord;
And He will give you the desires of your heart.
Commit your way to the Lord,
Trust also in Him, and He will do it.
Psalm 37:3-5
Want to know how to find your purpose? I believe it's hidden in this verse. Ask yourself:

  • Do you really trust the Lord?
  • Are you "doing good" with your life, putting others' needs before your own?
  • What was the last thing you knew you were supposed to do? Are you still doing it? Are you cultivating faithfulness by sticking it out until it's done? (Remember the verse above in James: knowing the right thing to do and not doing it is sin.)
  • Have you learned the secret of delighting in God and loving His plan for your life above your own (see Isaiah 55:8-9)?
  • Are you able to receive blessing when it comes?
  • Have you fully committed your ways to God? That means He's completely in the driver's seat, calling the shots, making the decisions.
  • Really though... Do you trust God?

Are you willing to say "OK" when He shows you the next step to take?

Isaiah showed us exactly what this heart looks like in chapter 6, verse 8:
Then I heard the voice of the Lord, saying, "Whom shall I send, and who will go for Us?" Then I said, "Here am I. Send me!"
Here am I. Send me!

I'm often amazed and heartbroken when I hear people say things like, "Yeah, I know God wants me to do [insert something here], but I'm really not willing to give up [insert something here] yet." They are like the pot telling the potter he doesn't know what he's building. They're like a chair, shaking their head at the carpenter. When we do this, we're essentially telling the Creator of the entire universe who knows the number of hairs on our heads that we have a better plan.

I have a very blessed life. If you know me, you know this to be true. I don't say that to boast in my own success, but I am willing to put it on display and say, "Look! God is good, and His plans for you are awesome!" My life isn't blessed because of the outward things you see. It's blessed because of the relationship I have with the one who created me.

The Author, through relationship, communicates to me my purpose.

Be Stoked.

Live your life on purpose because it's the only way worth living. You are unique, and no one else can do it for you.

If you'd like to talk about this more but don't feel comfortable leaving a public comment, please email me at luke.stokes at gmail.