560 blogs tracked4,950 posts indexed

#codesod

18 posts · 1 company · newest first

1

CodeSOD: A Tool for You (opens on the source site)

A junior dev on the team had a simple question: in Java, how do I correctly read user input from the CLI and act on it? Now, we can wonder about a junior dev not having such basic knowledge, but maybe they're new to Java specifically. Who knows. But Robert's team has a senior on it who "likes to mentor". This senior has strong opinions about how to write software, extreme confidence in those opinions, and has somehow been around the company long enough that he knows where the bodies are buried and no one will fire him. Here's his sample application for reading input. import…

codesodexcerpt only · body stays at the source
From the web
2

CodeSOD: A Random Article (opens on the source site)

Many years ago, DJ got his start doing odd web development jobs for a low hourly rate. He was only 19 when he got started, and what he lacked in experience he made up for in being cheap to hire. As I said, that was many years ago, and he has since learned a lot. But he still has some code from his very first "someone paid me for this" project. This particular function needed to pick a random bit of content from their database to display in the "right side" banner area. This is how DJ implemented it. // Create an array of all the ID's used $int = array(); $query = $sql->query("SELECT * FROM…

codesodexcerpt only · body stays at the source
From the web
3

CodeSOD: The Date and Time and Time (opens on the source site)

Today's anonymous submitter sends us some legacy Java code. The task is this: check what time it is. And fortunately, even in the older Java datetime libraries that were terrible, this was an easy task. You just needed to do something like new Date(), which constructs a date time object set to the current time. getTime() picks off just the time portion. Or, you could do this. Timestamp current = new Timestamp(new Date().getTime()); Date date = new Date(current.getTime()); This creates a date, picks the time off, populates a timestamp, gets the time from the timestamp, and constructs a new…

codesodexcerpt only · body stays at the source
From the web
4

CodeSOD: Very Valid Tokens (opens on the source site)

Today's anonymous submitter shares with us the function their company uses to validate authentication tokens. public bool IsTokenValid(string token) { //TODO return true; } This is running in production. So that's fun. The user they've authenticated as is tracked separately at the start of their session, which makes it at least marginally harder to pretend to be someone you aren't, but for authentication, any token will do. [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.

codesodexcerpt only · body stays at the source
From the web
5

CodeSOD: Quite a Distance from the Right Solution (opens on the source site)

Fritz's team needed to see if one point was within a certain distance from a center point. You or I would likely try and answer this question using a simple distance calculation, since that's the question we're trying to answer. But what if you didn't understand distances at all? Then you could write this little piece of genius. private bool IsWithin(Point point, int radius) { for (int x = -radius; x 1; x++) { for (int y = -radius; y 1; y++) { if ((PositionX == point.X + x) && (PositionY == point.Y + y)) return true; } } return false; } This limits us to integer point values, which itself is…

codesodexcerpt only · body stays at the source
From the web
6

CodeSOD: Historical Pads (opens on the source site)

Tim inherited a fairly antique Visual Basic application some time back. Yes, Visual Basic, not VB .Net. The application is old enough that we might consider it "vintage" or "historical"; it certainly dates from before the millennium. But it has its own unique approach to handling historical dates: mnYear% = CInt(Year(vntDate)) If mnYear% 1000 Then msYear$ = "0" & Trim$(CStr(mnYear%)) Else msYear$ = Trim$(CStr(mnYear%)) End If Insert obligatory complaints about Hungarian notation. What even is vnt. Now, one important thing about this program: it didn't have to handle dates back into the middle…

codesodexcerpt only · body stays at the source
From the web
7

CodeSOD: The John Cage Variable (opens on the source site)

David C sends us a true confession. For my job, I write C++ code as if it was a scripting language (long story) to produce programmatic animations for videos. Given that these are "write-and-run-once-and-never-look-at-it-again" programs, I tend to not try as hard to make my code good. But once I wrote this line of code, I had to take a step back and reevaluate my life choices. fade_out(scene, length4->range(0, 3), length4_3_3, length4[4], length4[5]->range(0, 3)); This is a natural consequence of passing parameters as arrays, it seems; instead of having meaningful named values in a struct or…

codesodexcerpt only · body stays at the source
From the web
8

CodeSOD: Vintage 2013 (opens on the source site)

Today we have more of a representative comment, from Watson. This comes from some GPL licensed code published by everybody's favorite evil empire, Oracle. /* * Copyright (c) 2011, 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013…

codesodexcerpt only · body stays at the source
From the web
9

CodeSOD: Extremely One Line (opens on the source site)

Autoformatting your code is a standard thing to do these days. And in those days past, if we're being honest. There's no excuse to not use some kind of autoformatter. Whether you configure your editor to do it or are a weirdo like me who runs a formatter from the CLI as a build step, you've got an easy way to format your code so it looks neat and readable. And some IDEs, like Visual Studio, are pretty insistent about doing this for you. Which makes today's code sample a bit more perplexing. This comes from an ancient ASP .Net application that Austin has the misfortune to work with: protected…

codesodexcerpt only · body stays at the source
From the web
10

CodeSOD: An Odd Sort (opens on the source site)

Let's say we wanted to query Active Directory and print out a report of all of our users, and their last logon time. That seems like a pretty normal task for a Powershell script. It'd probably be short and easy to read, at least if it were written by a normal person. Alice sends us one that wasn't. She's already done us a favor, as she writes: "Code cleaned up and indented for the whitespace-missing-impaired." ##################################### # lists accounts and selected attributes alphabetically ##################################### foreach( $letter in "a", "b", "c"......"z") {…

codesodexcerpt only · body stays at the source
From the web
11

CodeSOD: I Exist (opens on the source site)

In addition to using an ancient development environment, with terrible UX, Greta also has the misfortune of working in Pascal. Recently, she was diagnosing a bug. The program was reporting that files didn't exist when they definitely existed. She traced the problem down into the system library. Let's see if you can spot what's wrong: { Delphi / Kylix Cross-Platform Runtime Library } { System Utilities Unit } { } { Copyright (c) 1995-2001 Borland Softwrare Corporation } ... function FileAge(const FileName: string): Integer; {$IFDEF MSWINDOWS} var Handle: THandle; FindData: TWin32FindData;…

codesodexcerpt only · body stays at the source
From the web
12

CodeSOD: Asynchronous Directories (opens on the source site)

Eri has a mix of a "true confession" and a "wait, really?" today. The programming language Vala bills itself as a C# like language that compiles into something pretty close to C performance, designed specifically for writing code against Gnome and its associated libraries. One of the C#-isms in brings in is async/await type semantics. You can yield someAsyncFunction(), which returns control to the caller, allowing it to proceed until the yielded function returns an actual value. Because it has asynchronous functions, many library functions for handling I/O are already async. So you can…

codesodexcerpt only · body stays at the source
From the web
13

CodeSOD: Heating Up (opens on the source site)

A common option for retrofitting heating and cooling into older homes is a mini-split, frequently tied to a heat pump. They're (relatively) cheap to install, energy efficient, and can be added without substantial modifications to the home. They also, annoyingly, are mostly controlled via IR remotes, making them challenging to wire up to home automation or even a household thermostat. People have made solutions, and today's code comes from one of those solutions. Which, I want to stress, this code comes from an open source project for home automation, so it's not the code that's wrong, here.…

codesodexcerpt only · body stays at the source
From the web
14

CodeSOD: The Big Family (opens on the source site)

Some time ago, Charles shared with us some awful PHP, aka the most common sort. Today's code sample is maybe a little too big to sum up, but I'll let Charles take a crack at it. It's so bad that even analyzing and laughing at it feels impossible. But it’s so bad, I couldn’t not share it. I’m the only one handling all the IT-related tasks at my company, and I don’t have anyone here to vent or laugh about this kind of thing with. So, I figured, why not share it here? I’m hoping it’ll provide at least a little bit of catharsis or some dark humor. To make sure the confidentiality of the codebase…

codesodexcerpt only · body stays at the source
From the web
15

CodeSOD: Lock 'Em Dead (opens on the source site)

Kevin sends us an exception handler from C++. Let's see if we can spot what's going wrong: catch (Exception::Deadlock) { retry; } When we catch a deadlock happening, we retry. That's not a keyword in C++, and looking at how it's used, it has to be some kind of macro, and I suspect that the macro is hiding a goto underneath it. The real problem, though, is that we suspect we're in a deadlock situation. That means this thread is waiting on a resource held by another thread which is waiting for a resource held by this thread. Neither train may continue until the other has passed. So this retry…

codesodexcerpt only · body stays at the source
From the web
16

CodeSOD: Back to the Lab (opens on the source site)

Matlab is special. Scientists and researchers love it. Programmers hate it, and not just because it uses 1-based arrays. I've worked on a number of projects where the task was "take this Matlab code and convert it to C so we can run it on an embedded CPU". Somehow, in that process, I've avoided learning much about Matlab. Andre works on a team that uses Matlab to manage experimental scenarios. They wanted to do a simple task: generate a set of participant-specific images, store them in a database, and reference them later. Somewhere in the intersection of the database product they were using,…

codesodexcerpt only · body stays at the source
From the web
17

CodeSOD: Never Eating the Cookie (opens on the source site)

Maciej works as a freelancer, and that frequently means picking up old PHP code that nobody wants to support. One project had been lingering for ages with key features missing. Specifically, it was supposed to make HTTP requests to other services on an interval, and use that to populate its data. "The old dev tried, but never got it working." It was Maciej's turn to give it a shot. $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie ); curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie ); curl_setopt( $ch, CURLOPT_COOKIE, $cookie); // ... many other options set, of course not in…

codesodexcerpt only · body stays at the source
From the web
18

CodeSOD: Public Private Partnership (opens on the source site)

Eric O was trawling through an API for handling concurrency, and found this little mismatch between the comment and the definition: /// /// private Status, because while this object needs to be able to set the status, consumers should only be able to check it, lest everything break. /// public StatusType Status { get { return _status; } set { if (value != _status) { RaisePropertyChanged("Status"); } } } It's very important we make this property private, lest clients abuse it, and unleash dragons, chaos, and other potential horrors. Given that this happens inside of a concurrency API, I can…

codesodexcerpt only · body stays at the source
From the web
18 shown

Privacy choices

Reading never requires analytics. These choices last 90 days on this browser.

Essential sign-in and security storage always stays on. Read the privacy notice.