2. Contents
• ■ 1 Data Literacy
1.1 Data Literacy Test
• ■ 2 Making Variable Declarations Easy
2.1 Implicit Declarations
2.2 Explicit Declarations
■ 3 Guidelines for Initializing Variables
3.1 Guidelines for avoiding initialization problems:
• ■ 4 Scope
• ■ 5 Persistence
• ■ 6 Binding Time
• ■ 7 Relationship Between Data Types and Control Structures
• ■ 8 Using Each Variable for Exactly One Purpose
• 8.1 General Considerations In Using Data
ii
1/29/2016
Chp:10 General issues in using varibale
3. Data Literacy
Defination:
Data literacy is the ability to read,create, and communcate
data as infomation and has been formally described in varying
ways.
1.1
1/29/2016
Chp:10 General issues in using varibale
4. Data literacy
Data literacy Terms
Abstract data type literal Array
local variable bitmap lookup table
boolean variable member data B-tree
pointer character variable private
container class retroactive synapse double precision
referential integrity elongated stream stack
enumerated type string Floating point
Structured Variable Heap Tree
Index Typedef integer
Union Linked List Value Chain
Named Constant Variant
1.2
These are some Common Terms that use in programming language
1/29/2016Chp:10 General issues in using varibale
5. Making Variable Declaration Easy
Two types of Declaration occur
1. Implicit Declaration
(use in visual Basic,Fortron,PHP)
2. Explicit Declaration
(use in java,C++)
2.1
1/29/2016
Chp:10 General issues in using varibale
6. Making Variable Declaration Easy
Implicit Declaration:
• Some languages compiler implicitly declare, which means that you can
use every variable without Declaration. You can remove this
requirement and permit explicit declaration.
• In implicit declaration variable automatically convert to that type
which type of value assign to that variable.
2.2
Explicit Declaration:
• Most of the language use explicit declaration, which means that
you can not use any variable without Declaration. You should declare
the type of variable before use it .After this compiler assign the
memory to that variable according to that type.
• In explicit declaration user should follow the type of variable that he
declare otherwise compiler give error Message.
1/29/2016
Chp:10 General issues in using varibale
7. Making Variable Declaration Easy
Explicit Declaration is Recomended.
2.3
Which one is the best implicit or explicit??
1/29/2016Chp:10 General issues in using varibale
8. What do you do if you program in a language with implicit
declarations?
Some suggestions:
1. Turn off implicit declaration
2. Declare all variables
3. Use naming conventions
4. Check Variable names
2.4Making Variable Declaration Easy
1/29/2016Chp:10 General issues in using varibale
9. Guidelines for Initializing Variables
Improper data initialization is one of the most fertile sources
of error in computer programming.
3.1
Reasons:
The variable has never been assigned a value.
Part of the variable has been assigned a value and part has not.
The value in the variable is outdated .
1/29/2016Chp:10 General issues in using varibale
10. Following are guidelines for avoiding initialization
problems:
Initialize each variable as it's declared.
Use const/final when possible.
Pay attention to counters and accumulators.
Initialize a class's member data in its constructor.
Check the need for re-initialization.
Take advantage of your compiler's warning messages
Check input parameters for validity .
Use a memory-access checker to check for bad pointers.
Initialize working memory at the beginning of your program.
3.2Guidelines for Initializing Variables
1/29/2016Chp:10 General issues in using varibale
11. Scope
“Scope” is a way of thinking about a variable’s celebrity
status: how famous is it?
• It refers to the area to which your variables are known and
can be referenced throughout a program.
• A variable with limited or small scope is known in only a
small area of a program
• A variable with large scope is known in many places in a
program
4.1
1/29/2016Chp:10 General issues in using varibale
12. Scope
We can measure scope by:
Span.
It is a method of measuring how close together the
references to a variable are.
Live Time.
The total number of statements over which a variable is live
4.2
1/29/2016Chp:10 General issues in using varibale
13. Scope 4.3
Measuring span and life time
1. a = 0;
2. a++; Life time = (4-1 +1) = 4
3. a+= 10; Average span = (0+0+0)/3 = 0;
4. a = 3;
1. a = 0;
2. b= 1; Life time = (4-1 +1) = 4
3. c = 0; Average span = 2/1 = 2;
4. a = b + c;
1/29/2016Chp:10 General issues in using varibale
14. Scope 4.4
Why we try to restrict live time?
You can concentrate on a smaller section of code.
Code more readable.
reduces the chance of initialization errors .
Easy to modify.
1/29/2016Chp:10 General issues in using varibale
16. Scope 4.6
Initialize variables used in a loop immediately before the
loop .
Don't assign a value to a variable until just before the
value is used.
Break groups of related statements into separate routines .
Begin with most restricted visibility, and expand the
scope if necessary .
Keep both span and live time as short as you can.
Guideline for minimizing Scope:
1/29/2016Chp:10 General issues in using varibale
17. Persistence 5.1
Persistence is life span of a piece of data.
Problem:
When you assume variable is persist but it's not .
Solution:
Set variables to "unreasonable values" when they doesn’t persist.
Use assertions to check critical variables.
Declare and Initialize all data right before it's used.
1/29/2016Chp:10 General issues in using varibale
18. Binding Time 6.1
The time at which the variable and its value
are bound.
The later you bind, the more flexibility you get
and the higher complexity you need .
titleBar.color = ReadTitleBarColor( )
titleBar.color = 0xFF;
titleBar.color = TITLE_BAR_COLOR;
Run Time
Compile Time
Code-Writing Time
1/29/2016Chp:10 General issues in using varibale
19. Relationship Between Data Types
and Control Structures
• Data types and control structures relate to each other in well-
defined ways that were originally described by the British
computer scientist Michael Jackson
• Jackson draws connections between three types of data and
corresponding control structures:
1. Sequential data
2. Selective data
3. Iterative data
7.1
1/29/2016Chp:10 General issues in using varibale
20. Using Each Variable for Exactly
One Purpose
• Use each variable for one purpose only
it’s sometimes tempting to use one variable in two different
places for two different activities. Usually, the variable is
named inappropriately for one of its uses or a “temporary”
variable is used in both cases (with the
8.1
// Compute roots of a quadratic equation
temp = Sqrt( b*b - 4*a*c );
root[0] = ( -b + temp ) / ( 2 * a );
root[1] = ( -b - temp ) / ( 2 * a );
...
// swap the roots
temp = root[0];
root[0] = root[1];
root[1] = temp ;
1/29/2016Chp:10 General issues in using varibale
21. Using Each Variable for Exactly
One Purpose 8.2
Avoid hybrid coupling.
Double use is clear to you but it won't be to someone
else.
Make sure all declared variables are used.
1/29/2016Chp:10 General issues in using varibale
22. Checklist: General Considerations
In Using Data : 8.3
Initializing Variables
Does each routine check input parameters for validity?
Does the code declare variables close to where they're first
used?
Does the code initialize variables as they're declared, if
possible?
Are counters and accumulators initialized properly and, if
necessary, reinitialized each time they are used?
Does the code compile with no warnings from the compiler?
1/29/2016Chp:10 General issues in using varibale
23. Checklist: General Considerations
In Using Data Cont.. 8.4
Other General Issues in Using Data
Do all variables have the smallest lift time possible?
Are references to variables as close together as possible?
Are all the declared variables being used?
Are all variables bound with balance between the flexibility
and the complexity associated?
Does each variable have only one purpose?
Is each variable's meaning explicit, with no hidden meanings?
1/29/2016Chp:10 General issues in using varibale
1.The first step in creating effective data is knowing which kind of data to create.
2. A good repertoire(Range) of data types is a key part of a programmer’s toolbox.
3. “Data Literacy Test” will help you determine how much more you might need to learn about them.
The term implicit means when compiler automatically do that work without user knowledge.
The Term explicit mean when user do that work without depending on compiler
The most common disadvantage of using the implicit declaration is readability and does not recommended in a language that have pointers because it cause a lot of trouble in readability and if any mistake occur regarding to type then compiler doesnot point out that mistake .
Explicit declaration requires you to use data more carefully which is the main advantage of using this declaration.
Implicit declaration:
Some compilers allow you to disable implicit declarations.
For example, in Visual Basic you would use an Option Explicit statement, which
forces you to declare all variables before you use them.
Declare all the variables:
As you type in a new variable, declare it, even though the compiler
doesn’t require you to. This won’t catch all the errors, but it will catch some of them.
Naming conventions:
Establish a naming convention for common suffixes such as
Num and No so that you don’t use two variables when you mean to use one.
Check Variable names:
Use the cross-reference list generated by your compiler or
another utility program. Many compilers list all the variables in a routine, allowing
you to spot both acctNum and acctNo. They also point out variables that you’ve
declared and not used.
“Scope” is a way of thinking about a variable’s celebrity status: how famous is it?
Scope, or visibility, refers to the extent to which your variables are known and can be
referenced throughout a program. A variable with limited or small scope is known in
only a small area of a program—a loop index used in only one small loop, for instance.
A variable with large scope is known in many places in a program—a table of employee
information that’s used throughout a program, for instance.
Are they bound together when the code is written? When it is compiled? When it is loaded? When the program is run? Some other time?
Code time : if this 0xFF changes, it can get out of synch with 0xFFs used elsewhere in the code that must be the same value as this one.
Sequential data translates to sequential statements in a program Sequences consist
of clusters of data used together in a certain order, as suggested by Figure 10-2. If
you have five statements in a row that handle five different values, they are sequential
statements. If you read an employee’s name, Social Security Number, address, phone
number, and age from a file, you’d have sequential statements in your program to read
sequential data from the file.
Selective data translates to if and case statements in a program In general, selective
data is a collection in which one of several pieces of data is used at any particular time, but
only one, as shown in Figure 10-3. The corresponding program statements must do the
actual selection, and they consist of if-then-else or case statements. If you had an employee
payroll program, you might process employees differently depending on whether they
were paid hourly or salaried. Again, patterns in the code match patterns in the data.
Iterative data translates to for, repeat, and while looping structures in a program
Iterative data is the same type of data repeated several times, as suggested by Figure
10-4. Typically, iterative data is stored as elements in a container, records in a
file, or elements in an array. You might have a list of Social Security Numbers that
you read from a file. The iterative data would match the iterative code loop used to
read the data.
hybrid coupling :The value in the variable pageCount might represent the number of pages printed, unless it equals -1, in which case it indicates that an error has occurred.
the integer is moonlighting as a boolean