X-macros: Like the X-Men, except that nobody likes them.
Here’s why.
If you aren’t familiar with what x-macros are, they’re a way in C and C++ to embed lists of data tuples into a program, while minimizing the amount of boilerplate, at the cost of using the preprocessor. For example - let’s say you have a list of products, each with a unique product code, and you want to be able to convert those codes into nice product names.
Continue Reading
So there’s this variable naming convention called “Hungarian typing”, which defines itself by naming variables according to their type. The classic example is:
int iPotatoCount = 53; const char* sPotatoName = "Russet"; With the upside that where the variable is used, you can clearly see the type:
printf("Number of potatoes is %d\n", iPotatoCount); Which is not super useful for two reasons: One, modern text editors will just directly tell you the type if you hover over the variable, and two, your functions should all be short enough that all of your arguments and local variables should appear on the same screen as whatever code you’re looking at (or same page, if you print out your code).
Continue Reading
So during the Second World War (the less great one), in the Pacific theater, the Americans and the Japanese fought largely over pacific islands (famously places like Okinawa, Guam[1], Palau, etcetera). This was almost entirely for logistical reasons - The US needed to control the area in order to launch bombers and resupply forces in the region, and they provided access to the Japanese homeland in the event that a direct invasion was required.
Continue Reading
So I play this game, World of Warships:
It’s a fun game. You drive around and shoot at boats and such. Highly recommend it (despite the fact that their marketing department is seemingly high on drugs.)
Like many other games, it generates replay files. A small community has sprung up around building programs that parse the replays.
Mine is here. There are many like it, but this one is mine.
Continue Reading
It’s been a couple weeks, so let’s focus up and get down to business. Which, as we all know, means promulgating Rust talking about software engineering.
It was reported by the creator of curl a few weeks ago that half of curl’s vulnerabilities are C mistakes. Notably, this is only talking about security vulnerabilities. (also, fun fact, curl is apparently used by that really far away helicopter, Ingenuity. If Ingenuity is hosting a WiFi AP to serve its camera data, JPL isn’t telling)
Continue Reading
class MyClass: def __init__(self): self._signature = None def get_signature(self): if self._signature is None: self._signature = self.peek_register(SIGNATURE_ADDR) return self._signature We’ve all seen this code before. You have some getter that you want to return a value, but it’s expensive/inconvenient to initialize, so you cache it. You don’t want to do it when you make the class, maybe because not every class ends up needing one, maybe because it’s difficult to initialize in the constructor.
Continue Reading
Okay, so first off, floating point numbers are the worst and you should never use them.
But if you are, they say programmers should be aware of coercion rules. “ints promote to floats”, they say. “And floats promote to doubles.”
You probably had to go to a school and pay money for somebody to put that on a slide so that you could forget it and look it up later. So look at this code:
Continue Reading
Okay, finally, we’re going to talk about something other than errors. This is the story of how I destroyed $25,000 of company property and got a pat on the back for my efforts.
Or at least, I haven’t been fired.
It’s not clear to me how to describe the code. Maybe this?
It’s hard to portray “I deleted 1,700 lines of code.” But that’s what I did: I deleted 1,700 lines of code.
Continue Reading
Last time we talked about errors. And though I’m loath to think about anything for longer than a week at a time, errors are so core to our existence as developers that I feel we must talk about them.
(obviously, they’re core to your existence, my code has no bugs and therefore never errors)
In particular, I want to tell you the story about this error:
Error Code: -1074114855 For context, these days at work I’m playing a bit of customer support for a customer using a beta product in Japan.
Continue Reading
The astute reader may have noticed, I pulled a fast one in the last blog post. Last week, we replaced this code:
if (regex_match(portIterator->second, boost::regex("0*([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]);"))) { ... } with this:
const int port_no = std::stoi(portIterator->second); if (port_no <= 65535) { ... } and we sort of left it at that, and moved on to other things.
But these code pieces actually aren’t equivalent, for two reasons. First, stoi will accept a negative number, so the condition should be: port_no >= 0 && port_no <= 65535.
Continue Reading