I’m not setting out to explain recursion in full detail right now. I just want to do my best to relay the concept.
Recursion, in simple terms, is logic that depends on itself. I honestly can’t tell you whether it’s the simplest or the most complicated of concepts. It’s sort of both. Let me attempt to explain.
It receives some things (a and b) and it returns something (the sum of those). It starts when it is called and it ends when it returns. It’s as simple as that.
But a recursive function is one that calls itself. What?! It’s a little weird. Let’s try this…
Now, technically this is a recursive function, because it’s calling itself. But it’s not a very good one.
Can you tell what’s going to happen? It’s fun. It’s called a stack overflow. That is, when we call the function and it calls itself, that causes it to call itself, which causes it to call itself, which causes it to call itself, lather, rinse, repeat. Eventually (rather quickly actually) our system runs out of memory to hold this infinite stack of function calls and it throws an exception.
So, we successfully created a recursive function, but we didn’t limit it in any way. It’s like when you get a microphone too close to a speaker and it forms a feedback loop. Or it’s like when you plug a video camera into a TV and then point it at the TV. Or when you look in parallel mirrors just right. In every case, a somewhat perfect case is fulfilled. The output becomes the input… exactly as it is every iteration. The amplification continues, and you get strange and very extreme results.
Let’s change our function a bit so that instead of adding two numbers together, it simply takes a single number and then continues adding that number to itself minus one until it runs out of numbers (gets to zero). So you should be able to see that we now have a limiting case until we run out of numbers. That’s important.
Notice that the condition inside the function is asking if we reduce n by 1, is that still going to be bigger than zero?, and if it’s not (if n is 1 and thus n - 1 is zero) then we just return n. We don’t call ourselves again. That’s the critical piece. In order to avoid a stack overflow, there has to be some logic in your function that at some point allows the function to complete without calling itself again.
So, recursion obviously works great for finite mathematical operations like my example, but where else might you find this concept in the wild? The answer is all over the place. As it turns out, there are quite a lot of use cases. If you have ever worked with a folder structure or perhaps with a tree view control, you’ve likely discovered that recursion can turn hundreds of lines of code into a handful. Any data structure that contains an entity that can be involved as either the parent or child in relationships is inherently recursive. After all, who knows how deep the tree is? Who knows how many levels deep the relationships go?
I hope this helps you wrap your head around the concept of recursion. It’s a foundational one and mastering it gets you one step closer to being a computer scientist.
Have you noticed the overlap between WCF and Web API? I did.
And not only have I noticed it, but I’ve watched both of the frameworks change so that the overlap between them evolved, and I’ve done my fair share of speculating about what the potential paths forward are and when to use which.
Overall, the story with WCF and Web API is a convergent and not a divergent one. The teams at Microsoft are completely unified and so is the strategy. Nevertheless, the two frameworks exist as does the overlap and developers’ various solutions using one, the other, or some combination, so a little discussion on the matter might be helpful. A smidgen of official guidance on the subject is available from Microsoft’s developer network in an article called WCF and ASP.NET Web API.aspx”), but it is by no means wordy and leaves a lot of architectural concepts and decisions to you, the developer.
I’ll try to be more specific and prosaic on the matter, but I won’t obviously be able to make any decisions for you, especially where existing solutions are already in place. I can, however, let you know a bit more about where Microsoft is on the matter and where I am as well and you can use your own noodle and your own intimate knowledge of your problem space to make the best decision. After all, that’s what they pay you for, right?
After reading this brilliant discourse on the topic, I highly suggest you take the time to watch Daniel Roth at TechEd North America 2013 present Serious Web Services. You’ll walk away from that an expert.
Here’s where I advise you start - if you can choose Web API, choose it. You can choose Web API if the following are true for you…
- You can stick to the HTTP protocol. HTTP is a layer above TCP. That means that is uses TCP, but it adds some stuff. In adding some stuff it makes the communication just a tiny bit slower. Here’s a typical envelope for an HTTP request…
…and for a response…
xhr(), and likely about a billion more. If you’re in C# you have the
HttpClient helper class. Even if you don’t have any helpers, composing a text message like the sample above wouldn’t be rocket science (unless of course you’re in the aerospace industry).
WCF can speak HTTP, but it can speak a number of other protocols as well. The cool part is that all of the work you do to define your entities and your operations is independent of the transport protocol too. That means you can talk HTTP to some clients and straight TCP to others. That’s a big advantage.
If you know, however, that you can get away with only talking HTTP, then read on and keep on considering Web API.
- You don’t have a requirement to support SOAP. HTTP is an envelope on TCP. SOAP (Simple Object Access Protocol) is another envelope on that - on top of HTTP. And it’s a significant one too. Look at this SOAP message. Yowzer! And this is the best case scenario. Actual implementations usually end up with a lot more piled on top.
As you may spot immediately, SOAP is XML, so besides its inherent, relative verbosity, it is also subject to the verbosity of XML. When you have to
<thing> wrap all the things
</thing>, they start to get pretty long, n’est pas? SOAP is the foundation of the WS stack - a suite of standards to determine one way to implement web services. The problem is the WS stack is pretty thorough and pretty pervasive in the enterprise. So if you’re an enterprise developer trying to introduce some agility to your group, you may run up against the constraint of having to speak SOAP.
- You don’t need Reliable Messaging, WS-Transactions, or any of the other WS* junk. Web API implements web services using a variety of primitive and largely preexisting protocols such as HTTP, WebSockets, and SSL, so you don’t get the various higher level protocols such as RM or WS-Transaction. You don’t get the power of those protocols, but you also avoid the headache in my opinion. I have always felt the process of implementing such protocols was relegated either to suspicious black magic libraries or pain staking implementation ceremony.
There may be more, but those are the basic constraints I can come up with on a moment’s notice.
It seems like everyone is talking about the raw and simple HTTP REST JSON approach, and that’s great, but there are a couple of other approaches to consider. One of them is a shaking of even the HTTP protocol. You can accomplish that by embracing web sockets. You can implement an entire API using Signal R and it would surely be very fast and very impressive. Another alternative is to keep the HTTP envelope but get more specific with your data format specification by embracing OData. A client can look at any OData set of data with standard tools or code because they’re always formatted the same. Additionally, OData allows me to query my data with clever URL strings so I only get back the data I want. OData is nifty. Here’s a simple HTTP request to an OData resource…
This is a simple GET to an OData resource (a collection of widgets it appears) that will take only the id column of the widget with the name “Widget1”. So that is going to return a microscopic result that looks something like…
I used to consider it a strong advantage of WCF that I was able to create a WCF Data Service (a simple class inheritance), point it to a compliant data service (such as an EF DbObjectContext), and presto I had a full OData implementation of my dataset. It’s a great party trick (depending on the party), but Web API has matured to the point where the same solution (a very useful OData feed from a dataset) takes hardly any more effort and has the added advantage that it uses scaffolding so my service implementation is not hidden behind the façade of the WCF DataService class. Additionally, (and of critical importance) the WCF team has put some guidance out there that WCF Data Services is not the way forward. They’ve done a commendable job of implementing the basics of OData v4 to alleviate as many workplace constraints as possible, and I’m hearing, guessing, and hoping that they’ll do an equally commendable job of supporting existing namespaces and implementations, but it’s a sunset moment for WCF-DS.
That’s all I have on the subject for now, but feel free to engage me and the community via comments below.
Thanks to the following sites for info and inspiration…
Based on two laps around the exhibitor hall at OSCON and a couple of years working in the sometimes awkward space between marketing folks and developer folks, following is my list of the 10 most overused words in tech.
You’ll find these on a zillion websites, banners, and those spring up signs that people drag around in the airport. There’s probably a name for those, but I’m glad not to know what it is. If you know, don’t tell me.
It’s all an attempt to make an API or a service that has no material substance come to shape and life in the mind of a developer so that he’ll use it and love it and maybe make some contributions to the repo or pay some money for it.
And here they are…
- ninja. I have no idea how this expert in stealth, martial arts, and throwing stars came to represent the height of developer prowess, but it has… even though most developers wouldn’t be caught dead in black uni-suits or toe socks, and likely would be caught dead if they tried sneaking into the enemy’s lair under the cover of night.
- awesome. The term that attempts to capture that moment when a mortal lays eyes on a select sunset, a view of the milky way, or the power of God himself is reduced to describe the experience of successfully requesting a client record and retrieving his sales history.
- guru. One who knows everything about a religion becomes one who knows everything about his programming language.
- leverage. Sometimes the word use would work just fine, but for some reason we’re still compelled to leverage things.
- cloud. A list of clichés would not be complete without cloud. Never has a term risen so high so quickly. Hey, that sort of works! Do keep in mind that your cloud is actually on the ground. I’m just saying.
- stack. A choice of collaborating technologies building on top of one another. A stack. Got it. Yep, that describes it.
- bits. You’ve never seen a bit, but you talk about them all the time. Is that in the latest bits? Do you have access to the latest bits.
- mind blowing. Sometimes it does feel like that, but not often. I can recall on fewer fingers than has one hand the number of technologies that have felt like they blew my mind. And even then my mind remained literally unblown.
- real time. Even though most of the time it’s not true, it’s fun to say that your software runs in real time. The more honest claim near real time performance. What’s near? Within the hour?
- bacon. And making the list due entirely to the fact that I saw it used an entire 3 times in one exhibit hall… bacon. I love bacon for breakfast. Heck, I love bacon for lunch and dinner and dessert too. It feels like a cheap shot though to compare your API to bacon so I’ll love it. I’m still going to use your API though… since you said bacon. I do love bacon.
codeShow is a app for learning to make apps. It’s very meta in that way. The whole project is an open source project with community contributions. Use it to learn the web platform and Windows app development.
codefoster.com is entirely overhauled and with it my provider for blog commenting functionality.
I chose DISQUS and I’m pretty happy with it so far. It was very easy to plug in and gives me the basic commenting functionality that I’d like. I like the fact that many web users have used the system before and are already authenticated. I also like that comment threads can be tied to the page URL and are not tied into the content management software.
In an attempt to engage you, my readers, I’d like to offer an Oakley backpack like the one pictured below except it’s branded with the new Microsoft logo in gray.
To enter, either leave a comment below with your answer to the following question: What are the most important 3 items to carry in your day pack and have at the ready? or leave a comment on any other blog post on codefoster.com.
Unfortunately, I can only ship to the U.S., but if you’re overseas I’d still love to hear your answer.
I’ll pick a random winner this Friday the 25th and announce it in a comment and on Twitter.
I’m starting a new venture and I’m announcing it now.
It’s been on my mind for some time and I’m finally going to make it happen.
It’s called CodeChat and it’s a casual, weekly conversation about code and the myriad of periphery topics that may be of interest to developers. Chats will be targeted at 30 minutes, but who knows. They may be 15 minutes or 40.
The first episode will air on July 7, 2014 and new episodes will be released every Monday morning until the demise of the universe.
CodeChat will be broadcast via Channel 9 which will allow me to distribute it in various formats simultaneously. I, for one, listen to a lot of podcasts and never want to be limited to audio or video. If I’m driving or jogging, the video is pointless, but when I’m just sitting around it’s great to have a video feed for a more immersive experience.
I hope you’ll get some value out of the discussions I have with Microsoft employees, community developers, and whoever else I decide to bring on the show.
I’ll be interviewed at an upcoming online Microsoft Learning event. Join to get tips from experts on emerging opportunities for developers. The panel will include senior executives from Dice, PayScale, and the Center of Excellence for Information & Computing Technology for Bellevue College. Register now
Ugh… if I hear one more time about the death of the PC…
I don’t mind naysaying persay, although I try not to partake. It’s very popular to be negative. Complainers are a plenty. A person can do better though than to spend their time getting frustrated as a response.
I I tend to enjoy more talking about what’s good about this and that whether it’s born out of a tech giant or a tech startup. I don’t mind pointing out what’s great about the competition. Where would we be without competition?
But the death of the PC topic is annoying because it is carrying so much more momentum than sense, and I don’t like things that carry momentum and not sense.
Critics talk about the recent 14% decline in PC sales as if it’s the locked trend that will soon see us into a world where PCs don’t exist, and furthermore, they continue to attempt to tie the future of Microsoft to that number.
A couple of thoughts. First, 14% is not a linear trend to the demise of PCs. Second, Microsoft is decoupled.
To my first point, I think we are quickly headed into a world where PCs don’t exist… at least, PCs as we now know them. The big box you slide under your desk - going away. The laptop that confines you to a mouse and keyboard and requires a special backpack - goodbye. The PC is already experiencing an evolution swift enough to call a revolution. Watch this Intel commercial for example.
So what’s with the 14% decline? That’s just dinosaur PCs taking their right place. When my wife wanted to look up a recipe three years ago, she dragged her 75 pound Dell laptop out and looked it up. And it seemed great compared to trekking to the office to spin up a desktop. But today the dinosaur is simply not the right tool for the job. If we were contemplating a device purchase for looking up recipes, we would contribute to the 14% decline in sales by buying a tablet device that turns on faster, stows quicker, and can be held in one hand. And actually, that feature on the Surface where you can turn the page without getting brownie batter on the screen is pretty awesome too!
There are a lot more than 14% of the use cases that a tablet or a phone will do great, yet people are still using their dinosaur PCs for that. Why? Because they have them. Not everyone goes out and buys the latest gadget in its infancy. But they’re not going to spend money on a new 15” screen when all they need is 8. We know that.
So expect to see PC sales drop. They’ll take their right place, and consumers will be better off using technology that applies to the task at hand. But don’t bet the farm that PCs are going to dye, because I know a lot of people that would sooner cut steak with a butter knife than use a get real work done.
And to my second point. Microsoft is not a static entity and does not have its future tied to the dinosaur PCs. Some components of Microsoft’s business that I think you’d agree are significant mostly if not entirely immune - Azure, Office, Xbox. And even Windows has proved its agility.
Never underestimate the creative engine at Microsoft constantly innovating and pivoting to not only keep up with this higher tech world, but to lead where possible as well.
If you’re looking to take an exam and get MCSD certified, have I got a deal for you!
I’m going to give away 25 (that means you have a high chance of winning!) coupons that will get you two exams for the price of one.
Here’s what you do to qualify…
I’ll randomly draw 25 people out of everyone that qualifies and send them a code via email to redeem your prize.
The drawing will be Friday, Oct 25.
Because I didn’t. Not until recently.
I wish I had time to install and try both of these projects and report my findings to you, but I’ll have to leave the sleuth work to you. I’m just reminded by this project about how many amazing things come out of the MSR group that push the boundaries of computer science and turn the crank of true geeks everywhere.