Upload
juin-der-lee
View
41
Download
1
Embed Size (px)
Citation preview
Introduction
◆Why this topic
1.Trend:
a. Propose new algorithm together with code.(Jagathe)
b. Collect related algorithms into single software package.
(SPM, EEGLAB, FMRLAB…)
2. Program is equally important as algorithm. (Incorrect program produces incorrect result no matter how good the algorithm is.)
1. Our clients: doctors, psychologists, experimenter.
For them, though algorithm accuracy is important, program availability is more important. (In my point of view, that’s why most people use AIR to perform registration, though AIR may not be the most accurate algorithm.) They don’t want to waste time coding.
2. Internet makes resource sharing become easy.
3. Open software.(Linux)
Why this trend
Lab 1Implement 1 …
Stat sinica Algorithm Implement s (program)
Lab 2Implement 2
Lab nImplement n
Everybody codes
Lab 1Implement s …
Stat sinica Algorithm Implement s
Lab 2Implement s
Lab nImplement s
Save them from whole night coding
Lab 1Implement s
Implement s…
Stat sinica Algorithm Implement s
Lab 2Implement x
Lab nImplement s
UCSD Algorithm Implement x
…
They could have multiple choices
Conclusion
• Program is very important.
• We should pay more attention on programming.
• Improve our programming skills.
Programming Principles
• Correct(正確性)• Readability (可讀性)• Easy to modify (易於修改)• Reusability (可重用性)• Extendibility (擴充性)• Efficiency (效率)• Memory (所需記憶體的大小)• Code size (執行檔的大小)• Portability (可移植性)
………….
if( ischecked( orientU) ) VOL_ORIENT=OBLIQUE;
if( ischecked( orientS) ) VOL_ORIENT=SAGITTAL;
if( ischecked( orientA) ) {VOL_ORIENT=AXIAL;NEURO_AXIAL=False;}
if( ischecked( orientNA)) {VOL_ORIENT=AXIAL;NEURO_AXIAL=True;}
if( ischecked( orientC) ) {VOL_ORIENT=CORONAL;GE_COR=False;}
if( ischecked( orientG) ) {VOL_ORIENT=CORONAL;GE_COR=True;}
…………...
Bad Style
Code pieces from Mri3dX:
fuzzy logic!
………….
if(ischecked(orientU)) VOL_ORIENT=OBLIQUE;
else if(ischecked(orientS)) VOL_ORIENT=SAGITTAL;
else if(ischecked(orientA)) {VOL_ORIENT=AXIAL;NEURO_AXIAL=False;}
else if(ischecked(orientNA)) {VOL_ORIENT=AXIAL;NEURO_AXIAL=True;}
else if(ischecked(orientC)) {VOL_ORIENT=CORONAL;GE_COR=False;}
else if(ischecked(orientG)) {VOL_ORIENT=CORONAL;GE_COR=True;}
…………...
Good Style
After modification:
Use if-else-if statement to reveal the exclusive property of each situation.
Bad Style
function f(a,b,c)
%set default valueif( nargin ==0) a=1; b=2; c=3;end
if( nargin ==1) b=2; c=3;End
if( nargin ==2) c=3;end
A common matlab function:
1. Redundant test.
2. Have to take lots effort to modify (what if change default value of c to be 4?)
3. Redundant code.
Better Style
function f(a,b,c)
%set default value
if( nargin ==0)
a=1;
b=2;
c=3;
elseif( nargin ==1)
b=2;
c=3;
elseif( nargin ==2)
c=3;
end
A common matlab function:
1. Redundant test.
2. Have to take lots effort to modify (what if change default value of c to be 4?)
3. Redundant code.
Better Style
function f(a,b,c)
Default-a-value=1;
default-b-value=2;
default-c-value=3;
%set default value
if( nargin ==0)
a = default-a-value;
b = default-a-value;
c = default-a-value;
elseif( nargin ==1)
b = default-a-value;
c = default-a-value;
elseif( nargin ==2)
c = default-a-value;
end
1. Redundant test.
2. Have to take lots effort to modify (what if change default value of c to be 4?)
3. Redundant code.
Better Style
function f(a,b,c)
default-a-value=1;
default-b-value=2;
default-c-value=3;
%set default value
if( nargin <1) a=default-a-value; end
if( nargin <2) b=default-a-value; end
if( nargin <3) c=default-a-value; end
1. Redundant test.
2. Have to take lots effort to modify (what if change default value of c to be 4?)
3. Redundant code.
Simple is beautiful!
Use named constant to improve readability and make code easy to modify(avoid magic number)
fid = fopen(‘a.ima’,’rb’);
%skip header
fseek(fid,6144,’bof’);
%start reading data
…
What is 6144?
Magic number
fid = fopen(‘a.ima’,’rb’);
%skip header
%6144: header size
fseek(fid,6144,’bof’);
%start reading data
…
What if header size is changed?
Use named constant to improve readability and make code easy to modify(avoid magic number)
fid = fopen(‘a.ima’,’rb’);
%skip header
header_size=6144;
fseek(fid,header_size,’bof’);
%start reading data
…
What if header size is changed?
named constant
Use named constant to improve readability and make code easy to modify(avoid magic number)
Named constant not only plays the role of comment but also makes future modification easier.
Improve efficiency by simple rearrangement of code
% find how many boys and girls are in the class
for i=1:NumStudent,
if( student(i).sexual==‘Boy’)
NumBoy = NumBoy + 1;
elseif( student(i).sexual==‘Girl’)
NumGirl = NumGirl + 1;
end
end
Boy: 1 test + 1 assignment
Girl: 2 tests + 1 assignment
50 students:
1. 40 boys, 10 girls:
60 tests + 50 assignments
2. 10 boys, 40 girls:
90 tests + 50 assignments
Put more frequency situation in the first if statement.
Programming Principles
• Correct(正確性)• Readability (可讀性)• Easy to modify (易於修改)• Reusability (可重用性)• Extendibility (擴充性)• Efficiency (效率)• Memory (所需記憶體的大小)• Code size (執行檔的大小)• Portability (可移植性)