Regional Conflicts

We have all come to love our IDE features and take them for granted nowadays. Features like intellisense, code-complete, refactoring and code organization and of course the use of Regions.

The #region and #endregion keywords basically provide the ability to hide code and collapse it into a short word or phrase provided at the beginning of the block with the IDE showing a plus sign to expand and collapse the region.

Eg: #region Public Methods

      #endregion

While regions are handy and have become popular, I use them myself occasionally. I don’t particularly fancy them, neither do they improve my code’s readability by much. I believe in keeping my code concise, using descriptive names for methods, classes, variables, etc. and keeping my classes short and easy to read. But sometimes a region for collapsing those using statements and grouping similar methods like constructors, private variables and properties, disposing, etc comes handy.

As regions help you “hide” your code some developers like to abuse it to hide shameful code.

Let me share with you an example I saw recently. Here the developer is mapping data from his objects to an external system based on keys. The code for this method went of for a few hundred lines of code having a mapping for every field the external system needed.

public string GetMapDataValue(string key, Account accountObject) 
{

switch (key)
{
#region Account Data

case "AccountFirstName":
case "FirstName":
return accountObject.FirstName;

case "AccountLastName":
return accountObject.LastName;

#endregion

#region Address Data

case "AddressLine1":
return accountObject.PrimaryAddress.Line1;

case "AddressLine2":
return accountObject.PrimaryAddress.Line2;

case "AddressCity":
return accountObject.PrimaryAddress.City;

case "AddressState":
return accountObject.PrimaryAddress.State;

case "AddressZip":
return accountObject.PrimaryAddress.Zip;

#endregion

……

}

}


After collapsing the regions, the code looks like this:



image



This clearly shows that the developer is trying to write separate methods for mapping different pieces of data but instead choose to shove all the code in a switch case statement broken up into different regions for each specific piece of information. It may look organized but in reality it’s a poorly designed function that has been “regionized” to look readable. A classic case of hiding your dirty laundry in the closet.



Another strange use of regions I have seen over the years since regions have been around is the use of regions as grouping methods as if they were writing classes. This is a classic example from my procedural programmer friends who really have not gotten on the Object Oriented boat that left the shore a long time ago!



Here is an example:



public class DataAccess
{
#region Account Data

public InsertAccount(Account account)
{
}

public DeleteAccount(long accountId)
{
...
}

public UpdateAccount(Account account)
{
...
}

#endregion

#region Order Data

public InsertOrder(Order order)
{
...
}

public DeleteOrder(long orderId)
{
...
}

public UpdateOrder(Order order)
{
...
}

#endregion

... you get the point ...
}


A class like this went on for the few hundred objects in the system that needed to be persisted and was no less than 10,000 lines of code! The developer boasted the code to his colleagues and marveled at it. I remember working on this code file. It was so big that even the intellisense started to choke up. When I typed ‘this’ and hit the period key it caused the machine to run on overdrive for a few seconds before the list showed up! Now that is some serious regioning!



I don't want to give directions on how to use regions but I will say that if you design your code well and make sure your program functionality is broken down in to understandable, re-usable and maintainable classes you will find that your code does not need to be regionalized much. Short concise classes broken down into short descriptive methods will take you far in keeping your code organized and readable. By default the IDE makes methods, classes, and namespaces into regions and that should be enough for the majority of the code you will write!

3 comments:

  1. Nếu bạn đang cần chuyển hàng từ thái lan về việt nam hãy liên hệ với chúng tôi. Chúng tôi sẽ giúp bạn mua hàng thái lan online một cách dễ dàng. Chỉ cần đưa thông tin sản phẩm bạn cần cho chúng tôi, chúng tôi sẽ mua và vận chuyển về Việt Nam. Khi chỉ là mỗi Thái Lan chúng tôi còn có các dịch vụ khác như: dịch vụ chuyển hàng từ mỹ về việt nam, dịch vụ vận chuyển hàng đi campuchia, dịch vụ đặt hàng quảng châu giá rẻ, .... Nếu bạn cần mua hàng trung quốc giá rẻ hay cần mua hàng trên taobao hãy liên hệ và sử dụng
    dịch vụ order hàng trung quốc của chúng tôi. Chúng tôi sẽ giúp bạn
    chuyển hàng trung quốc về việt nam một cách nhanh chóng và tiện lợi nhất.

    ReplyDelete
  2. I am really glad to read it and being able to share my thoughts on it. I want to use this opportunity to say that I really love this blog. It is an amazing resource of information for my working. Thank you so much. Coming my site profile picture to watch more wonderful videos on instagram.

    ReplyDelete
  3. It's an fascinating function not only of blackjack 코인카지노 but of all on line casino games that the house—or casino— at all times has the benefit. It's another fascinating function of blackjack that the supplier's bust potential is normally lower than the gamers'. But even for the casual participant who performs a reasonably good recreation, the on line casino odds are much less, making Blackjack one of the most engaging on line casino games for the player. While the recognition of Blackjack dates from World War I, its roots go back to the 1760s in France, the place it is known as Vingt-et-Un . Today, Blackjack is the one card recreation that may be} present in every American on line casino. As a well-liked residence recreation, it is performed with slightly totally different guidelines.

    ReplyDelete