Article 8JDP CodeSOD: The Forest of Trees

CodeSOD: The Forest of Trees

by
Remy Porter
from The Daily WTF on (#8JDP)

Sally's co-worker wanted to turn some data into HTML. It would flow from his application into client-side JavaScript which would build the DOM. He decided that it made sense to use a tree to represent the data as it's translated.

The C# declaration of his tree looked something like this:

Dictionary<int, Dictionary<CatalogLoader_Helper.CatalogNode, List<CatalogLoader_Helper.CatalogNode>>> treeOBJMODEL;

Isn't it fantastic how Generics can make your code more readable and type-safe? I mean, that's not what's happening in this example, but"

To traverse the tree, he used a terse, readable block like this:

Dictionary<int, Dictionary<CatalogLoader_Helper.CatalogNode, List<CatalogLoader_Helper.CatalogNode>>> treeOBJMODEL = (Dictionary<int, Dictionary<CatalogLoader_Helper.CatalogNode, List<CatalogLoader_Helper.CatalogNode>>>)Application["ProductTypesTree"];foreach (int level in treeOBJMODEL.Keys){ Dictionary<CatalogLoader_Helper.CatalogNode, List<CatalogLoader_Helper.CatalogNode>> levelNodes = treeOBJMODEL[level]; if (level == 1) { foreach (List<CatalogLoader_Helper.CatalogNode> rootNodeL in levelNodes.Values) { foreach (CatalogLoader_Helper.CatalogNode rootNode in rootNodeL) { if(this.IsLeaf(rootNode, level)) { // ... } // ... } } } else { foreach (CatalogLoader_Helper.CatalogNode parentNode in levelNodes.Keys) { List<CatalogLoader_Helper.CatalogNode> childSiblingNodesOf1AndTheSameParent = levelNodes[parentNode]; foreach (CatalogLoader_Helper.CatalogNode leafNodeToAdd in childSiblingNodesOf1AndTheSameParent) { if(this.IsLeaf(leafNodeToAdd, level)) { // ... } // ... } } }}

Thanks to the super-powerful (ab)use of Generics, Sally's co-worker was able to write even more streamlined code for normally challenging functions, like IsLeaf:

private bool IsLeaf(CatalogLoader_Helper.CatalogNode node, int nodeLevel){ //It enters the collection and checks: //If the nodeLevel is the highest (we are on the last level) //then it immediately returns true: it is definitely a leaf; //Otherwise: //It procures itself the level mapping for the next level //and checks if it finds itself in its keys //if yes, then it immediately returns false and exits; //otherwise it returns true. int nextLevel = nodeLevel + 1; if (!treeOBJMODEL.ContainsKey(nextLevel)) { return true; } else { string thisNodeNumRef = node.numRef; Dictionary<CatalogLoader_Helper.CatalogNode, List<CatalogLoader_Helper.CatalogNode>> levelMapping = treeOBJMODEL[nextLevel]; foreach (CatalogLoader_Helper.CatalogNode parentsForNextLevel in levelMapping.Keys) { if (parentsForNextLevel.numRef.Equals(thisNodeNumRef)) return false; } return true; }}
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=yIl2AUoC8zA73w0pspXAG0
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