Naming issue: It is hard to keep it consistent

Discussion in 'ASP .Net' started by Author #1, Aug 11, 2009.

  1. Author #1

    Author #1 Guest

    Maybe naming isn't a big deal for many of you, but this has been
    bothering me for a while. I like to be consistent in naming. But,
    sometimes I think it is really hard to be consistent.

    I use camelCase for variables like so:

    private string firstName;
    private string lastName;

    I use PascalCase for class names and method names:

    public class Employee
    {
    public DataTable GetEmployeeDetails(int employeeId)
    {
    }
    }

    This is fine and consistent, but I see that in the examples of many
    articles, they use Hungarian notation for IDs of server controls such
    as buttons (e.g. btnSave, btnCancel), and the click event handlers
    will then have names like btnSave_Click, btnCancel_Click.

    I am following their examples and doing this, too. But, then these
    event handler names conflict with my intention of using PascalCase for
    method names. If I substitute btnSave_Click with SaveEmployee, then
    it does not seem to be as self-explaining as btnSave_Click because
    btnSave_Click tells you it is a click event handler, whereas
    SaveEmployee does not.

    Maybe I should use camelCase for method names just like in java, but
    MS coding standards recommend using PascalCase.

    See how confused I am? Any consistent coders? Thank you.
     
    Author #1, Aug 11, 2009
    #1
    1. Advertisements

  2. Author #1

    Stan Guest

    How about calling the button control btnSaveEmployee and the event
    handler btnSaveEmployee_click ?
     
    Stan, Aug 11, 2009
    #2
    1. Advertisements

  3. Author #1

    Author #1 Guest

    Thank you. Either I didn't make my points clear, or you did not
    understand what I was talking about. :)
     
    Author #1, Aug 11, 2009
    #3
  4. Author #1

    Author #1 Guest

    Let me try to make it clear:

    I'd like to *avoid* Hungarian notation (things like btnSave,
    btnCancel, iCount, strName) , since a lot of articles recommend
    against it. But if we avoid Hungarian notation, what's the best
    practice in naming server controls and their corresponding event
    handlers?
     
    Author #1, Aug 11, 2009
    #4
  5. Author #1

    Stan Guest

    I don't think there is any. If you don't like Hungarian notation then
    how else are you going to incorporate object type information into the
    identifier unless you use lengthy names such as SaveEmployeeButton and
    SaveEmployeeButton_Click?
     
    Stan, Aug 12, 2009
    #5
  6. On Tue, 11 Aug 2009 12:09:54 -0700 (PDT), "Author #1"

    -snip-
    -snip-

    What makes a method called btnSave_Click an event handler? It's not
    the name alone but the method's signature which clarifies a method's
    purpose. When a method's signature contains an argument of type object
    and an argument derived from type EventArgs, the method's name
    doesn't. need to explicitly shout "event handler".

    With that rant out of the way ;) I would suggest keeping both names
    and do some refactoring. What you're doing is tightly coupling the UI
    and BL. That is not the best practice. It would be better to have the
    UI's btnSave_Click event handler invoke the SaveEmployee method in a
    business layer.

    regards
    A.G.
     
    Registered User, Aug 12, 2009
    #6
  7. Author #1

    Author #1 Guest

    Thank you very much for you input, but that's way too off-topic.

    I guess I didn't make my points clear. I am talking about *naming
    practice*. I'd like to hear the best, most consistent naming
    practice, that's it.
     
    Author #1, Aug 12, 2009
    #7
  8. Author #1

    Author #1 Guest

    Thank you. I agree with you. It seems that it is really hard to stay
    with only *one* naming convention throughout a project.
     
    Author #1, Aug 12, 2009
    #8
  9. It's a matter of personal preference. The keys are consistency,
    legibility and simplicity. I view pseudo-hungarian notation as the
    antithesis of simplicity. If a control is always referred to as the
    save button why not name the control saveButton instead of btnSave?

    With event handlers there may not always be a one-to-one relationship
    with a single control. A save button and a save menu item can use the
    same event handler. The name of the event handler should describe the
    functionality of the method rather than the control which fired the
    event.

    Avoid terse and/or arcane conventions, little naming verbosity is not
    a bad thing.

    regards
    A.G.
     
    Registered User, Aug 12, 2009
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.