Naming issue: It is hard to keep it consistent

  • Thread starter Thread starter Author #1
  • Start date Start date
A

Author #1

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.
 
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.

How about calling the button control btnSaveEmployee and the event
handler btnSaveEmployee_click ?
 
How about calling the button control btnSaveEmployee and the event
handler btnSaveEmployee_click ?

Thank you. Either I didn't make my points clear, or you did not
understand what I was talking about. :-)
 
How about calling the button control btnSaveEmployee and the event
handler btnSaveEmployee_click ?

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?
 
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?

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?
 
On Tue, 11 Aug 2009 12:09:54 -0700 (PDT), "Author #1"

-snip-
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.
-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.
 
On Tue, 11 Aug 2009 12:09:54 -0700 (PDT), "Author #1"


-snip->  If I substitute btnSave_Click with SaveEmployee, then

-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.

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.
 
-----Original Message-----
From: Author #1 [mailto:[email protected]]
Posted At: Tuesday, August 11, 2009 6:52 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: Naming issue: It is hard to keep it consistent
Subject: Re: Naming issue: It is hard to keep it consistent
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.

One advantage I find when naming a button web control BtnSave and the
event handler BtnSave_Click is the way intellisense works by sorting the
variable names and function names alphabetically. By prefixing your
Button controls with "Btn" when in code-behind you immediately get the
list of all buttons such as:
BtnEdit
BtnSave
BtnQuit

With intellisense, Hungarian notation can be beneficial in certain
cases.

One particular good read on naming convention is Brad Abram's "Framework
Design Guidelines" that explains the design rationales behind .NET
framework including .NET framework naming convention.

Hong

Thank you. I agree with you. It seems that it is really hard to stay
with only *one* naming convention throughout a project.
 
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.

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.
 
Back
Top