I have the following:
public class ContentsController : BaseController {
private IContentService _content;
private IPageService pageService;
private IReferenceService _reference;
private ISequenceService seqService;
public ContentsController(
IContentService contentService,
IPageService pageService,
IReferenceService referenceService,
ISequenceService sequenceService) {
this._content = contentService;
this.pageService = pageService;
this._reference = referenceService;
this.seqService = sequenceService;
}
Note that it's completely mixed up with different naming standards.
Is there anything recommended for the naming of services that are used by my controller?
3 Answers 3
I don't think it matters as long as you are consistent. I personally prefer: _{servicename}Service format. ex. _referenceService
public class ContentsController : BaseController {
private IContentService _contentService;
private IPageService _pageService;
private IReferenceService _referenceService;
private ISequenceService _seqService;
public ContentsController(
IContentService contentService,
IPageService pageService,
IReferenceService referenceService,
ISequenceService sequenceService) {
this._contentService = contentService;
this._pageService = pageService;
this._referenceService = referenceService;
this._seqService = sequenceService;
}
-
\$\begingroup\$ yes, I use this same standard for naming. I would also remove the this. reference as another standard (albiet off topic from OP question) \$\endgroup\$dreza– dreza2012年09月14日 20:11:37 +00:00Commented Sep 14, 2012 at 20:11
IMHO, member variables should begin with an underscore and be descriptive
This is fine
private IContentService _content;
This is better
private IContentService _contentService;
BTW, using 'this.
' in your constructor is redundant.
-
\$\begingroup\$ Using underscores is just one naming style and one that is not universally accepted. \$\endgroup\$svick– svick2012年09月14日 13:42:54 +00:00Commented Sep 14, 2012 at 13:42
-
1\$\begingroup\$ @svick You are correct, edited my answer. I do like this convention best as it visually differentiates a member variable, and I can have the exact same name for my parameters (without the underscore), making the code very readable. Again, just MHO. \$\endgroup\$Forty-Two– Forty-Two2012年09月14日 13:59:57 +00:00Commented Sep 14, 2012 at 13:59
-
\$\begingroup\$ @Forty-Two underscores or no underscores,
this.
or none these are a matter of style. One that makes minimal difference. What is more important is that there is a consistent style. \$\endgroup\$James Khoury– James Khoury2012年09月18日 00:29:54 +00:00Commented Sep 18, 2012 at 0:29 -
\$\begingroup\$ @james yes, huskey's answer above has already pointed that out, but thanks for your input. \$\endgroup\$Forty-Two– Forty-Two2012年09月18日 00:38:37 +00:00Commented Sep 18, 2012 at 0:38
I'd recommend to use Microsoft's Guidelines for Names, so you can check your code with tools like FxCop.
Here are guidelines for your case:
Do not use underscores, hyphens, or any other nonalphanumeric characters (
contentService
instead of_contentService
)Spell out all words used in a field name. Use abbreviations only if developers generally understand them. Do not use uppercase letters for field names (
sequenceService
instead of_seqService
)Do use camel casing in parameter names (you did)