3
\$\begingroup\$

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?

asked Sep 14, 2012 at 13:01
\$\endgroup\$
0

3 Answers 3

5
\$\begingroup\$

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;
 }
answered Sep 14, 2012 at 13:14
\$\endgroup\$
1
  • \$\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\$ Commented Sep 14, 2012 at 20:11
4
\$\begingroup\$

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.

answered Sep 14, 2012 at 13:13
\$\endgroup\$
4
  • \$\begingroup\$ Using underscores is just one naming style and one that is not universally accepted. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Sep 18, 2012 at 0:29
  • \$\begingroup\$ @james yes, huskey's answer above has already pointed that out, but thanks for your input. \$\endgroup\$ Commented Sep 18, 2012 at 0:38
1
\$\begingroup\$

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)

answered Sep 17, 2012 at 13:35
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.