Download presentation
Presentation is loading. Please wait.
1
Naming Programming Studio Spring 2008 Lecture 2
2
Purpose of Coding
3
To give the computer instructions?
4
Purpose of Coding To give the computer instructions
5
Purpose of Coding To give the computer instructions To demonstrate your skill?
6
Purpose of Coding To give the computer instructions To demonstrate your skill
7
Purpose of Coding To give the computer instructions To demonstrate your skill An effective way to express ideas of what you want the computer to do
8
Purpose of Coding An effective way to express ideas of what you want the computer to do Communication! –To the computer –To yourself (later on) –To others
9
Names We assign names throughout a program Good names are better than good documentation –They are a critical part of documentation –With good naming, other comments are often unnecessary!
10
Naming Gives identity Implies behavior/purpose Makes it recognizable
11
What gets named? Variables Functions Types/classes Namespaces Macros Source Files
12
Choosing Names Sometimes there are naming conventions –If you work at a company that has an agreed convention, follow it! But, there are several “wise” ideas to consider when choosing names.
13
Naming Considerations Be sure it’s not reserved (Duh!) Sometimes it’s easy to forget… 1.Make it informative 2.Keep it concise 3.Make it memorable 4.Make it pronounceable
14
Informative Names The amount of information a name needs depends on its scope – understand it when seen Use descriptive names for globals, short names for locals s = 0; for (WhichGroup=0; WhichGroup<num; WhichGroup++) { s += G[WhichGroup].n(); }
15
Informative Names The amount of information a name needs depends on its scope Use descriptive names for globals, short names for locals nAnimals = 0; for (i=0; i<NumAnimalGroups; i++) { nAnimals += AnimalGroup[i].getnumber(); }
16
Descriptive Names Names should convey what it represents or does, unless obvious from context Functions: Active names –AnotherStudent(s) vs. AddStudent(s) –Query functions different: numStudents() Booleans: Be clear what is returned –checkEOF vs. isEOF
17
Conventions Lots of conventions out there Globals have initial capital letters Constants are in ALL CAPS Etc.
18
Conventions Beginning/ending with a p if a pointer Starting with n for a number i, j are integer indices s is a string, c or ch are characters
19
Consistent Names Key: Be Consistent! –nKids, numKids, num_kids, NumKids, nkids, Number_Kids Functions too: Write1stObject() WriteSecondObject() write_third_object()
20
Other Random Considerations Beware of “temp” variables Be careful of reusing variable names Be careful of overloading names
21
Hungarian Naming Convention Base types: –wnWindow –scrScreen Region –fonFont –chCharacter –paParagraph Eg: wnMain, scrUserWorkspace
22
Hungarian Naming Convention Prefixes –aarray –ccount –ddifference between two variables –eelement of array –gglobal variable –hhandle –iindex into array E.g. iwnUserView = index into array of windows giving user views
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.