Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Dec 2022

Hanging By a Thread

by in CodeSOD on

Wojciech has to maintain a C++ service that, among other things, manages a large pool of TCP sockets. These sockets are, as one might expect, for asynchronous communication. They mostly sit idle for a long time, wait for data to arrive, then actually do work.

As one might expect, this is handled via threads. Spin up a thread for the sockets, have the thread sleep until data arrives on the socket- usually by selecting, wake up and process the data. This kind of approach keeps CPU usage down, ensures that the rest of the program remains responsive to other events, and is basically the default practice for this kind of problem. And that's exactly what the threads in Wojciech's program did.


Cole's Law of Authentication

by in CodeSOD on

Cabbages are an interesting vegetable, especially as they're one of those subtypes of brassica, that endlessly polymorphic family of plants which includes everything from cauliflower to Brussels sprouts and likely Audrey II.

Gabe was doing for work for a family of academic institutions, and ended up drawing the short straw and working on their scholarship application system. For an application that just needed to maintain a list of candidates and their grades, it was… difficult to support. But it also introduced Gabe to an entirely new use of cabbage: authorization.


Bad Parenting

by in CodeSOD on

Conditionals are hard. So are data structures. And today's anonymous submission gives us both of those.

var myParent = this.Parent;
bool parentRemoved = false;
if (myParent is SomeClass asSomeClass)
{
	myParent = myParent.Parent;
	myParent.Children.Remove(asSomeClass);
	parentRemoved = true;
}

if (!parentRemoved)
{
	myParent.Children.Remove(this);
}

Switching to Booleans

by in CodeSOD on

If you understand booleans in C, then you know that you don't really understand booleans in C. That's the case with Bodo's co-worker, anyway. They either don't understand booleans, or they don't understand their business requirements.

switch (getBool()) {
case false:
  /* do something... */
  break;
case true:
  /* do something else... */
  break;
default:
  break;
}

A Bit of Javascript

by in CodeSOD on

We've recently discussed how bit masks can confuse people. Argle's in a position to inherit some of that confused code.

In this case, Argle's company inherited some NodeJS code from a customer who was very upset with the previous development team they had hired. It was a mix of NodeJS with some custom hardware involved.


Holiday Sample Pack

by in CodeSOD on

Today, let's whip up a holiday sampler of some short snippets. We start with this anonymously supplied ternary from a C program, which certainly terned my head:

return (accounts == 1 ? 1 : accounts)

Very Productive Code

by in CodeSOD on

Today's anonymous submitter sends us a pile of code that exists to flummox us. It's consfuing, weird, and I don't understand it, even as I understand it. So we're going to walk through this packet of Python in steps, to see if we can understand what's going on.

First is this handy helper function, to turn a value into a number:


Enabled and Disabled

by in CodeSOD on

A large part of programming is about communicating intent. The code you write needs to communicate your intent for what the code is supposed to do, both to the computer itself but also to all of the humans which come after you. So much of what we mean when we talk about code style and "clear names" is really about making your intent comprehensible to anyone else who looks at the code.

When the intent of the code isn't clear, we often rely on comments to describe that intent.


An XML Parser

by in CodeSOD on

Since we were discussing XML earlier this week, it's a good time to take a peek at this submission from Mark.

Before we get into it, though, we need to talk briefly about some different XML parsing philosophies. XML documents are frequently large and deeply nested trees, and once loaded into memory, consume a lot of it. So there are two methods we might use to parse XML. We might parse and load the entire DOM- having all of that expensive processing and memory usage. The advantage is that we have the whole structure in memory, and can quickly and efficiently navigate through it. The other option is to read it as a stream, and process it one node at a time. This is both faster and a lot gentler on memory, but it means you have forward-only access to the contents.


Counting on Switching

by in CodeSOD on

Frequent contributor Argle found some code written at his current company a "long time ago", but unfortunately in a galaxy far too close to our own.

If rsRecordSetD1T3("ItemState")<>"N" then Select Case rsRecordSetD1T3("ItemState") Case "R", "L" tally=tally+1 Case "B" tally=tally+2 Case "N" tally=tally+0 Case Else tally=tally+0 End SelectEnd If

A Bit of a Misunderstanding

by in CodeSOD on

Mark B sends us another sample from a big-ball-o-wtf.

Let's take a look at two variables. One variable is named taskDetail. The other variable is named binaryTaskDetail. You might ask yourself: what are the differences between these two?


Shipping a Gallon of Soap

by in CodeSOD on

While JSON's strength is its (relative) simplicity, XML's strength was always its power. Combined with an XML schema, you can create strongly typed documents with arbitrary data-types. For example, it's easy to define, oh, I don't know, a date time field in a way that isn't stringly typed.

Of course, that's what also creates its complexity. XML is big, and bloated, and expensive to parse. Which brings us back to the old days of SOAP- the Simple Object Access Protocol, essentially an XML remote procedure call. It was powerful, but complex, with lots of interlocking standards. With its Web Service Description Language (WSDL), the service could even be self documenting, laying out all the objects and fields you could interact with.


Common Variables

by in CodeSOD on

It's important to be prepared- but not too prepared. A common trap developers fall into is "premature abstraction"- trying to solve the general case of a problem when you only need to solve a very specific case.

Frequent contributor Argle sends us some very old BASIC code. The task was to convert this ancient language into C#.