Callbacks, callbacks, callbacks!

May 18, 2015

Waiter,

There’s a promise in my spaghetti.


So, dig, I like promises.  People I work with and people I talk to think I don’t but I really, genuinely do. Promises (in a computer science way) are just plain awesome.  Here’s the idea:

Program module: Yo, system, I want to do something and I want it to happen on another thread so I can keep doing stuff.

System: Okay. I’m doing it. Here’s an IOU, let me know when you are ready to collect.

Program module: Okay, I need that stuff now.

System: I’m not ready yet. Please hold.

Program module: okay, I’ll wait.

System: Okay, I’m done now, here’s the stuff you wanted.

Program module: Cool, thanks. Game on!

What happened here?  Basically a promise was issued and the program continued running with an async process continued in the background. When the program was ready for the data, but the data wasn’t ready, the promise became a blocking operation. Once the system was done, it delivered what was needed and the block was released.  This is awesome because you don’t end up with something that blocks up front. This can be annoying when you need something to appear completely transparent and non-blocking.  Such is the way of the world.

Javascript promises are, let’s just say… different. Everyone likes to say “oh they stop callback hell! They are the magic bullet!” Incorrect!

Promises are just syntactic sugar over a callback structure, which basically seems like a big fat lie to me. I don’t like the idea that I am “getting rid of callbacks” and really all I am doing is tucking away functions in a place where I have to do a TON of work to get tests around them. I have seen some of the worst code ever written inside of promise callbacks because “hey, it’s promise. It’s cool, man. You don’t need to test that.”

cough yes you do cough

Did you notice how I said “promise callback?” Yep, there it is. Promises and callbacks are still the same. You still pass in callbacks. You still have to handle the asynchronous nature of it all. This is where I climb to the mountaintop and proclaim “the cake is a lie!”

Then there is the q.all argument:

“What if you need to do a bunch of things and then call back? That’s like… complicated, man.”

It is. This was one the one concession I make… well, I USED to make. Q.all is pretty cool. Don’t get me wrong, anything that will bundle up a bunch of async calls and then hang out until they are all done, THEN callback is pretty darn nifty.  The problem is you are still introducing this idea of promises into the mix.  Stuff happens, the spell is woven and magic happens… Magic that is basically untestable.

So, let’s stop trying to paint the callback turd with a single layer of abstraction that makes things murkier and more difficult to understand. Promises are magic that have become lingua franca of async spaghetti. Instead, let’s have a look at a handy new library borne of Node and easily pulled into the client: Async

Dear promises, never send to know for whom the bells tolls; it tolls for thee.

Async deals with callbacks differently. Instead of wrapping everything up in a nasty set of promise.then().then().thens, try async.waterfall(). It’s like magic:

async.waterfall([ firstOperation, secondOperation, thirdOperation ], finishFunction);

Now your code actually says what it is doing. Callback hell is gone. Promises are eliminated. All is right in the world.

But what if I want to do a bunch of stuff that doesn’t happen in serial? Parallel. Check it, yo:

async.parallel([ anOperation, anotherOperation, yetAnotherOperation ], finishFunction);

It’s like magic right?

In closing, all I would ask is, if you are going to write a bunch of async stuff, please give me and everyone else on your team a break. Async is the way to righteousness and the light. Promises are great when you absolutely, positively must have it sometime later, but most work can be done with async. Give it a try and make your code a better place.

To New Beginnings

May 1, 2015

Wow! It’s shocking that the last post I wrote here was almost a year and a half ago. I guess it’s been busy!

In that time I worked for an education company, got laid off, found work at a startup and wrote a LOT of code. Am I older? Definitely. Am I wiser? I certainly hope so. If anything I gained a lot of insight and found evidence for my suspicions.  User experience is crucial and the only people who can guarantee a good experience are the people who create the software that users touch.

I spent much of the past 10 years trying to figure out what I wanted to be when I grew up.  I went through phases of user experience, accessibility, project management and research.  All the while I was busy writing software.  What I ultimately discovered is I like writing software.  I like creating things.  There’s a rush that I don’t get anywhere else but there.  That doesn’t get me the answer I was necessarily looking for, but I suppose it gave me something I could hold on to.

Another important thing I discovered along the way is there are a lot of people who have a lot to learn (including me) but I discovered something I didn’t believe before now: I have something to share.  For as much as I don’t know I have been fortunate to learn a lot of lessons and I can share that with people so, maybe… hopefully, they can either grow from what I experienced or, at least, I can help buoy their spirits a little by showing that everyone makes mistakes.

Wisdom is what you get when you don’t get what you want.

I didn’t come up with that phrase, but it sure sums up what I feel to be true.  Just because things didn’t work out the way you originally planned or wanted doesn’t mean that things didn’t turn out at all. Failure is such a better teacher than success that I just can’t imagine why people fear failure so much.  I aim to fail every day. Failing is awesome. Success just means you’re done, but when you fail, that means you went from working on something routine to a fascinating new puzzle. What a great opportunity!

I could go on and on like this for pages, talking in circles and rambling about vague pasts and lessons learned, but that isn’t what I set out to do.  This post marks the beginning of a new era for me and this blog.  I won’t promise that I will never post long diatribes about strange new discoveries I may or may not have uncovered myself.  What I can say is I have a focus today like I never could have imagined when I started writing this blog years ago.

I want to make stuff awesome and make awesome stuff. I want you to do the same.  I still want to make the web a better place, but maybe, somewhere along the way, I, or we, can actually make software better.  Let’s toast to new beginnings and new projects.  Here’s to all the projects that never quite were and the ones that are yet to come. Let’s make software. Let’s make it great. Let’s make it together.

Yo dawg, I heard you like accessibility...

Sep 5, 2013

So I put accessibility in your menus so your users can use your site even if they are disabled.

Okay, so I suck at yo, dawg jokes. I did, however make a handy little menu script.  It’s small. It’s functional. It supports accessibility features like roles and keyboard access. It’s easy to turn some plain old HTML into a menu without having to know ANY javascript.  Am I a mad genius? Quite possibly, but I’ll take what I can get.

So, here’s the deal, you can download the alpha release at the following link:

https://github.com/cmstead/ClickBeetleJS/releases/tag/v0.5-alpha

If you want to actually code and help make it better you can fork the project here:

https://github.com/cmstead/ClickBeetleJS

Important things you should know:

ClickBeetleJS requires jQuery. Pull requests won’t be accepted without passing tests associated. All my tests are written with QUnit. Please write yours using the same framework.

I hope this is useful for you.

Help me make the web a better place.

We Live in a Biosphere

Jun 27, 2013

You may or may not be aware of this, but when we are interacting with an application, we are entering a biosphere. People always talk about suites of applications as being an ecosystem, but even within the constraints of single app, we are still interacting with a smaller, but equally important, ecosystem.

About three years ago, I wrote a blog about page-behavior taxonomy or, as I generally refer to it now, behavior taxonomy. This taxonomy is really a mental model the user creates based on previous interactions they have had within your page, site or application.  It’s really crucial to understand that as you present links, behaviors and interactions, you are training your user.  When you break that interaction model, you are breaking rules the user expecting you to follow.

Consider the biosphere projects scientists construct for the purpose of experimentation.  Everything in that tiny ecosystem must work together properly.  If an element is introduced which changes the ecosystem, it could cause a breakdown of the entire experiment. These small changes are what happens when the biological equivalent of microinteractions are not considered.

The key to considering these mental models is to look at the application as a whole and start developing an expectation of what the user will experience throughout their time in your app or site. If you look at the app as a whole early on, then it becomes clear what each interaction should bring.  Think about how people are going to move about.  What kinds of input are they going to give.  Why are they doing it and how would they expect it to work?

Of course you should expect change, but the if the initial behavior taxonomy is sound, new additions will only serve to enhance the experience.  If, on the other hand, care is not taken to develop the rules to which your app should adhere, you will paint yourself into a corner.  You will make decisions early on which will force your hand later on.  If you choose to break from the model, you will also break your users trust If your app is something that is going to be foisted upon users, they will learn to deal with it and resent you.  If your app is something users interact with by choice, they will leave.

In the end, we are all in this little biosphere together.  Let’s live in peace and harmony.  Consider the environment and care for it. Your users will thank you for it. Consider your app’s behavior taxonomy and make the web a better place.

Monetizing the Web

Jan 16, 2013

Today I read an article about Irish newspapers plotting to charge for linking to their content. We’ve all heard about this before and people seem to get up in arms about it every single time it is mentioned. I think this frustration and disgust are misplaced.

See, here’s the deal: if you want to make money from people using your site, you have to get them there.

Ultimately, there is a paradigm shift that happened with the web which traditional media is having a hard time wrapping their brains around – links are gold. They are the currency of the web and the word of mouth in the electronic universe.

At one time, it made sense for media outlets to limit the distribution of their content through other media channels. It hurt sales and trimmed valuable advertising money from their pockets. They never said “don’t mention our article to people or we’ll charge you.” That’s foolishness. Big media wanted, and still wants, people to know they got the scoop before anyone else. It keeps them in business.

So why limit linking? It limits the number of page views and, subsequently, the amount of advertiser money that comes in.

I get it. They want you to click around the site looking for that one piece of content you heard about. The problem? News sites pretty much universally stink with regard to user experience and findability. If it isn’t on the front page it may as well have never happened. This is the heart of their problem, really. They are spending so much time trying to milk every penny out of someone’s pocket they can’t see they need some real help.

My message you to, media outlets: Fix your sites. Make them easier to use. Put your reader first instead of your pocketbook. The advertising money will come. If you want to erect paywalls, fine, but don’t kill the one thing which might save you. Protect your links. Treat them like someone handed you free money. Encourage people to share. If you don’t, I won’t be angry, I’ll just never know you existed. Do us all a favor and make the web a better place.