ソフトウェアテスト
[9] プログラミング技術
Software Testing
[9] Programming Tips and Techniques
あまん ひろひさ ひろひさ
阿萬 裕久 裕久( AMAN
Hirohisa )
(C) 2007-2022 Hirohisa AMAN
1
Positioning of programming
The role of programming is to realize “P
rogram specification” on a computer
Program specification
programming
Make
something
like this
human
world
The key is
how
accurately
it can be
described
(C) 2007-2022 Hirohisa AMAN
program
Formal
execution
instructions
computer world
The computer faithfully
executes the contents of
the program
2
Software development ≠ program program
ming
People tend to think that “Programming is software dev
elopment”
In the case of small-scale development, requirement
s analysis & design are done only in the developer's
head, & programming is suddenly started.
However, this is not enough for Large-scale develop
ment
Programming skills are important
Don't underestimate design and management
For example, OS for smartphones: millions of lines or more
When printed more than 100,000 sheets of A4 paper (over 10m in height)
(C) 2007-2022 Hirohisa AMAN
3
Important thing in programming
Writing programs that correctly reflect the sp
ecification (error-free)
Be careful not to make the mistake (error) of crea
ting a bug
Write Easy to understand program
Idea: “I just need to know myself” Is not good
Maintenance is impossible unless the program is u
nderstood by others
(Even the person who wrote it may not be able to un
derstand it after 6 months)
(C) 2007-2022 Hirohisa AMAN
4
How to write and make a program
1. Easy to see and understand
2. To prevent mistakes
3. To a simple structure
4. Be environment independent
5. Comment text appropriately
6. Make it easy to change
(C) 2007-2022 Hirohisa AMAN
5
(1) Easy to see and understand
Indent to visually indicate the beginni
ng and end of block
To prevent
forgetting,
Write “{” “ }” at the
same time & insert
code between it
sum = 0;
for ( i = 0; i < n; i++ ){
if ( a[i] > 0 ){
sum += a[i];
}
}
Intentionally
add empty
lines to appeal
to switch
printf("%dn", sum);
execution contents
(C) 2007-2022 Hirohisa AMAN
6
Make variable names meaningful
A name that explains what the variables rep
resent is desirable
However, with a few lines of
program, you can immediately
understand the meaning If you
look at the content
(Ex) Variable i on the previous
page, etc
index, position, count
or idx, pos, cnt for short
When asking Yes/No,
the question form is also used
To separate words, use
is_empty, hasNext
underscore (_)
or use capital letters
Example: If the set is
Example: In the list, the
empty 1, Otherwise
following node is 1 if present,
0
otherwise
(C) 2007-2022 Hirohisa
AMAN 0
7
variable name, function name
snake case and camel case
snake case
Use underscore (_) to connect words
Ex: file_name, get_file_size
For C language,
camel case
snake case
is often used
use capital letters for word breaks
Ex: fileName, getFileSize
(C) 2007-2022 Hirohisa AMAN
8
Be aware of function and return
value in function names
A name that indicates what the functi
on does and what it returns.
(example)
int get_length (char str[ ]){void move_to (int x, int y){
・・・文字列の長さを調べるの長さを調べる長さを調べるさを調べる調べるべる ・・・ (x,y) へ移動移動
}
}
void sort (int array[ ], int length){To separate words, use
underscore (_)
・・・ array を調べるソーティング
or use capital letters
}
(C) 2007-2022 Hirohisa AMAN
9
[Exercise 1] Think of a function n
ame
get help
void
()
leap year
int
(int year)
Copy the contents of an array to another ar
ray
void
(int src[], int dest[], int length)
Original
(C) 2007-2022 Hirohisa AMAN
Copy to
10
length
[Exercise 1] Think of a function na
me (Example answer)
get help
(Another solution)
printHelp
isLeapYear
copyArray
voidprint_help ( )
leap year
int is_leap_y (int year)
Copyear
the contents of an array to another ar
ray
Or array_copy
void copy_array
(int src[], int dest[], int length)
Original
(C) 2007-2022 Hirohisa AMAN
Copy to
11
length
(2) To prevent mistakes
Write curly braces even in a single sentence
if ( x >
0 )
foo(x);
A little mistake
if ( x > 0 )
if ( x > 0 )
printf("%d",x);
foo(x);
printf("%d",x);
foo(x);
}
Avoid reuse of variables
int x = 0;
・・・ (use x)
x = 5;
・・・ (x used for another purpose)
(C) 2007-2022 Hirohisa AMAN
12
{
(3) Simple structure
Do not use goto
I feel that goto sentences
are convenient because
you can jump to any
position you want, but
only in the middle of
making them, and as a
result, they only get
complicated
This kind of program is
called
"spaghetti program”
(C) 2007-2022 Hirohisa AMAN
13
Avoid too many conditional branc
hes
The more if, for, while statements, the more diffic
ult to understand and the more mistakes
a better program (same movement)
sum = 0;
scanf("%d", &x);
do{
if ( x > 0 ){
sum += x;
scanf("%d", &x);
}
} while( x > 0 );
sum = 0;
scanf("%d", &x);
while( x > 0 ){
sum += x;
scanf("%d", &x);
}
After trial and error, the program may have
been like the one on the left, but it has
become more complicated than necessary
(C) 2007-2022 Hirohisa AMAN
14
Cyclomatic number
Number of indepe
ndent loops in the
flow chart
Known as a scale to
evaluate the
Complexity of program
structure
Studies have reported that if
this number exceeds 10,
maintainability is quite poor
(C) 2007-2022 Hirohisa AMAN
15
(4) Do not depend on the environ
ment
Avoid the influence of differences in compu
ter environments
In the C language, the number of bytes allo
cated to a variable may vary depending on t
he processing system (for example, an int t
ype may be 2 bytes or 4 bytes).
That is why we write size of(int)
If the OS is different, the header (library) re
ad by #include may also be different.
A little technical, but switching using #ifdef
etc.
(C) 2007-2022 Hirohisa AMAN
16
(5) Appropriate comments
Functional description should be included
as a comment for each meaningful block
However, it is not enough to simply writ
e
A program that cannot be understood without
writing comments is also a problem
In many cases, it is enough to keep the program st
ructure simple by making the names of function
s and variables easy to understand.
(C) 2007-2022 Hirohisa AMAN
17
(6) Make it easy to change
Let's stop hard coding
Writing numbers and strings as they are
in the source code is called hard coding.
Should be defined or read with symbols f
irst (Ex) Number of data and file name
for ( i = 0; i < 256; i++ ){FILE* fp = fopen(“foo.txt”, “r”);
for ( j = 0; j < 256; j++ ){
Read the file name at runtime,
・・・・・・
Replace with macros such as
SIZE
specified by a macro
(C) 2007-2022 Hirohisa AMAN
18
[Exercise 2] About Hard coding
For example, it is recommended to give
the length of an array and the name of
a data file with macros like
#define SIZE 256
#define DATA_FILE
"foo.txt"
Consider the benefits of doing this
(C) 2007-2022 Hirohisa AMAN
19
[Exercise 2] About Hard coding
(Sample answer)
① Resistant to change
When changing the length of the array, it is OK to rewr
ite 256
If "255" is written in
the sense of "SIZE② Easy to grasp the meaning
1", it will not be
In some cases, the meaning of a number orchanged.
string alon
e may be misunderstood (the same "256" can also
mean "there and here are different meanings")
③ Easy to manage settings
Unique settings can be grouped in one place (at the be
ginning of the program), making it easy to change
and check.
(C) 2007-2022 Hirohisa AMAN
20