More Related Content
PDF
10分で分かるr言語入門ver2.10 14 1101 PDF
PDF
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014 PDF
PDF
20130716 はじパタ3章前半 ベイズの識別規則 PDF
PPTX
PPTX
What's hot
PPTX
PDF
PDF
最近のRのランダムフォレストパッケージ -ranger/Rborist- PDF
PDF
PPTX
Feature Selection with R / in JP PDF
R入門(dplyrでデータ加工)-TokyoR42 PDF
PDF
PDF
PDF
PDF
PDF
ODP
PDF
Tokyor60 r data_science_part1 PDF
10分で分かるr言語入門ver2.9 14 0920 PDF
TensorFlow を使った機械学習ことはじめ (GDG京都 機械学習勉強会) PDF
PPTX
PDF
Viewers also liked
PDF
PDF
PDF
PDF
PPTX
PPT
PDF
デザインパターン(state,strategy,template) PPT
PPT
PDF
PDF
PDF
PDF
PPT
PDF
DevLOVE関西2012 B-2「メンバーの行動が激変!「ペアふりかえり」ワークショップ PPT
お客様へ価値を届け続けるために~継続的デリバリーの活用~ PDF
デブサミ2011(17-B-3)講演資料「チケット駆動開発~タスクマネジメントからAgile開発へ」 PDF
PDF
GoF のデザインパターンじゃないけど、よくあるパターン PDF
Email Deliverability Guide - メールを確実に届けるために Similar to XP寺子屋第9回「シンプル・プログラミング」
PDF
PDF
PDF
PDF
PDF
PDF
PDF
競技プログラミングにおけるコードの書き方とその利便性 PDF
PDF
PDF
KEY
Algebraic DP: 動的計画法を書きやすく PDF
KEY
Clojure programming-chapter-2 PDF
関数型都市忘年会『はじめての函数型プログラミング』 PDF
Unity2015_No10_~UGUI&Audio~ PPTX
Ocaml lecture slides 01 at axsh PDF
PDF
PDF
KEY
More from takepu
PDF
PPT
PPT
Xp寺子屋出張版#2「ペアワークの楽しさ実感!ペアドローワークショップ」 PDF
PDF
PDF
PDF
PDF
PPT
もえる!えっくす・ぴぃ入門第1回「えっくす・ぴぃの歴史」 PDF
Lt「5分で分かる!e xtremeprogramming」.ppt PDF
PDF
PDF
PPT
PPT
劇的改善!ペアふりかえり」 Before→After XP寺子屋第9回「シンプル・プログラミング」
- 1.
- 2.
- 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寺子屋
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 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寺子屋
- 23.
- 24.
- 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寺子屋
- 27.
- 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寺子屋
- 30.
- 31.
- 32.
- 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
- 35.
- 36.
- 37.
- 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 );
}
- 39.
- 40.
- 41.
- 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寺子屋
①~③は最下層なのでどの順に開発しても良い
- 44.
- 45.
- 46.
- 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
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.