Monthly Archives: June 2013

Concepts

DRY

DRY, as in Don’t Repeat Yourself, is another fundamental principle in coding. And it’s all about avoiding repetition, or doing the same thing twice.

Rule of thumb: if you use the same code more than once in your project, you’re probably doing it wrong.

Code

Iterate through a hierarchical list of infinite child elements

Example that flattens a nested and, possibly infinite, hierarchical list of child elements.


    class Program
    {
        static void Main(string[] args)
        {
            // Create dummy categories - not especially pretty
            var categories = new List();
            categories.AddRange(new List()
                                    {
                                        new Category(){ Id = 1, Name = "Cat1", Children = new List()
                                                                                              {
                                                                                                  new Category(){ Id = 101, Name = "Subcat1"},
                                                                                                  new Category(){ Id = 102, Name = "Subcat2"},
                                                                                                  new Category(){ Id = 103, Name = "Subcat3WithChildren", Children = new List()
                                                                                                                                                                         {
                                                                                                                                                                             new Category() { Id = 10301, Name = "Subcat1-sub1" },
                                                                                                                                                                             new Category() { Id = 10302, Name = "Subcat1-sub2" }
                                                                                                                                                                        }
                                                                                                                }
                                                                                              }},
                                        new Category() { Id = 2, Name = "Cat2"}
                                    });

            // Flatten the nesteded list of *child* categories
            var flatCategories = new List();
            foreach (var rootCategory in categories)
                flatCategories.AddRange(IterateChildCategories(rootCategory));

            // Spit out the results
            foreach (var cat in flatCategories)
                Console.WriteLine(cat.Name);

            Console.ReadKey();

        }

        /// 
        /// Iterates through a list of child categories. 
        /// 
        /// 
        /// Note that this skips the parent categories, and focusses only on the children.
        /// 
        private static IEnumerable IterateChildCategories(Category parent)
        {
            if (parent.Children == null) yield break;

            foreach (var g in parent.Children)
            {
                yield return g;

                foreach (var sub in IterateChildCategories(g))
                    yield return sub;
            }
        }

        public class Category
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List Children { get; set; }

            public Category()
            {
                this.Children = new List();
            }
        }
    }

 

Concepts

YAGNI

Each and every piece of software will start with a design. Ranging from common sense, to a fully blown YAGNI, You ain’t gonna need it.

Rule of thumb: Don’t over-engineer things. Only write functionality when you need it right now. If you will not have a use right now for it, you will most likely not need it in the future. Even though you think else. Here are some typical statements that should ring some alarm bells when it comes to violating the YANGNI principle:

  • We may have a use for it in the future
  • This may come in handy sometime
  • I think the customer wants it this way

More often than not, when the YAGNI concept is violated it is presumably someone in the base forgot the KISS rule.