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]);"))) { ... } if you look at it hard enough, you can tell what the regex does. But, if you’re not familiar, it comes with the comment:
// check the RPC port number which is an unsigned 16-bit integer. The value ranges from 0-65535 (also, I highly recommend the website regex101.com, if you don’t have it already bookmarked)
Obviously, regex isn’t anybody’s first choice to see if a number is an integer.
Continue Reading
It’s a point that I have, and will continue to, drive home on this blog: In professional software engineering, code readability is paramount. Its importance is second to none.
When you’re evaluating a language, especially, it’s important to consider more than just whether the programming language is turing complete (or, more generally, whether it can do all the things you need it to). You should also consider how conducive the language is to being read or reviewed.
Continue Reading
So I was working on some driver code for a device the other day, and came across this interesting bug. The device has a clock rate that a user can specify, they can choose from one of several options (call them 10MHz, 11MHz, 40MHz, and 44MHz). The user can also pre-configure a “multiplier” option on their device (by loading a different FPGA bitfile), which has two options: x1 and x4. As you might’ve guessed, the 10MHz and 11MHz selections only work when the multiplier is set to x1, and the 40MHz and 44MHz options only work when the multiplier is set to x4.
Continue Reading
I’m going to break from my usual style (again) and go for a more ranty style with more blatent product placement.
If you like this kind of content, remember to smash that like button and catch me live over on Twitch. (I’m just kidding, I don’t blog or program live on Twitch, although maybe I should?)
Anyway. C/C++ includes. Love them, hate them, but we’ve all seen (and written) code like this:
Continue Reading
} + closeDir(dirPtr); } Today’s post is slightly unusual - we won’t talk about code, or coding conventions, or about how much more clever Rust is than C++ (which is a lot), today we’re going to talk about Process. And source control. This snippet of code is small, and ultimately not particularly important, but it highlights a not often talked about process in software engineering.
So the codebase I’m working in now has a dependency on a “SDK” provided by one of our vendors.
Continue Reading
So several months ago, I had to write this (C++) function:
std::vector<uint8_t> extract_bitstream(const std::string& original_bitstream) { std::string encoded_bitstream(original_bitstream); // Strip the whitespace encoded_bitstream.erase(std::remove_if(encoded_bitstream.begin(), encoded_bitstream.end(), [](char c) { return std::isspace(c); }), encoded_bitstream.end()); // Decode the result namespace bai = boost::archive::iterators; using base64_dec = bai::transform_width<bai::binary_from_base64<char*>, 8, 6>; std::vector<uint8_t> bitstream(base64_dec(encoded_bitstream.data()), base64_dec(encoded_bitstream.data() + encoded_bitstream.length())); // Remove null bytes that were formed from the padding const size_t pad_count = std::count(encoded_bitstream.begin(), encoded_bitstream.end(), '='); bitstream.erase(bitstream.end() - pad_count, bitstream.
Continue Reading
Many years ago, my flight instructor once described to me the “Swiss cheese model” in crash analysis and risk management. The thinking goes, you can think of each layer of defense against a bad outcome, such as a plane crash, as a slice of Swiss cheese. For a midair collision, maybe one slice is the pilot’s visual scan. A second slice is ATC’s radar screen. A third slice is a traffic display on a moving map.
Continue Reading
Today’s programming for maintainability snippet comes from a (wonderful) Rust library I was using the other day, built, which lets you embed build information into your binaries. Things like the compiler version, git HEAD commit, build time, etc. It’s a wonderful library and if you need that sort of thing I recommend it.
It’s a code generator, which spits out a built.rs file which you with your source. The only problem is, when you compile it, you get errors like:
Continue Reading
Today’s programming for maintainability snippet comes from some code I wrote a few weeks ago. In our codebase we have a (C++) class which looks something like:
template<typename T> class tAttribute { public: tAttribute& operator=(const tAttribute& other) { _coercedValue = other._coercedValue; _getCallbacks = other._getCallbacks; _setCallbacks = other._setCallbacks; _commitCallbacks = other._commitCallbacks; } tAttribute(const tAttribute& other) { *this = other; } ... private: T _coercedValue; std::vector<iCallback> _getCallbacks; std::vector<iCallback> _setCallbacks; std::vector<iCallback> _commitCallbacks; }; This code has been written many years ago, and nobody had touched it until recently.
Continue Reading
When car companies design a car, one of the major things they care about is the ability to maintain the car. The oil stick needs to be accessible and legible, the diagnostic codes need to be within reach of the driver (this is actually a legal thing too), the driver needs to be able to change a tire. Software engineering is not much different: Yes, we need the code we write to compile and not crash, but any professional programmer will tell you they spend at least 5x more time reading code than writing code, whether it be fixing bugs or writing new features in an existing codebase.
Continue Reading