Article 3N4H7 CodeSOD: A Problematic Place

CodeSOD: A Problematic Place

by
Josh Cox
from The Daily WTF on (#3N4H7)

In programming, sometimes the ordering of your data matters. And sometimes the ordering doesn't matter and it can be completely random. And sometimes" well, El Dorko found a case where it apparently matters that it doesn't matter:

DirectoryInfo di = new DirectoryInfo(directory);FileInfo[] files = di.GetFiles();DirectoryInfo[] subdirs = di.GetDirectories();// shuffle subdirs to avoid problematic placesRandom rnd = new Random();for( int i = subdirs.Length - 1; i > 0; i-- ){ int n = rnd.Next( i + 1 ); DirectoryInfo tmp = subdirs[i]; subdirs[i] = subdirs[n]; subdirs[n] = tmp;}foreach (DirectoryInfo dir in subdirs){ // process files in directory}

This code does some processing on a list of directories. Apparently while coding this, the author found themself in a "problematic place". We all have our ways of avoiding problematic places, but this programmer decided the best way was to introduce some randomness into the equation. By randomizing the order of the list, they seem to have completely mitigated" well, it's not entirely clear what they've mitigated. And while their choice of shuffling algorithm is commendable, maybe next time they could leave us a comment elaborating on the problematic place they found themself in.

proget-icon.png [Advertisement] ProGet can centralize your organization's software applications and components to provide uniform access to developers and servers. Check it out! TheDailyWtf?d=yIl2AUoC8zApdnJpoDTYd8
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