160 likes | 220 Views
Variables. Software Engineering. The data literacy test. Count 1.0 if you know what the concept means. Count 0.5 if you believe you know what the concept means, but you are not sure. Add the points and check your score. The data literacy test. abstract data type heap retroactive synapse
E N D
Variables Software Engineering
The data literacy test Count 1.0 if you know what the concept means. Count 0.5 if you believe you know what the concept means, but you are not sure. Add the points and check your score.
The data literacy test abstract data type heap retroactive synapse array index referential integrity bitmap integer stack boolean variable linked list string B-tree named constant structured variable character variable literal tree container class local variable typedef double precision lookup table union elongated stream member data value chain enumerated type pointer variant floating point private
The data literacy test 0-14: beginner. You should not be attending this course. 15-19: intermediate. How would you consider attending more basic courses? 20-24: expert. Welcome to this course. 25-29: genius. You should be teaching this course. 30-32: fraud. You could not even identify some meaningless names that were included in the list...
Variable initialisation Initialise each variable as it is declared (re)Initialise each variable close to where it is first used Ideally, declare and initialise each variable close to where it is first used Use final (Java), const (C++), etc. whenever possible, so that programming variables behave similar to mathematical variables
Variable initialisation Pay special attention to counters and accumulators Initialise a class's member data in its constructor Check the need for reinitialisation Check input parameters for validity
Scoping Localise (i.e. put as close as possible) references to variables Minimise average variable span (span: distance between consecutive references of a variable, in lines): a = 0; span(a) = 2 b = 0; span(b) = 1 c = 0; span(c) = 0 a = b + c;
Scoping Minimise “life span” of each variable (life span: total number of lines between first and last reference to a variable) Initialise variables in a loop immediately before the loop Do not assign a value to a variable until just before the value is used
Scoping Group related statements with respect to variable referencing Break groups of related statements into separate routines Larger scopes make program easier to write; smaller scopes make program easier to read
Using variables Use each variable for one purpose only Avoid hidden meanings (e.g. “Age means age of a person, except when it values -1, in which case it denotes that the person's age is unknown”) Make sure that all declared variables are used
Naming variables Names should be expressive and self-contained Names should refer to the problem rather than the solution (e.g. EmployeeData is better than InputRec) Names should not be too long nor too short (between 8 and 20 characters is fine)
Naming variables Use conventions for variable names related to specific data types: Loop indices: recordCount, teamIndex Status variables: characterType, recalcNeed Temporary variables: avoid them; if you need them, avoid names like temp or x Boolean variables: done, error, found, success, ok; avoid negative names like notFound
Naming variables USE CONVENTIONS
Naming variables Abbreviations: Do not abbreviate by removing one character from a word Abbreviate consistently Create names that you can pronounce Avoid combinations that result in misreading or mispronunciation Document short names with translation tables in the code
Naming variables Names to avoid: Avoid misleading names or abbreviations Avoid names with similar meanings Avoid variables with different meanings and similar names Avoid names that sound similar Avoid numerals in names, as in file1 and file2 Avoid misspelled words in names Do not differentiate variables solely by capitalisation
Naming variables Names to avoid: Avoid multiple natural languages Avoid the names of standard types, variables and routines Avoid names containing hard-to-read characters, e.g. Hard2Read and HardZRead Do not use names that are totally unrelated to what the variables represent