Constraints Driven Development…my favorite line of code
This post is inspired by the recent blog battle topic “My favorite line(s) of code” and a recent line of code that I was, literally, Laughing-Out-Loud at. (I apologize for disturbing any of my neighboring SEPeers.)
Not the kind of laughing where you are like “man, what were they thinking!?!”. This was the kind of laughing like, “oh yeah, I’ve had to do some crazy stuff before…I understand what you were doing there.”
This simple, yet beautiful, line of code took me a minute to understand what it was actually doing.
Yes, the readability was less than desired.
Yes, it was nearly impossible to search for in the code base.
And yes, it was flat out ugly (in a beautiful way).
But sometimes, you are so constrained, that you have to come up with amazing little tricks like this one. So, without further ado…
applicationAfAppPrintln(“%p%pSTATUS-UP”, “APPLICATION_”, “CODE-AREA_”);
If you’re like me…you were thinking – “What in the world!?! Why did you do that to simply print the string ‘APPLICATION_CODE-AREA_STATUS-UP’?????”
(For the record…the strings/names have been changed to protect the
Okay, let’s add some clarity.
First of all, this is in an application-framework that was intended to be integrated into other applications. So the developer had constraints on their constraints, with an extra side of unknown-constraints…and limited resources for dessert.
Secondly, this is an embedded C application and the footprint had to be ridiculously small – we are talking memory usage, code size, power consumption…the whole kitchen sink.
So what were the developers thinking?
Well, with our compilers, a string (“APPLICATION_CODE-AREA_STATUS-UP” for example) will get put into a section of memory called CONST.
This CONST section of memory is commonly shared with the “code space” where your code lives after it is compiled onto your target.
Many times in embedded applications, we have to come up with ways to save every. single. byte. that we can save in our work.
In this case, these developers noticed that if they pulled out certain strings, the compiler would make them common in the CONST section of memory (“APPLICATION” in this case) .
So let’s do some math…
If every character is 1 byte, and you wanted to print the word “APPLICATION” 20 times…that would be 220 bytes.
Combining that with the string “CODE-AREA” and you’ve got another 180 bytes.
And the only thing you cared about changing was the string “STATUS-UP”…then you just saved 380 bytes!
Creative solutions like this one, can add up to save thousands of bytes.
Now, 380 bytes might not sound like a lot of space to most people…but try to imagine if you were developing an application-framework that was targeting platforms with only 48,000 bytes of code-space? (What a coincidence…it just so happens that my current project has a constraint of 48k bytes of code-space, too.)
Sometimes, constraints are what make us more creative and innovative.
I would love to hear about some of your Constraints Driven Development stories!
- What is your favorite line of code?
- What other creative solutions have you seen in the wild?
- What constraints have helped you think outside of the box on your projects?