Article 36MQ CodeSOD: Variables Everywhere, But Not a Stop to Think

CodeSOD: Variables Everywhere, But Not a Stop to Think

by
snoofle
from The Daily WTF on (#36MQ)

SharePoint. What can you say about it? Among other things, it's designed to help you manage and present content. It's supposed to make things easy for you. If you want some customization, just write some code to do whatever and configure it to run at the appropriate time for the appropriate page(s).

Of course, this leaves open the possibility that folks who may be something less than experts might author said customizations.

Paul is responsible for numerous customizations written by developers long gone from their project. One particular customization was to perform some clean up. At first, it was run once per day. Over time, that grew to running 8+ times per day on each of several servers.

One day, the customization stopped working. Naturally, there was no record of it anywhere in source control (thank you predecessor coders).

Having no alternative, the code was decompiled into the following loveliness:

private static void Main(string[] args) { using (SPSite site = new SPSite("<SITE ANONYMOUS>")) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["Calendar"]; site.AllowUnsafeUpdates = true; try { foreach (SPListItem item in list.Items) { DateTime now = DateTime.Now; DateTime time2 = now.AddDays(30.0); DateTime time3 = (DateTime) item["End Time"]; DateTime time4 = (DateTime) item["Start Time"]; DateTime time5 = time3; DateTime time6 = time4; DateTime time7 = now; DateTime time8 = time2; if (time3 >= now) { if ((time5 >= time7) && (time6 <= time8)) { item["DisplayThis"] = "YES"; item.Update(); } else { item["DisplayThis"] = "NO"; item.Update(); } } else if (time3 < now) { item["DisplayThis"] = "NO"; item.Update(); } } } catch (Exception) { } finally { site.AllowUnsafeUpdates = false; } } }}

There seem to be three different ways to refer to DateTime.Now, and eight variables that could easily be knocked down to...none.

inedo50.png[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too! TheDailyWtf?d=yIl2AUoC8zAFXbzwbOmWDE
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