On Code Comments
dJeff Atwood had a post about code comments, which I am completely agree with. One of the comments to the post caught my eye, talking about the assumption made when commenting:
I had posted before about this topic, but I think that this is an interesting take on the topic. When I comment, there are some assumptions that I make about the person reading the code:
- That s/he knows the language, or is capable of learning it, for instnace, I tend to use this quite a bit:
public string CacheId
{
get
{
return ( ViewState["CacheId"] ?? (ViewState["CacheId"] = Guid.NewGuid()) ).ToString();
}
} - That s/he have at least a rudimetry understanding of the domain and the model. This is much harder than merely understanding the language/framework, by the way. If we will return to the HR model for a second, here is a piece of code that give the employee a 10% raise:
SalarySnapShot raisedSalary = employee.At(startDate).Salary.Copy(startDate, endDate);
raiseSalary.Amount *= 1.1m;
emloyee.Salaries.AddOccurance(raiseSalary); - That they understand the technology:
With.Transaction(delegate
{
Employee employee = Repository<Employee>.Find(empId);
SalarySnapShot raisedSalary = employee.At(startDate).Salary.Copy(startDate, endDate);
raiseSalary.Amount *= 1.1m;
emloyee.Salaries.AddOccurance(raiseSalary);
});
So, what do I comment?
I comment why I am not doing things when the situation is unique:
end = end.AddDays(1);// Need to make sure that the end date is after the start date, even though we are only using the time portion.
When there is a hidden catch:
Bug fixes:
employee.AddNote(newNote);
//We have leaking this pointer here, because the AddNote set newNote.Employee = this.
//this can cause problems when we group things by employee, so we do this explictly, to set to the proxy.
newNote.Employee = employee;
Comments
Comment preview