Add semantic through your code, not through commenting
2 min read
2 min read
if (someConditionHolds)This code looks quite complex, you see the reading of the culture from the current thread, then there is some other code involved which is creating a new culture object based on the previously used culture etc...Basically a lot of complex stuff which I don't want to bother about if I was not the developer who wrote this piece. So one would read the comment above and understand, ok, these lines are just formatting the date according to the current culture and assigning it to the textbox.
{
//formats the date according to the given culture
DateTime date;
if (DateTime.TryParse(textBoxDate.Text, new CultureInfo(this.PreviousCulture), DateTimeStyles.AssumeLocal, out date))
{
textBoxDate.Text = date.ToString(this.DisplayFormat, Thread.CurrentThread.CurrentUICulture);
}
}
if (someConditionHolds)
{
textBoxDate.Text = GetCultureFormattedDateString(textBoxDate.Text);
}
private string GetCultureFormattedDateString(string toFormat)Now this really looks simple, doesn't it? When I read over the code, I come across the method call and I immediately catch what is going on by reading the method name. Then, if I'm interested in this special thing, I can go and read the complex implementation, but otherwise I just don't care and go on.
{
string result = toFormat;
DateTime date;
if (DateTime.TryParse(textBoxDate.Text, new CultureInfo(this.PreviousCulture), DateTimeStyles.AssumeLocal, out date))
{
result = date.ToString(this.DisplayFormat, Thread.CurrentThread.CurrentUICulture);
}
return result;
}