CodeSOD: Wear a Wrap

While jQuery isn't as popular today as it once was, its still a widely used "utility belt" library. Its popularity arises from the fact that it takes cumbersome APIs and wraps convenience functions around them.

But what if those convenience functions are too convenient, like Ryan's co-worker found. For example, if you want to perform an asynchronous HTTP request using jQuery, you could do something like:

$.ajax({ url: urltosend, type: posttype, data: datatosend, dataType: datatype, context:cntxt, success: successfunction, error: errorfunction });

Now, we can object to passing callbacks directly instead of using promises here, but we have a complex call with a lot of parameters and so we wrap those parameters in an object to ensure they're clearly named and readable. It allows the flexibility of constructing the request in multiple steps before actually sending it.

But what if you don't want any of that, and like counting parameters to make sure you've properly passed your inputs?

function callAjax(urltosend,posttype,datatosend,datatype,cntxt,successfunction,errorfunction){ $.ajax({ url: urltosend, type: posttype, data: datatosend, dataType: datatype, context:cntxt, success: successfunction, error: errorfunction }); }

Now that's convenient.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.