HTTP Libraries – Your Best Friends

For a programmer like me, the web is a playground like no other. Not just the information that can be retrieved, but the sheer amount of services and APIs available online make getting structured, useful data a matter of reading a few documentation pages. From social data like Facebook and Twitter to data services like Xbox Music, the web is a repository of rich information that can be manipulated as per your whim.

So while the data is omnipresent on the web, the bigger task is pulling that data from the stores they reside in. This is where the HTTP protocol comes in. With its basic operations like GET, POST, PUT and DELETE, you have a handy mechanism to retrieve, push or manipulate the data on the servers. Now, the HTTP protocol is not as simple a protocol as this blog post makes it seem. Things like HTTP Headers, application types, authentication tokens and other similar things make the task of HTTP based retrieval slightly more complex than it must have been intended to be.

This is where HTTP libraries come in. They handle the most of the heavy lifting and let you focus on the kind of content you are dealing with on a per API basis while abstracting away the inner complexities of packet and protocol rules. As someone who has been doing a lot of development in C# and Python, the HttpClient library in the .NET framework and Python’s Requests libraries have helped me unleash the raw power of the web countless times. To give you a glimpse of this power, you can take a look at how the HttpClient library handles various forms of HTTP content in this Stackoverflow answer.

The simplicity of Python’s requests library makes rapid prototyping with new web APIs a cinch. Simple methods like requests.get() and (which do exactly as the names suggest) let you examine in real time the data coming in. This knowledge can then be leveraged to build more robust languages either in Python or in enterprise level languages like C#. The HttpClient library brings amazing power in terms of its ability to not only do various kinds of HTTP requests, such as upload images programmatically, but also manipulate headers and metadata effectively, thus helping you quickly get off the ground with your application. Add to that C#’s asynchronous operation support and you’ll be developing .NET software full-time.

Now while I learn Ruby, I have already exposed myself to the Net::Http library of Ruby to take advantage of this newly discovered ability. I personally will definitely make it a point to master the HTTP libraries of whatever language I use along with the defining features of the language. Accordingly, especially if you are a new programmer, I urge you to do the same and reward yourself with this power and flexibility to leverage existing information to build upon, create cool things and make the world a whole better place!


More Interesting C++ Notions

I’ve been doing some work in C++ again the past few days, and it is always good to check back on your first computer language even though these days I prefer to be called a Lisper and use Python a lot more. This time, I was checking out the source of the utterly mind-blowing and awesome OpenCV project! While looking at the code for their Support Vector Machine class in the ML module, I noticed 2 interesting ideas and after some background research, here they are:

1. Const Functions

We all know very well about the const keyword in C/C++ and multiple other languages. This keyword helps define constant variables so that we can be assured that our code does not change the value of PI from 3.1415 to 2.71828 (hi-five if you got the hint!). So what exactly are const functions? Functions that don’t change their behavior? Pfft, of course not silly! Functions never change their behavior unless you edited them or your code got hacked because you allowed some form of code injection (I place my bets on stack smashing). In this case, the issue has to do with C++ and its object system. Now when we define an object in C++, we can mark it const to ensure that the object reference of a variable and the object itself is enduring and unchanging. But what is the purpose of methods in a class? They only exist to change the state of an object, which shouldn’t happen if we have marked an object as const. So what we do, is that we mark the functions that don’t change object state as const (similar to static functions in other languages, remember C++ only allows static for variables) so that the compiler knows that it is safe to call this function/method from a const object. Of course, you can always call a const function from a non-const object since this function is guaranteed to not change the invoking object’s state which is alright for a non-const object. The rules can be summarized as:

  1. Const functions can always be called.
  2. Non-const functions can only be called by non-const objects.

2. Typedef for Functions

Admit it, we abuse the typedef keyword. What was added as syntactic sugar to help code readability and shorten elaborate declarations by allowing aliases for keywords is overused by competitive programmers and those without a fundamental sense of code clarity. Hey, I’m not innocent myself, using typedef for an advantage in programming competitions, but the real aesthetic of this keyword is how we use it to improve our code’s accessibility.

Philosophy aside, we have been content to use typedef just to alias our variable declarations, but little do people know that typedef can apply to functions as well.

Take this code for example:

As you can see, the main idea here is that we can define an alias for the Addition function and call it as Add. To typedef a function, we need to declare the alias as a function pointer after the typedef keyword while specifying the parameters and return type as in a normal function signature (line 10). Then all we need to do is map the function pointer to a variable (line 26) and use that variable as a normal function (line 28). Voila! Now you can define easy to remember and map function names for those pesky, over-complicated functions whose names make no sense without touching (or worse, breaking) the original code!

Hope you liked these unique ideas present in C++, since the language receives enough flak for being bulky (something that I am reading upon in a research paper by the legendary John Backus). You can read about my earlier C++ notions here.


Understanding Python Decorators in 12 Easy Steps!

A really fantastic explanation to one of the more trickier concepts in Python. A must read if you want to either become a better Python Hacker or just learn better, more modern design principles! – Understanding Python Decorators in 12 Easy Steps!.