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.

Sep 2024

Switch How We Do Padding

by in CodeSOD on

We've seen so many home-brew string padding functions. And yet, there are still new ways to do this wrong. An endless supply of them. Nate, for example sent us this one.

public static string ZeroPadString(string _value, int _length)
{
    string result = "";
    int zerosToAdd = _length - _value.length;

True Parseimony

by in CodeSOD on

We've seen this pattern many times here:

return (someCondition) ? true : false;

Secure Cryptography

by in CodeSOD on

Governments have a difficult relationship with cryptography. Certainly, they benefit from having secure, reliable and fast encryption. Arguably, their citizens also benefit- I would argue that being able to, say, handle online banking transactions securely is a net positive. But it creates a prisoner's dilemma: malicious individuals may conceal their activity behind encryption. From the perspective of a state authority, this is bad.

Thus, you get the regular calls for a cryptosystem which allows secure communications but also allows the state to snoop on those messages. Of course, if you intentionally weaken a cryptographic system so that some people can bypass it, you've created a system which anyone can bypass. You can't have secure encryption which also allows snooping, any more than you can have an even prime number larger than two.


A Managed Session

by in CodeSOD on

Some time ago, Roald started an internship on a ASP .Net application. It didn't take long to find some "special" code.

    public string RetrieveSessionString(string sessionName)
	{
		try
		{
			return Session[sessionName].ToString();
		}
		catch (NullReferenceException)
		{
			return null;
		}
	}

String in your Colon

by in CodeSOD on

Anders sends us an example which isn't precisely a WTF. It's just C handling C strings. Which, I guess, when I say it that way, is a WTF.

while(modPath != NULL) {
    p = strchr(modPath, ':');
    if(p != NULL) {
      *p++ = '\0';
    }
    dvModpathCreate(utSymCreate(modPath));
    modPath = p;
} while(modPath != NULL);

String Du Jour

by in CodeSOD on

It's not brought up frequently, but a "CodeSOD" is a "Code Sample of the Day". Martin brings us this function, entitled StringOfToday. It's in VB.Net, which, as we all know, has date formatting functions built in.

Public Function StringOfToday() As String
	Dim d As New DateTime
	d = Now

	Dim DayString As String
	If d.Day < 10 Then
		DayString = "0" & d.Day.ToString
	Else
		DayString = d.Day.ToString
	End If

	Dim MonthString As String
	If d.Month < 10 Then
		MonthString = "0" & d.Month.ToString
	Else
		MonthString = d.Month.ToString
	End If

	Dim YearString As String = d.Year.ToString
	Return YearString & MonthString & DayString
End Function


A Clever Base

by in CodeSOD on

Mark worked with the kind of programmer who understood the nuances and flexibility of PHP on a level like none other. This programmer also wanted to use all of those features.

This resulted in the Base class, from which all other classes descend.


Switching the Order

by in CodeSOD on

Let's say you had 6 different labels to display. There are many, many ways to solve this problem. Most of them are fine. Some of them are the dreaded loop-switch anti-pattern.

Julien unfortunately inherited one of those.


Enumerated Science

by in CodeSOD on

As frequently discussed here, there are scientists who end up writing a fair bit of code, but they're not software engineers. This means that the code frequently solves the problem in front of them, it often has issues.

Nancy works in a lab with a slew of data scientists, and the code she has to handle gets… problematic.


Unique Emails

by in CodeSOD on

Once upon a time, a company started a large React application. Like so many such projects, the requirements were vague and poorly understood, the management didn't have any sort of plan, and the developers didn't understand the framework. Once this inevitably started to go off the rails, all the developers were fired and then contractors were brought in, because that would be "cheaper".

Spoilers: it was not. So all the contractors were fired, and a new round of hires were brought in. This time, though, they'd correct their mistakes by strictly enforcing Scrum practices to be "agile".


Take a Line Break

by in CodeSOD on

The number of web applications which need to generate PDFs is too damn high, in my experience. But it's a requirement which continues to exist for a variety of reasons, so we just have to accept it.

Derek was accepting of it, at least until he found this attempt to add ten lines of space between paragraphs.


Strings go Asplodey

by in CodeSOD on

Anton has the joy of doing PHP work using one of the more popular e-commerce platforms.

Now, say what you will about PHP, its absolute mess of a root namespace has a function or class for just about everything. You want to split a string? We call that explode because that's way more radical than split. Want to join a string back together? We use implode, because that's the opposite of explode.