Object-Oriented Programming

Naming

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Importance of Naming

  • Critical issue for software engineering
  • Software Engineers name things constantly
  • Naming impacts the readability and comprehension of software
  • Good names reduce the cost of development
  • Careful selection of names can convey the high-level meaning of a task to the developer

Naming

If you have a good name for a method, you do not need to look at the body - Fowler et al.

There are only two hard things in Computer Science: cache invalidation and naming things - Phil Karlton

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors Leon Bambrick

What is a name?

  • Technical term: identifier
  • Lowercase letters: 'a'..'z'
  • Uppercase letters: 'A'..'Z'
  • Digits: '0'..'9'
  • Underscore: '_'
  • First character: letter or underscore

Only ASCII?

Only ASCII?

Only ASCII?

Non-ASCII Names

  • C++ standards only allow certain Unicode non-ASCII letters
  • Other languages allow Unicode characters, e.g., Swift, even allowing emojis
  • Some compilers support extensions of this, e.g., clang
  • As used in srcML TextLexer.g
  • Some parts of your toolchain may handle only ASCII, so stick to ASCII unless needed

Exceptions to the Rules

  • Reserved Words: C++ Reserved Words
  • contextual keywords - identifiers with special meaning in a specific context
  • Avoid starting with an underscore '_' as it is reserved for system use

Lots to Name

  • variables
  • fields (data members)
  • parameters
  • types
  • functions
  • methods
  • classes

General Rules

  • Names should not include types or any symbol for the types. I.e., the Hungarian Notation is not a good style
  • Variables, parameters, types, classes, fields, classes, files, are entities, i.e., things and therefore use nouns
  • Functions and methods are actions and therefore use verbs
  • For most functions/methods, the action is in the imperative form, e.g., use parse() instead of parsing() or parsed()
  • Will focus on function/method names for now

Operator Overloading: Built-In Types

Function Overloading

  • C++, and most modern programming languages, allow function overloading, while C does not
  • Can use the same name for multiple functions as long as the parameter list is different (number, types)
  • Uniqueness is not influenced by the return type
  • Name is based on: function name, cv-qualifiers, parameter types

g++ -S struct.cpp

struct.cpp
struct.s

Executable Environment

  • C language has functions (with no overloading) and structs
  • CPU supports assembly labels that refer to a memory address
  • CPU supports instructions to call a function located at a memory location (callq, call, call1, bl) and return from a function (retq, ret, retl)

Mangling

 

C++ Function Assembly Name
action() __Z6actionv
action(int) __Z6actioni
action(double) __Z6actiond
action(char) __Z6actionc
action(double&) __Z6actionRd
action(double const&) __Z6actionRKd
action(double*) __Z6actionPd
action(double const*) __Z6actionPKd
action(std::vector<int>&) __Z6actionRNSt3__16vectorIiNS_9allocatorIiEEEE
action(const std::vector<int>&) __Z6actionRKNSt3__16vectorIiNS_9allocatorIiEEEE

C++ Function Naming Tools

Description Command
Compile to Assembly g++ -S overloading.cpp
Compile to x86 Assembly on M1 Mac g++ -target x86_64 -S overloading.cpp
List symbols in object file nm overloading
Demangle a symbol c++filt __Z6actioni
Demangle all symbols in object file nm --demangle overloading