05-variable-names
05-variable-names
variable names
professor patrick baudisch
pt1
hasso-plattner institute
imagine a actual operating system (Linux has ~15 Million lines of code)
better
number_of_active_processes -= 2;
// we just removed two running processes at once
variable names
variable names in C ::
are made up of letters (upper and lower case) and digits.
The underscore character ("_") is also permitted.
variables starting with “_” (underscore) are formally ok,
but discouraged, since conflicts with library names could occur
from <stdio.h>
_IOFBF // Input/output fully buffered.
_IOLBF // Input/output line buffered.
_IONBF // Input/output unbuffered.
not ok. continue is a reserved word
and thus cannot be a variable name
ok or not?
<30sec brainstorming>
these are the C keywords. You cannot use them as variable names
“ These days, (in part because of type-safe linkage in C++), you can
reasonably rely on much longer names for external symbols. If you start drifting
above 31 characters, you may run into problems - but you are also running into
”
readability problems too.”
http://stackoverflow.com/questions/2352209/max-identifier-length
my 2ct
use great variable names, irrespective of any 6 character limit
(the chance of your code ever running on a 1980’s main frame is smaller than
your code never running at all, because you lost track of variable names.)
styles for naming
variables
capitalization
from what you have seen so far, how do C programmers name their variables
if the variable name is a single word and
if the variable name is composed of multiple words?
<30sec brainstorming>
lowercase ::
The C standard library is mostly lowercase
(and 6ish characters long
lisp-case aka kebab-case ::
words separated by minus '-'
does not work in C
The name "snake_case" comes from the Ruby community,
where it was coined in 2004 by Gavin Kistner, writing:
"BTW...what *do* you call that naming style? snake_case?
That's what I'll call it until someone corrects me.”
snake_case ::
separates words using underscore (e.g. back_color)
in C widely used for variable names
https://en.wikipedia.org/wiki/Snake_case
camelCase ::
starts with a lower case letter (e.g. backColor)
PascalCase ::
like camelCase, but always begins with a capital letter (e.g. BackColor)
coming soon (but what might it be?)
ALL CAPS ::
in C only used for macros = the "preprocessor"
<30sec brainstorming>
i ::
as in
for (int i=3; i<5; i++)
C style guides
just some random example
https://www.cs.arizona.edu/~mccann/cstyle.html
ongoing discussion on stack overflow
http://stackoverflow.com/questions/1722112/what-are-the-most-common-naming-conventions-in-c
if you like to topic, also look at
https://en.wikipedia.org/wiki/Naming_convention_(programming)
end
variable names
professor patrick baudisch
pt1
hasso-plattner institute