Wednesday, May 22, 2013

When a language feature renders the naming convention / design pattern obsolete

I initially wrote this kind of code:

string i18n_EmployeeName = I18nUtility.GetGlobalResourceString("EmployeeName");
string i18n_CurrentRecipient = I18nUtility.GetGlobalResourceString("CurrentRecipient");
string i18n_SubmittedBy = I18nUtility.GetGlobalResourceString("SubmittedBy");
string i18n_Submitted = I18nUtility.GetGlobalResourceString("Submitted");
string i18n_EffectiveDate = I18nUtility.GetGlobalResourceString("EffectiveDate");
string i18n_Reason = I18nUtility.GetGlobalResourceString("Reason");
string i18n_OffCycleApprovalStatus = I18nUtility.GetGlobalResourceString("Status");
 
string dateFormat = "m/dd/yyyy";
 
.
.
.
 
byte[] ret = ExcelResultFromList.Create(
    mapAction: map =>
    {
       map.Include(x => x.EmployeeName).Label(i18n_EmployeeName);
       map.Include(x => x.CurrentRecipient).Label(i18n_CurrentRecipient);
       map.Include(x => x.SubmittedBy).Label(i18n_SubmittedBy);
       map.Include(x => x.Submitted).Label(i18n_Submitted).Format(dateFormat);
       map.Include(x => x.EffectiveDate).Label(i18n_EffectiveDate).Format(dateFormat);
       map.Include(x => x.ReasonCode).Label(i18n_Reason);
       map.Include(x => x.OffCycleApprovalStatus).Label(i18n_OffCycleApprovalStatus);
    },
    mapActionForSecond: map =>
    {
        map.Include(x => x.EmployeeName).Label(i18n_EmployeeName);
        map.Include(x => x.CurrentRecipient).Label(i18n_CurrentRecipient);
        map.Include(x => x.SubmittedBy).Label(i18n_SubmittedBy);
        map.Include(x => x.Submitted).Label(i18n_Submitted).Format(dateFormat);
        map.Include(x => x.EffectiveDate).Label(i18n_EffectiveDate).Format(dateFormat);
        map.Include(x => x.ReasonCode).Label(i18n_Reason);
        map.Include(x => x.OffCycleApprovalStatus).Label(i18n_OffCycleApprovalStatus);
    }
)

I don't want to create a separate class just to emphasize the related variables (e.g. labels), it's too ceremonial. I put prefix on those label variables so I can segregate them from business variables(i.e. variables use for computation, flow, etc), achieving separation of concerns via naming convention. Whether prefixing things is desirable or not is entirely another matter; but certainly, separation of concerns must be addressed.


And then I realize, I can do adhoc-y structure with anonymous type, and voila! naming convention is not necessary anymore to achieve separation of concerns. And suddenly, naming convention feels so WET


var i18n = new
               {
                   EmployeeName = I18nUtility.GetGlobalResourceString("EmployeeName"),
                   CurrentRecipient = I18nUtility.GetGlobalResourceString("CurrentRecipient"),
                   SubmittedBy = I18nUtility.GetGlobalResourceString("SubmittedBy"),
                   Submitted = I18nUtility.GetGlobalResourceString("Submitted"),
                   EffectiveDate = I18nUtility.GetGlobalResourceString("EffectiveDate"),
                   Reason = I18nUtility.GetGlobalResourceString("Reason"),
                   OffcycleApprovalStatus = I18nUtility.GetGlobalResourceString("Status")
               };
 
string dateFormat = "m/dd/yyyy";
 
.
.
.
 
byte[] ret = ExcelResultFromList.Create(
    mapAction: map =>
    {
       map.Include(x => x.EmployeeName).Label(i18n.EmployeeName);
       map.Include(x => x.CurrentRecipient).Label(i18n.CurrentRecipient);
       map.Include(x => x.SubmittedBy).Label(i18n.SubmittedBy);
       map.Include(x => x.Submitted).Label(i18n_Submitted).Format(dateFormat);
       map.Include(x => x.EffectiveDate).Label(i18n.EffectiveDate).Format(dateFormat);
       map.Include(x => x.ReasonCode).Label(i18n.Reason);
       map.Include(x => x.OffCycleApprovalStatus).Label(i18n.OffCycleApprovalStatus);
    },
    mapActionForSecond: map =>
    {
        map.Include(x => x.EmployeeName).Label(i18n.EmployeeName);
        map.Include(x => x.CurrentRecipient).Label(i18n.CurrentRecipient);
        map.Include(x => x.SubmittedBy).Label(i18n.SubmittedBy);
        map.Include(x => x.Submitted).Label(i18n.Submitted).Format(dateFormat);
        map.Include(x => x.EffectiveDate).Label(i18n.EffectiveDate).Format(dateFormat);
        map.Include(x => x.ReasonCode).Label(i18n.Reason);
        map.Include(x => x.OffCycleApprovalStatus).Label(i18n.OffCycleApprovalStatus);
    }
)


Happy Computing! ツ

No comments:

Post a Comment