Variable
De Software testing
Facts
- It is important that the variable be defined before it is used. <bib>vincenzi-etal:slides:2007</bib>
- Variables that contain data values have a defined life cycle: they are created, used, and then killed (destroyed). <bib>vincenzi-maldonado:slides:2007</bib>
- Variables defined within a code block are created when their definitions are executed. <bib>vincenzi-maldonado:slides:2007</bib>
- Variables defined within a code block are automatically destroyed at the end of a block. <bib>vincenzi-maldonado:slides:2007</bib>
- The scope of a variable is the period between its creation and destruction. <bib>vincenzi-maldonado:slides:2007</bib>
- An occurrence of a variable in a program can be classified as a variable definition or a variable use. <bib>vincenzi-maldonado:slides:2007</bib>
Fact
Consider the following notation:
Three possibilities exist for the first occurrence of a variable through a program path:
- d the variable does not exist, then it is defined (correct)
- u the variable does not exist, then it is used (incorrect - depends on the language)
- k the variable does not exist, then it is killed or destroyed (probably incorrect)
Now consider the following time-sequenced pairs of defined (d), used (u), and killed (k):
dd | Defined and defined again | Not invalid, but suspicious |
du | Defined and used | Perfectly correct |
dk | Defined and then killed | Not invalid, but suspicious |
ud | Used and defined | Acceptable |
uu | Used and used again | Acceptable |
uk | Used and killed | Acceptable |
kd | Killed and defined | Acceptable |
ku | Killed and used | A serious defect |
---|---|---|
kk | Killed and killed | Probably a programming error |
References
- <bib>copeland:2004</bib>
- <bib>vincenzi-maldonado:slides:2007</bib>