AppA_HungarianForDotNETPrograms.pdf

(358 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework
Appendix A
Appendix A
Hungarian for .NET Programs
Version 1.0
September 5, 2003
By: Paul Yao and David Durant
Time is a software developer's most precious resource. For that reason, anything
that can help save time – at any phase of the project – while maintaining or increasing
productivity is a good thing. When we started to work on this book 1 , we began by
collaborating on a set of standards to minimize the differences between our coding
styles. As the project moved forward, we refined and updated various elements of this
file. We offer this set to you, our readers, as a starting point for your own coding
standards. (To get an editable copy of this file in Microsoft Word format, email
After the book is published, we expect to continue updating the various elements of
this file and maintaining it on our web site (http://www.pauyao.com), and welcome your
comments and suggestions. To submit a comment or suggestion, click on any hyperlink
in a machine-readable version of this file. Each hyperlink is numbered, to help us
understand the specific part of the document which triggered your response. For general
comments, general suggestions, or new types to include, click on this link: [Comment A.1]
Goals:
1
Enhance code readability. [Comment A.2]
2
Make elements of the sample code – classes, properties, methods,
events, fields – clearly distinct from those elements of the Compact
Framework. [Comment A.3]
3
Work equally well in both C# and Visual Basic .NET. [Comment A.4]
4
Establish standards which would work equally well in Compact
Framework code as well as desktop .NET Framework code. [Comment
Guidelines
These standards are for a coding project that exclusively uses Microsoft's .NET
technology, specifically the C# and VB .NET languages. These are, in short,
1 The books mentioned above (for the PDF version of this file) are: Programming the .NET
Compact Framework in C# and Programming the .NET Compact Framework in VB.NET , both by
Paul Yao and David Durant, and both published by Addison-Wesley.
© Copyright 2002 Paul Yao & David Durant
1 of 14
9/10/2003 2:24 PM
All Rights Reserved
870585302.048.png 870585302.051.png
 
Programming the .NET Compact Framework
Appendix A
recommendations that apply specifically to .NET projects. These standards were created
with the following guidelines in mind: [Comment A.6]
Case Insensitive – C# is case-sensitive, but VB .NET is not. For that reason, we avoided
any convention that relied on distinguishing upper-case from lower-case names. The
single exception to this rule involves the names for constants, and for members of
enumerations, which shall be all upper-case. [Comment A.7]
Hungarian Naming – Although this style of naming has lost favor with some, it has
always been one that we like. For that reason, our naming standard relies heavily on
Hungarian. [Comment A.8]
The Visual Studio .NET forms designer suggests names, some of which can be left alone
but others are likely to change. For example, labels on forms are given names like label1,
label2, label3, etc. In many cases, programmers have no interest in changing these. As
much as possible, the naming scheme described here works with the default names
provided by the forms designer. [Comment A.9]
Avoid terse abbreviations - both for prefixes and for variable names – in favor of making
them more meaningful. While terse names make it easier to type, the problem they create
is that they are somewhat hard to remember. For example, the name for TextBox is
text rather than the shorter, but less understandable, txt . [Comment A.10]
Include elements in the Compact Framework. The focus of this book is the Compact
Framework, as such we avoid naming things that are not in the Compact Framework.
Because one of the design goals of the Compact Framework was to maintain consistency
with the desktop framework, this approach means that these guidelines can be extended
as future versions of the Compact Framework become available. [Comment A.11]
.NET Naming Standards
The Microsoft .NET documentation provides some guidelines to start with. Of
particular interest is a section titled "Design Guidelines for Class Library Developers."
This strict set of rules helps insure that new class libraries – custom controls, SQL data
adapters, etc. – are easily understandable to programmers who are already familiar with
.NET conventions. You can read the details of these guidelines here: [Comment A.12]
Rather than provide the copious detail of those design guidelines, we have
summarized the key points here. This book generally follows these guidelines, with a few
exceptions which are noted later. Understanding these guidelines will help you write
code that is generally more readable and maintainable than if you did not. [Comment
Type
Convention
Notes and Examples
Containing Elements
Namespace [Comment A.14] s
Combine the following elements
together for namespace names in
public libraries:
Company Name
Technology Name
Example:
Microsoft.WindowsCE.Forms
© Copyright 2002 Paul Yao & David Durant
2 of 14
9/10/2003 2:24 PM
All Rights Reserved
870585302.052.png 870585302.001.png 870585302.002.png 870585302.003.png 870585302.004.png 870585302.005.png 870585302.006.png 870585302.007.png 870585302.008.png 870585302.009.png
 
Programming the .NET Compact Framework
Appendix A
Type
Convention
Notes and Examples
Feature
Design
Examples of nouns as class names:
Use nouns or noun
phrases for classes
Control, Form, TextBox
For derived classes,
create compound names
for classes by appending
base class name at end
of new class name.
Examples of derived class names:
DateTextBox
MainForm
Contained Elements
Attribut [Comment A.16] e
Example:
HelpFileAttribute,
AuthorAttribute
Use suffix of "Attribute"
for custom attribute
classes.
Enumeratio [Comment A.17] n
Do not use "Enum" in
name of enumeration
Examples:
CombineMode
FontStyle
GraphicsUnit
Keys
DayOfWeek
FileShare
Event Argument Clas [Comment
A.18] s
Use suffix of EventArgs
for event argument class
Examples:
RowUpdatedEventArgs
KeyEventArgs
MouseEventArgs
Event Handle [Comment A.19] r
Examples:
TreeViewEventHandler
KeyEventHandler
Use suffix of
"EventHandler" for event
handler itself
Event Handling Metho [Comment
A.20] d
Provide protected
method OnXXX where
XXX = event name
Examples:
OnParentChanged
OnRowUpdated
OnKeyDown
OnMouseMove
Event Name [Comment A.21] s
Examples:
ParentChanged
EnabledChanged
GotFocus
LostFocus
Use verbs for event
names
Exceptio n [Comment A.22] s
Examples:
ArithmeticException
DivideByZeroException
IndexOutOfRangeException
Use suffix of "Exception"
for exception names.
Interfac [Comment A.23] e
Examples:
ICollection
IComparer
IDictionary
IEnumerable
IEnumerator
IList
Use prefix of "I" for
interface names.
Use nouns, noun
phrases, or adjectives
that describe behavior.
Method [Comment A.24] s
Examples:
Activate
Use verbs or verb
phrases
© Copyright 2002 Paul Yao & David Durant
3 of 14
9/10/2003 2:24 PM
All Rights Reserved
870585302.010.png 870585302.011.png 870585302.012.png 870585302.013.png 870585302.014.png 870585302.015.png 870585302.016.png 870585302.017.png 870585302.018.png 870585302.019.png 870585302.020.png 870585302.021.png 870585302.022.png 870585302.023.png 870585302.024.png 870585302.025.png 870585302.026.png 870585302.027.png 870585302.028.png
 
Programming the .NET Compact Framework
Appendix A
Type
Convention
Notes and Examples
Close
Invoke
ShowDialog
Method Parameter [Comment
A.25] s
Use descriptive
parameter names
Do not use Hungarian
naming
Use type-based
parameter names
sparingly
Example:
Property [Comment A.26]
Use noun or noun phrase
Examples:
BackColor
Font
Text
Width
Do not use Hungarian
naming
Static Field [Comment A.27]
Examples:
Use noun, noun phrases,
or noun abbreviations for
the name
Use Hungarian naming
Do not use Hungarian
naming – good names
describe semantics not
type
Hungarian Naming
Hungarian naming refers to a style of creating meaningful variable names. An MSDN
Technical Article gives this explanation for how this approach got its name: [Comment
"It came to be known as "Hungarian notation" because the prefixes make the variable names look
a bit as though they're written in some non-English language and because Simonyi is originally
from Hungary." – Note from Dr. Gui, November, 1999. [Comment A.30]
Rather than repeat all the details that are so well described in this article, here is a link to
a reprint of Charles Simonyi's original white paper on this naming convention for readers
who wish to learn more: http://msdn.microsoft.com/library/en-
Hungarian naming is not a standard. In other words, you will not find an ISO
standards committee – or, for that matter, an ANSI / IEEE / ECMA committee – that
dictates what prefixes you can and cannot use. While there are many lists of suggested
prefixes – including this appendix – none claim to be the definitive one that supersedes
all others. [Comment A.32]
Hungarian naming is, instead, a style. Lists (like this one) that call themselves
"standards" are really just starting points for project-specific data types, because with
few exceptions every programming project has its own unique set of types. Where you
get the greatest benefits from Hungarian are on projects where all agree to use the
same set of types. It provides a common language so that any team member can look at
any code and understand it. [Comment A.33]
© Copyright 2002 Paul Yao & David Durant
4 of 14
9/10/2003 2:24 PM
All Rights Reserved
870585302.029.png 870585302.030.png 870585302.031.png 870585302.032.png 870585302.033.png 870585302.034.png 870585302.035.png 870585302.036.png 870585302.037.png 870585302.038.png 870585302.039.png
 
Programming the .NET Compact Framework
Appendix A
We have a friend who used to work at Microsoft. He states that the disciplined use of
Hungarian naming – and a project-specific set of types and associated Hungarian
prefixes – allowed him to transfer to the Excel team during a crunch period and be
productive very quickly. [Comment A.34]
This Book
We use Hungarian naming throughout this book for data types. We do so because
we find it makes our code more readable, and therefore more understandable. [Comment
The use of Hungarian also provides another important benefit: it allows you to quickly
find names in an IntelliSense list. IntelliSense is, of course, a feature of the Visual Studio
.NET text editing windows. Just about every time you type a dot operator, it pops up with
possible choices for the next part of an operation. [Comment A.36]
For example, suppose you are writing some code and need to know the name of a
string variable that is a data member of the class you are writing. The "old-fashioned"
way to find the variable name would be to do a search, or to scroll to the top of your
source file (or wherever you normally put such things). When you get there, you might
find a list of string names defined like this: [Comment A.37]
string strFirstName;
string strLastName;
string strAddress;
string strCity;
string strState;
string strPostalCode;
Another alternative is to ask IntelliSense to help you. By typing
this.str
in the text editor window, you give IntelliSense enough details to help you remember the
specific name that you were looking for. Figure B-1 shows how the IntelliSense window
showing the available data members that starts with a " str " prefix. [Comment A.38]
Fig ure B-1 Hungarian Naming helps IntelliSense remind you of variable na mes
© Copyright 2002 Paul Yao & David Durant
5 of 14
9/10/2003 2:24 PM
All Rights Reserved
870585302.040.png 870585302.041.png 870585302.042.png 870585302.043.png 870585302.044.png 870585302.045.png 870585302.046.png 870585302.047.png 870585302.049.png 870585302.050.png
 
Zgłoś jeśli naruszono regulamin