Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do class properties need comments

Tags:

c#

StyleCop uses the Gets or Sets ... notation which it enforces with rule SA1623

My question is why, as from what I can see the vast majority of the time it is self explanatory and requires lots of repetition to have these?

/// <summary>
/// Gets or sets the name of the customer
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets whether the record is archived
/// </summary>
public string IsArchive{ get; set; } 

It can also make it overly verbose, when you have just 10 lines, each one declaring a property, you end up with 30+ lines to have a summary comment on each!

like image 404
Coops Avatar asked Dec 12 '22 07:12

Coops


1 Answers

StyleCop rules are not absolutes but instead guidelines. In general this is a good rule but in some cases yes the properties are simple enough that documentation is probably overkill. For example

class Student { 
  public string FirstName { get; set; } 
  public string LastName { get; set; }
}

A comment on these properties really adds no value as are pretty self descriptive.

I'm a big fan of using StyleCop in projects. But that doesn't mean you have to take every single rule it comes with. Disable the ones that don't make sense for your project and embrace the ones that do

like image 153
JaredPar Avatar answered Dec 26 '22 16:12

JaredPar