More Related Content
Similar to XP寺子屋第9回「シンプル・プログラミング」 (20)
XP寺子屋第9回「シンプル・プログラミング」
- 3. 3
・名前
丈善(たけぷ~)
: 西 丈善(たけぷ~)
・仕事
: 組み込み系
・業界歴
: 20年以上
年以上
関西、
・コミュニティ : XPJUG関西、PFP関西
関西
関西
・使用言語
: C, C++
・SNS
: Twitter takepu
FaceBook 西 丈善
・宣伝
: ▼アジャイルラジオ 毎週水曜日公開
http://www.agileradio.info/
開催決定!
▼XP祭り関西
祭 関西2014 2014年4月26開催決定!
年 月 開催決定
http://www.xpjug.jp
▼出張アジャイル社内研修(無料)受付中
出張アジャイル社内研修(無料)
アジャイル社内研修
XPJUG関西 / XP寺子屋
- 20. 20
悪い例
/******************/
/* バブルソート */
/******************/
#include <stdio.h>
#define MAX 10
void main( )
{
int data[MAX]={ 80,5,36,23,12,100,45,9,1,78 };
int n,i,w;
// 未整列データ数
for( n=MAX; n>1; n-- )
{
for( i=0; i<n-1; i++ )
{
// 次のデータが小さい
if ( data[i]>data[i+1] )
{
// 入れ替え—
w=data[i];
data[i]=data[i+1];
data[i+1]=w;
}
}
}
printf("¥nソート後¥n");
for( i=0; i<MAX; i++ )
{
printf("%d ",data[i]);
}
printf("¥n");
}
XPJUG関西 / XP寺子屋
- 21. 21
改善例
/******************/
/* バブルソート */
/******************/
#include <stdio.h>
#define MAX 10
/******************/
/* バブルソート */
/******************/
#include <stdio.h>
#define MAX 10
void main( )
{
int data[MAX]={ 80,5,36,23,12,100,45,9,1,78 };
int n,i,w;
void main( )
{
int data[MAX]={ 80,5,36,23,12,100,45,9,1,78 };
int n,i,w;
// 未整列データ数
for( n=MAX; n>1; n-- )
{
for( i=0; i<n-1; i++ )
{
// 次のデータが小さい
if ( data[i]>data[i+1] )
{
// 入れ替え—
w=data[i];
data[i]=data[i+1];
data[i+1]=w;
}
}
}
バブルソートを
// バブルソートを行う
for( n=MAX; n>1; n-- )
{
for( i=0; i<n-1; i++ )
{
データを交換する
// データを交換する
if ( data[i]>data[i+1] )
{
w=data[i];
data[i]=data[i+1];
data[i+1]=w;
}
}
}
ソート結果 表示する
結果を
// ソート結果を表示する
printf("¥nソート後¥n");
for( i=0; i<MAX; i++ )
{
printf("%d ",data[i]);
}
printf("¥n");
printf("¥nソート後¥n");
for( i=0; i<MAX; i++ )
{
printf("%d ",data[i]);
}
printf("¥n");
}
XPJUG関西 / XP寺子屋
}
- 22. 22
解説
/******************/
/* バブルソート */
/******************/
バブルソートを
// バブルソートを行う
/******************/
/* バブルソート */
/******************/
#include <stdio.h>
#define MAX 10
データを交換する
// データを交換する
ソート結果 表示する
結果を
// ソート結果を表示する
void main( )
{
int data[MAX]={ 80,5,36,23,12,100,45,9,1,78 };
int n,i,w;
バブルソートを
// バブルソートを行う
for( n=MAX; n>1; n-- )
{
for( i=0; i<n-1; i++ )
{
データを交換する
// データを交換する
if ( data[i]>data[i+1] )
{
w=data[i];
data[i]=data[i+1];
data[i+1]=w;
}
}
}
•コメントは「やりたい事」
ソート結果 表示する
結果を
// ソート結果を表示する
printf("¥nソート後¥n");
for( i=0; i<MAX; i++ )
{
printf("%d ",data[i]);
}
printf("¥n");
•コードは「実現方法」
}
XPJUG関西 / XP寺子屋
- 25. 25
命名規則(1) 関数名
• 変数を取得する関数
– XXX getXXX( )
• 変数を設定する関数
– void getXXX( XXX 変数 )
• Booleanを返す関数
–
–
–
–
–
–
–
is + 形容詞,can + 動詞,has + 過去分詞,三単元動詞,三単元動詞 + 名詞.
boolean isEmpty() // JavaBeans でプロパティとして扱える(推奨)
boolean empty() // だめ!’空にする’という動詞的な意味に取れるため良くない.
boolean canGet()
boolean hasChanged()
boolean contains(Object)
boolean containsKey(Key)
XPJUG関西 / XP寺子屋
- 26. 26
命名規則(2) 変数名
• Boolean 変数
–
–
–
–
形容詞,is + 形容詞,can + 動詞,has + 過去分詞,三単元動詞,三単元動詞 + 名詞.
boolean isEmpty
boolean dirty
boolean containsMoreElements
• 全て有意な名前を付ける
– Info, Data, Temp, Str, Bufという名前は極力使わない
– ループカウンタなどで用いる i, j, k も、適切な名前を付ける
XPJUG関西 / XP寺子屋
- 28. 28
悪い例
/****************/
/* 素因数分解 */
/****************/
#include <stdio.h>
#include <stdlib.h>
void FuncFactorization ( int argc, char *argv[] )
{
int n0,n; // 整数
int ns=2; // 素因数
int j;
// 乗数
n0=atoi(argv[1]);
// 因数分解
for( n=n0,ns=2; n>=ns; ns++ )
{
for( j=0; n%ns==0; j++ )
{
n /= ns;
}
if ( j==0 ) continue; // 1回も割り切れなかった
printf("素因数:%d 乗数:%d¥n",ns,j);
}
}
XPJUG関西 / XP寺子屋
- 29. 29
改善例
/****************/
/* 素因数分解 */
/****************/
#include <stdio.h>
#include <stdlib.h>
void FuncFactorization ( int argc, char *argv[] )
{
int n0,n; // 整数
int ns=2; // 素因数
// 乗数
int j;
/****************/
/* 素因数分解 */
/****************/
#include <stdio.h>
#include <stdlib.h>
void DoFuncFactorization ( int argc, char *argv[] )
{
int num;
// 整数
int num_div; // 割り算した整数
int prime = 2; // 素因数
int multiple; // 乗数
n0=atoi(argv[1]);
num=atoi(argv[1]); // 整数を入力
// 因数分解
for( n=n0,ns=2; n>=ns; ns++ )
{
for( j=0; n%ns==0; j++ )
{
n /= ns;
}
if ( j==0 ) continue; // 1回も割り切れなかった
printf("素因数:%d 乗数:%d¥n",ns,j);
}
// 素因数分解
for( num_div=num,prime=2; num_div>=prime; prime++ )
{
for( multiple=0; num_div%prime==0; multiple++ )
{
num_div /= prime;
}
if ( multiple==0 ) continue; // 1回も割り切れなかった
printf("素因数:%d 乗数:%d¥n",prime,multiple);
}
}
}
XPJUG関西 / XP寺子屋
- 33. 悪い例
//----------------------------------------int FuncA( int *array, int array_cnt )
{
int idx;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
}
//----------------------------------------int FuncB( int *array1, int array_cnt1,
int *array2, int array_cnt2 )
{
int idx;
int *array;
int array_cnt;
array
= array1;
array_cnt = array_cnt1;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
array
= array2;
array_cnt = array_cnt2;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
}
XPJUG関西 / XP寺子屋
33
- 34. 改善例
//----------------------------------------int FuncA( int *array, int array_cnt )
{
int idx;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
}
//----------------------------------------int FuncB( int *array1, int array_cnt1,
int *array2, int array_cnt2 )
{
int idx;
int *array;
int array_cnt;
array
= array1;
array_cnt = array_cnt1;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
array
= array2;
array_cnt = array_cnt2;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
}
XPJUG関西 / XP寺子屋
//---------------------------------------------------------void arrayPrint( int array, int array_cnt )
{
int idx;
for( idx=0; idx<array_cnt; idx++ )
{
printf( "%d ", array[idx] );
}
printf( "¥n" );
}
//---------------------------------------------------------int FuncA( int *array, int array_cnt )
{
arrayPrint( array, array_cnt );
}
//---------------------------------------------------------int FuncB( int *array1, int array_cnt1,
int *array2, int array_cnt2 )
{
arrayPrint( array1, array_cnt1 );
arrayPrint( array2, array_cnt2 );
}
小さい関数を作る事で、再利用性が
向上します。
34
- 38. 38
改善例
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
int main(void)
{
FILE *fp;
char *fname = "test.txt";
char str[100];
fp = fopen( fname, "r" );
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
fclose( fp );
return 0;
}
XPJUG関西 / XP寺子屋
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
//------------------------------int main()
{
FILE *fp = NULL;
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
- 42. 42
悪い例
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
//------------------------------int main()
{
FILE *fp = NULL;
①
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
②
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
XPJUG関西 / XP寺子屋
③
④
①~④の順に開発する
- 43. 43
改善例
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
//------------------------------int main()
{
FILE *fp = NULL;
//------------------------------int main()
{
FILE *fp = NULL;
①
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
④
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
②
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
③
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
③
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
②
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
④
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
①
XPJUG関西 / XP寺子屋
①~③は最下層なのでどの順に開発しても良い
- 47. 47
悪い例
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
//------------------------------int main()
{
FILE *fp = NULL;
①
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
•①~④の順に開発する
•一気にテストを実施
•バグってたら修正
②
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
③
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
④
XPJUG関西 / XP寺子屋
•手短な設計
•「リファクタリング」しない
- 48. 改善例
/********************************/
/* ファイルの内容を1行づつ表示 */
/********************************/
#include <stdio.h>
//------------------------------int main()
{
FILE *fp = NULL;
•①開発
④
fp = openFile( "test.txt" ) ;
if( fp == NULL )
{
printf( "%sファイルが開けません¥n", fname );
return -1;
}
displayFile( fp );
closeFile( fp );
return 0;
}
//------------------------------FILE* openFile( char * fname)
{
return fopen( fname, "r" );
}
③
•テストパターン検討
•①開発
•main( )から①を呼び出しテスト
•①リファクタリング
•②開発
•手短な設計
//------------------------------void displayFile( FILE* fp )
{
char str[100];
printf( "¥n-- fgets() --¥n" );
while( fgets( str, 100, fp ) != NULL )
{
printf( "%s", str );
}
}
②
//------------------------------void closeFile( FILE *fp )
{
fclose( fp );
}
①
XPJUG関西 / XP寺子屋
•手短な設計
•テストパターン検討
•②開発
•main( )から②を呼び出しテスト
•②リファクタリング
48