I need to write the binary function for the Processing.js library for my DPS909 class. I first took on the task simply because I needed some ‘filler’ functions. I wanted to code something small and fun on the side of my larger tasks which for my second assignment include implementing ambient light and directional lights in the library. The problem is binary turned out to be not so easy to implement and I have already written about it once. I got some help from Al and my professor, Dave, yet I’m still having difficulty. I’m at the point where I need some serious help.
All developers do day-in and day-out is solve problems. We take on large, complex challenges with reassuring nods. “Of course I can do that.” As soon as we get back to our desks, we hope it’s technically possible and pray we can learn it in time. Sometimes the problem is small, in which case we follow all the problem solving steps automatically. Identify the problem, create possible solutions, blah, blah, blah. We don’t even have to think about it, it just happens automatically. And of course sometimes we hit a wall. That’s what I’ve done. I’m so hopelessly lost I actually need to sit down and draw out this problem step-by-step to get a firm handle of the situation. So here I present the steps I haven’t written out since high school.
Define the Problem
String binary(toConvert, numDigits);
This little guy gives me insomnia. It can either take an byte (8bits), char(16bits), int(32bits) or color(32bits). Depending on the type used, a different amount of bits will be returned in the form of a String. For byte, it will always be 8, for int it will vary depending on the value. For example
binary(5); //returns "101".
If the user wants the padding bits, they can just call:
So the problem is this: when the user calls my binary function, I have no way of knowing how to distinguish between a byte and an int. For example when calling typeof on different arguments, this is what I get:
color => object
int => number
char => number
byte => number
Now, I may be able to do some tricks to single out the char, but it’s the int and byte that’s causing me grief. Okay, there is the problem: When my function is called, I have no way to determine what type was used to declare the variable that was passed to my function, thus I don’t know how many bits my return string should contain.
Now, let’s take a look at possible solutions. Of course, brainstorming has no filter, so every idea is considered. Who knows, it could lead to another avenue which could hold a solution.
Drop the course. Haha, I’m just joking. I’m here to learn, right?
Give up. Yeah, I thought about it a bunch of times. Walk away from the problem and watch my course mark get slashed. This is just a last resort. Hopefully it won’t be necessary.
Assign it to someone else. My class if full of smart developers, I’m sure some of them could solve this faster than I can. I may even be able to get someone online to do it for me for free (please?).
Rent-A-Coder? Nay, I’m still a student. I like to keep my money where I can see it, which is in my wallet.
Break the binary() standard another way. Force the user to enter in the number of bits to return. Does this ever sound sweet. The function would still be testable and prove it is working like it should except for the argument list. I could then later figure out the finer details of how to get the type and remove the unnecessary argument.
Postpone to 0.3. I can always take on something else for 0.2 and come back to this horrible, horrible function for 0.3. However, I would be very careful about my selection this time.
Do some crazy regex. Al suggested a piece of regex. But since the parsing code runs over all the Processing.js code first and converts it to JS, I won’t have access to how the variables were defined anymore (byte, int, char).
Create some meta table. Generate a table on startup and everytime a user creates a variable, throw in the reference and some meta data about that variable. Sounds like a lot of work. It could also break a lot of other library code. What if the references change? What about locals? What about casts?
Convert ‘primitives’ to objects. If I created Int, Char and Byte objects whenever a user declares int, char or byte, I could put in as much meta data I want and I could later query that within binary(). Unfortunately this will also require modifying most of the other functions already in the library.
Making a Commitment
Now is the time to pick a solution from this crappy list. I guess I should choose the one that sucks the least. But I also don’t want to start down one path, then hit another wall. I want to be sure the solution will work. Unless I hear otherwise, I’m going to force the user to supply the type/size. Once I know my function is working with the training wheel argument and I have time left over before my 0.2 submission, I’ll take another crack and this. Rest assured, I’m not at all happy with my selection, but it doesn’t look like I have much choice. Unless of course you can offer a better solution. I’m also running out of time and I know putting 3D and lights into the library will be exponentially more difficult than this. Or will it be easy? I have no idea, I’m just a developer.