Article 6BVTN CodeSOD: An Operating Query

CodeSOD: An Operating Query

by
Remy Porter
from The Daily WTF on (#6BVTN)

Sami inherited some C# LINQ code. The actual behavior and purpose of this code is fairly simple. The way the code was written, however, well...

foreach (var operatingMode in ahu.CalculationData.OperatingModes) { operatingModesModel.OperatingModeNames.Add(operatingModeNumber, operatingMode.OperatingModeName); var innerOperatingModeNumber = operatingModeNumber; foreach (var property in from partData in operatingMode.PartDatas.Where(p => p.PartGuid == partGuid) let finalOperatingModeNumber = innerOperatingModeNumber from property in (from resultProperty in this.GetProperties(partData).Where(p => FilterAcceptNonSoundAndNonImageProperties(p, updateResult.For(partData)) && (propertyFilterFunction?.Invoke(partData, p) ?? true)).ToList() let measurementUnit = resultProperty.Type.GetPresentationMeasurementUnit(measurementUnits) let measurementUnitTranslationId = measurementUnit?.TextId select new OperatingModesModel.OperatingModePropertyModel(finalOperatingModeNumber, this.TranslationService.GetTranslator(this.Language.Code).Translate(resultProperty.Type.NameId), this.PrintoutUtil.GetValueString(resultProperty, measurementUnit, this.Language), string.IsNullOrEmpty(measurementUnitTranslationId) ? "-" : this.TranslationService.GetTranslator(this.Language.Code).Translate(measurementUnitTranslationId), resultProperty.Key)) select property) { operatingModesModel.OperatingModeProperties.Add(property); } operatingModeNumber++; }

Whitespace added for wrapping, as this is all one line in the submission.

Sami has this to say:

It blows my mind that someone would go this way to create such an unreadable nested mess with LINQ and leave it as is for the next developers to handle. It's no wonder the original writers of this code no longer desire to work on the project.

Now, we could try and unpick this code- it's a pretty straightforward series of nested loops and internal queries, but... why? I think we should learn a lesson from the original developers, and not put any more work into this than we have to.

Sami adds that this code is now obsolete, and replaced with something far simpler and easier to read.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments