Adventures in Programming
This is a real code example I encountered in my job recently. We have two header files which declare the following
main.h
extern obj_type the_object;processor.h
extern obj_type the_object;And then within the cpp files we see the following
main.cpp
// main.cpp
the_object = {};
if(object.ready == 1)
{
process_object(the_object);
}processor.cpp
int bytes = read(fd, object.buffer, sizeof(object.buffer));
object.ready = 1;This is perfectly legal code, and it actually seems pretty straightforward. What happens in large software projects, though, is you don’t always see all the code at every stage of development.
For example, I didn’t develop this code and I didn’t realize main.h also used this extern. I only saw the code in the main.cpp file.
No comments to suggest this was being updated in processor.cpp. The object was created, checked if was ready, and then used. Very confusing. So, what drove this code to be like this? The original functions of the code were actually written in what I would call C+. Not really C, and not really C++…







