CodeSOD: Sorting Out a Late Night
Karl's trials of crunch (previously) didn't end with a badly written brain-fart. After too many consecutive late nights, Karl noticed that their grid layout was wrong.
It did this:
Alpha | Beta |
Delta | Gamma |
Karl expected the alphabetic layout to be more like:
Alpha | Delta |
Beta | Gamma |
Specifically, this was the layout for a few checkboxes controlling how their system would autogenerate filenames. The field layout was not considered a problem by anyone, other than Karl, and certainly wasn't the desperate issue that was driving the crunch.
After a few hours poking at this and having no real success, Karl did the natural thing: he roped in one of his co-workers. The two of them stared at the code, poked, prodded, cursed, made stupid syntax errors, and drank too much coffee.
By the time the sun rose, they had solved the problem, which again, wasn't a problem anyone else had identified, with this masterpiece of .NET MVC:
HashSet<string> fields = Util.GetAllFields(namingConvention, out nameConv);displayNames.Add(new KeyValuePair<string, bool>("ArchiveImport", fields.Contains("ArchiveImport")));foreach (var property in typeof(GuiImport).GetProperties() .Where(g => g.PropertyType != typeof(bool) && g.PropertyType != typeof(bool?)).OrderBy(g => g.Name)){ displayNames.Add(new KeyValuePair<string, bool>(property.Name.Trim(), fields.Contains(property.Name)));}//SNIP//NEVER CHANGE THIS#region DANGERViewBag.Lines = (int)Math.Ceiling((decimal)displayNames.Count() / 4);int startAtColumn = (displayNames.Count() % 4) + 1;for (int i = startAtColumn; i > 1 && i <= 4; i++){ if (i == 4) displayNames.Add(new KeyValuePair<string, bool>(null, false)); else displayNames.Insert(i * ViewBag.Lines - 1, new KeyValuePair<string, bool>(null, false));}ViewBag.PropertyNames = displayNames;#endregion
When you see GetProperties, you know you're looking at .NET's reflection API, and that means there's a pretty high probability someone is doing things they shouldn't be. Beyond that, we've got a weird fumbling attempt to sort in a non-conventional order.
Karl also promises: "ViewBag.PropertyNames is iterated through on the cshtml page to populate it in a wildly awful mix of logic and display which MVC is supposed to help prevent," but didn't supply that code.
But the clearest product of any late night coding session while coasting on the edge of burnout is:
//NEVER CHANGE THIS#region DANGER
The real danger, of course, is the kind of working environment that leads to these kinds of late hours and their attendant mistakes.
[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!