The Struggles of a Programmer
August 1, 2020
Over the years, I've spent countless hours programming. In fact, I've enjoyed it so much that I decided to pursue a degree in Computer Science! With me doing so much programming these days, I've learnt a lot more on how to think like a computer (well, minus all the proofs; computers don't really think in those). The problem is many common words now have completely different meanings to me now. I figure I might as well make a running list of all of these just for the fun of it. Again, this is all in jest; as GLaDOS once said in Portal 2, "Comedy equals tragedy plus time." So let the comedy begin!
Counting
One of the more frustrating ones is counting. Normal people count by starting at 1. However computers
are anything but normal people. Instead of starting at one when I count, I keep catching myself starting at 0 (darn off-by-one errors amiright?).
Why would I start at 0? Well in programming, a common task is to go through a whole list of items by
accessing each one at it's index. However, the first item isn't at index one. Instead it's at index zero.
As a result, you often see code looking similar to this:
// Create a list of elements
var items = ["item1", "item2", "item3", "item4", ..., "itemX"];
// Notice how i starts at 0
// So when getting item1, we use index 0
for(var i=0; i<items.length; i++) {
print(items[i]); // Prints item at index i
}
While this may seem strange, it's due to how items are stored within memory. More specifically, it's because of memory addressing and how offset on it works. It gets a little technical so I'll just leave it as that. If you're curious feel free to do some research of your own!
Number Base
We humans use a numbering system that is Base 10. Meaning we have 10 different available numbers (0-9) in each position*. When we reach 9 and want to add 1 to it, we have to carry over to the tens place. As a budding computer scientist, you learn that computers do not use a Base 10 numbering scheme. Instead, they use Binary, or Base 2. As the name implies, binary has 2 available numbers (0-1) in each position. So if we have the number 12 (the subscript denotes it's base 2), adding 12 to it would create the number 102 (12 + 12 = 102 = 210). Just like with our base 10 system when we hit 910, we have to carry to the next digit.
To further complicate matters, binary is not the only base system used within computers. Another very common one is Hexadecimal ‐ or Base 16. This means there are 16 different numbers to increment through before you have to carry. These are numbered 0-9 and then A-F (A-F stands for 1010 to 1510 respectively). So this means F16 + 116 = 1016 = 1610
With just these three bases, the number "10" could mean 1010, 210, or 1610 depending on the base in which the number is in! As a result, it's sometimes difficult to figure out what number a number is when you're trying to read it. Especially after programming in a language called C which makes heavy use of shifting and modifying individual bits within numbers.
2s Compliment
Further complicating matters, numbers can either be "signed" or "unsigned". I won't go into the specifics here, but essentially unsigned numbers can only represent non-negative numbers. To allow for negative numbers, you have to make the number "signed". This is often done in the 2s Complement format where the far left (or most significant) number is made one to denote it's negative.
This means that "10", if unsigned, could be 1010, 210, or 1610 or some sort of negative number if it's signed. Talk about confusing when you see something costs $10!
Ranges
In many classes, it's typical to hear the teacher say something like "read chapters 3 to 5 for homework." A programmer may
ask, "Is the 5 inclusive or exclusive?" Why would they ask this? Let's take a look at some example Python code:
for num in range(3, 5):
print(num)
Looking at the Python Documentation,
it notes that the range of values printed is exclusive i.e. it would print 3 and 4 but not 5. Many for loops have an exclusive range
‐ especially when indexing within the loop (the for loop in the counting example is also exclusive)
As a result, many ranges are exclusive within programming while many ranges used by people are inclusive ranges. It can be slightly confusing to determine whether someone means inclusive or exclusive if they do not specify.
Or
"Should I ride by bike today or go swimming?" Similar to ranges, is the or exclusive or inclusive? Is the person
able to go biking and go swimming? Or can they only do one? In programming, there are two types of "or"s you can use.
Typically, the first one is ||
and the second one is ^
(the caret does not mean exponent).
As alluded to, the first one allows for the both case whereas the second does not allow for it. Just like all the others, this leads to
the question "Which or are you talking about?"
Conclusion
Basically, computers are really complicated which causes programmers to "think like a computer." This mindset can lead to confusion or miscounting. After a long week of looking at machine code, you may accidentally think the number 10 actually represents 1610 when in fact it's 1010. You may also get confused on what is exclusive or exclusive when it comes to options or ranges. Implicit assumptions can sometimes lead to wrong conclusions. Again, while I have personally had all these issues after many long weeks of college programming assignments, this is all in jest. Hope you enjoyed it! If not, sorry for such a nerdy article.