Communicating in Code: Naming

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Naming Programming Studio Spring 2008 Lecture 2. Purpose of Coding.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Dobrin / Keller / Weisser : Technical Communication in the Twenty-First Century. © 2008 Pearson Education. Upper Saddle River, NJ, All Rights Reserved.
General Issues in Using Variables
Communicating in Code: Naming Programming Studio Spring 2013.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Input & Output: Console
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Reading. This part of the TOEFL tests your ability to read academic English. This is important if you want to succeed at an English college or university.
CPS120: Introduction to Computer Science
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS242.  Reduce Complexity  Introduce an intermediate, understandable abstraction  Avoid code duplication  Support subclassing  Hide sequences  Hide.
Guidelines for a Language- Independent Convention Identify global variables –Use g_prefix –Exp: g_RunningTotal Identify module variables –Use m_prefix.
More C++ Features True object initialisation
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
Communicating in Code: Naming Programming Studio Spring 2009.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CPS120: Introduction to Computer Science Variables and Constants.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
1 January 31, Documenting Software William Cohen NCSU CSC 591W January 31, 2008.
Bill Tucker Austin Community College COSC 1315
Chapter VII: Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
VBScript Session 1 Dani Vainstein.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
4. Java language basics: Function
VBScript Session 1 IT Professional Academy.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
MEmos.
Big-O notation.
Lecture – 2 on Data structures
C Programming Tutorial – Part I
Road Map Style Principle of proximity Naming variables if / else.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Programmazione I a.a. 2017/2018.
Array.
Variables and Arithmetic Operations
Communicating in Code: Naming
Object Oriented Programming COP3330 / CGS5409
Week 9 – Lesson 1 Arrays – Character Strings
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CMSC 202 Java Primer 2.
Pointers and Dynamic Variables
Operator Overloading, Friends, and References
Data Types and Data Structures
7.4 | Editing.
Tonga Institute of Higher Education
Random Access Files / Direct Access Files
Building Java Programs
Beginning Style 27-Feb-19.
Overloading functions
Lists.
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
VBScript Session 1.
Variables in C Topics Naming Variables Declaring Variables
C Structures and Commands
Miscellaneous Topics I
Web Programming and Design
Presentation transcript:

Communicating in Code: Naming Programming Studio, Fall 2017 Robert Lightfoot

Names We assign names throughout a program Give identity Imply behavior/purpose Provide recognition

What gets named? Variables Functions Types/classes Namespaces Macros Source Files

Choosing Names Why are names like i, j, x, y, p, n, b used? 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.

Naming Considerations Be sure it’s not a reserved name (Duh!) Sometimes it’s easy to forget… Make it informative Keep it concise Make it memorable Make it pronounceable

Why is Good Naming Important? Human brain’s storage capacity is practically limitless Approximately 2.5 petabytes (~300 years worth of FHD video) However, the ‘short term’ memory used during thinking and being creative is quite limited Only 5 to 9 pieces of information (some studies show even less) Therefore, we do not add anything to that small footprint Make things as self-describing as possible and remember them only for a short time when necessary (i.e., using them in code) Source: https://blog.synap.ac/10-surprising-facts-about-your-memory/

Informative Names Ideally, we want everything to be apparent from the name itself The larger or more descriptive the name is, the better However, long names are not easy to remember/reuse either To resolve this conflict (long name vs short), we will take help from the “context” of the variable/function The amount of information a name needs depends on its context/scope – understand it when seen Use descriptive names for globals, short names for locals Large routines/loops need more descriptive names s = 0; for (WhichGroup=0; WhichGroup<num; WhichGroup++) { s += G[WhichGroup].n(); } Do we really need to be this much descriptive? What is the goal? What on the world is this? What are we doing again?

Informative Names The amount of information a name needs depends on its scope – understand it when seen Use descriptive names for global items, short names for local item Large routines/loops need more descriptive names nAnimals = 0; for (i=0; i<NumAnimalGroups; i++) { nAnimals += AnimalGroup[i].NumberInGroup(); }

Descriptive Names Names should convey what it represents or does, unless obvious from context Describe everything a routine does Print() vs. PrintAndCloseFile() Avoid meaningless or vague names HandleData(), PerformAction(), etc. Focus on the “what” instead of “how” inputRecord vs employeeData

Descriptive Names Procedures: Active names Verb followed by noun AnotherStudent(s) vs. AddStudent(s) Functions different: give return value GetNumStudents() vs. numStudents() Booleans: Be clear what is returned checkEOF() vs. isEOF()

Consistent Names Key: Be Consistent! nKids, numKids, num_kids, NumKids, nkids, Number_Kids, numberofkids Write1stObject(), WriteSecondObject(), write_third_object() averageSalary vs. salaryMinimum open/close, first/last, old/new, min/max, etc.

Consistent Names CloseFile() vs. fclose() Use related names for related operations OpenFile(): CloseFile() vs. fclose() open/close, first/last, old/new, min/max, etc.

Name Length Tradeoff between description and visual space Moderate-length names tend to be best 8-20 characters If a glance at the code seems like it has lots of short or lots of long names, use caution! Scope plays a role Rarely-used functions might be longer

Name Length Taken from Code Complete 2nd Edition

Other Random Naming Considerations Beware of “temp” variables Sometimes indicates lack of understanding Should be replaced with real names when possible Be careful of reusing variable names Be careful of overloading names Avoid intentional misspellings Consider pronunciation People will end up “reading” your code

Conventions Lots of conventions out there Conventions help convey information away from its definition Very useful for larger groups/programs Sometimes critical to large projects! Examples: Globals have initial capital letters Constants are in ALL CAPS Etc.

Common Naming 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 Beginning with _ for private information, or reserved commands

Example: Hungarian Naming Convention Base types: wn Window scr Screen Region fon Font ch Character pa Paragraph Eg: wnMain, scrUserWorkspace

Example: Hungarian Naming Convention Prefixes a array c count d difference between two variables e element of array g global variable h handle i index into array e.g. iwnUserView = index into array of windows giving user views

Naming Conventions Have to assume that others will follow the convention Hungarian notation: confusing or helpful? Always use a defined convention at your workplace If no defined convention, use a common one, rather than your own creation This goes for other style aspects besides naming…