Beautiful Code Makes Life Beautiful

This is the Part 1 of my post series about good code.

Part 2 : Why I Always Choose Decorator Pattern

There are two kinds of software developers. Those who write crappy code and those who write beautiful code. I'm sure if you are a fan of beautiful code, you'd agree with me. Those who don't agree and those who think I'm stupid, read on.

Image credits: http://xpconference.in/
Everyone has to start somewhere, right? Us, developers, when we start creating our first program, most of us don't really know what design patterns are or don't understand them or why we should be using them. We just jump right into writing code. And then we hit run and the code works. So fucking great!

Atleast, that's what I thought before I start making a living out of my code. Organizations need readable, maintainable, bug free, well commented and well understood code. Imagine we were tasked with adding a new functionality to assembly code. I don't even know assembly because that shit is ugly. Sure, we can talk to the machines at the lowest level, but why bother? This isn't 1960's anymore.

I've read somewhere that software engineers strive for well crafted maintainable code while developers just want to get something done faster. Developers don't really have to care about how maintainable their code is. Engineers does. Because, If you build a bridge without a proper plan, bride is doomed to collapse.

If you used to be a developer ages ago and now you are a engineer or an architect, then have you ever came across your ages old code and had this reaction?

What the fuck is this shit? I can't believe I wrote this shit. yuk! 
I did, my friends did. Probably a million other developers did too.

Take a look at this code. Those who think this is beautiful and this code will make your life beautiful. Thanks for visiting my blog. You don't have to read on. Instead, check out my personal site.

(Following code is missing a lot of classes and methods. This was something I wrote years ago to search hotels and filter them based on user selected criteria -facets- )

public class HotleSearchService
    {
        private Log _log = new Log();
        public HotelResult Search(HotelQuery query)
        {
            query.Filters.Add(new Filter
            {
                Name = "Price",
                Expression = res => res.Price.Between(query.SelectedFilters.Price)
            });
            query.Filters.Add(new Filter
            {
                Name = "Check In Date",
                Expression = res => res.CheckInDate.Between(query.SelectedFilters.Date)
            });
            query.Filters.Add(new Filter
            {
                Name = "Check Out Date",
                Expression = res => res.CheckOutDate.Between(query.SelectedFilters.Date)
            });
            query.Filters.Add(new Filter
            {
                Name = "Facilities",
                Expression = res => res.Facilities.Contains(query.SelectedFilters.Facilities)
            });
            query.Filters.Add(new Filter
            {
                Name = "Property Types",
                Expression = res => res.PropertyTypes.Contains(query.SelectedFilters.PropertyTypes)
            }); 
            query.Filters.Add(new Filter
            {
                Name = "Rating",
                Expression = res => res.Rating.Between(query.SelectedFilters.Rating)
            });
      

            _log.Info("New hotel search request" + query.ToJson());
            var st = new Stopwatch();
            st.Start();
            var service = new HotelClient();
            var results = service.Search(ConvertRequest(query)); 
            st.Stop();
            _log.Info("hotel search took : " + st.ElapsedMilliseconds +  "ms");
            _log.Info("applying filters");
            st.Start();

            foreach (var filter in query.Filters)
            {
                results = filter.Apply(results);
            }

            st.Stop();
            _log.Info("hotel search took : " + st.ElapsedMilliseconds + "ms");

            var x = Convert(results);
            x.DoCalculations();
            x.LoadCached();
            return x;
        }
    }

Above code is messy. Does too much in one class. Which clearly violates Single Responsibility principle of OOP. I have called it search service, so it should only do searching. But, it also defines filters, do logging, measure execution time, filters results, converts results. The complete class will be thousands of lines of code. Maintaining this shit is an obvious nightmare. I know what it does, I know where to change. but what if a new developer comes and she was tasked with adding a new filter or add a completely new functionality like caching frequent searches and relevant results? We are looking at a lot of sleepless nights and depression here. Of course, I can do it in a day. But maintainable software should be maintainable by others too. Not by the one who originally wrote it.

From my next post, I will discuss how to make this code more readable and maintainable. I will follow SOLID principles, best practices and patterns as best as I can and will try to make this code beautiful. Making our lives beautiful along with it..

Part 2 : Why I Always Choose Decorator Pattern

Popular posts from this blog

Print a receipt using a Thermal Printer with C#.NET

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Complex Master-Detail Form using Knockout.js and ASP.NET MVC