13. Misc topics
Today's topics
Extreme programming
Human as programming resource
Callback programming and thread programming
Extreme Programming (XP)
Proposed by Kent Beck
Practical programming method
Kent Beck
Object-oriented programming professional
Author of Smalltalk books
http://gyazo.com/90e878e7e3991568c575010daaa2c06a.png
Basics
Communication
Simplicity
Feedback
Courage
http://gyazo.com/fa758d64d0dca5d8c0f8bcc1813b5c41.png
XP practices
Repetitive development
Design / implementation / test for each iteration
Release partially-completed producdt
Unit-based implementation
Repetition of code modification and testing
XP practices (Cont'd)
Use common words
Open development space
Easy conversation
Development practice
Test-driven development
Pair programming
Refactoring
Source code sharing
Continuous integration tests
YAGNI
Test-driven development
Write test codes before writing codes
Goal is to write codes which satisfy the tests
Clear goal and simple implementation
Pair programming
https://gyazo.com/735c1c0f9df0486fc392e9d22a0089a3.png
Two person share a terminal and write code
One writes the code
The other check the coding and navigate
Roles changes
Always codes are checked
Keep concenterated
Good for code sharing
Refactoring
Modify working codes
Change codes without changing specs
Strict test environment necessary
Sharing codes
Anyone in the team can modify codes
Everyone is responsible for the codes
Continuous integration (CI)
Always run integration tests after unit tests
Integration test at least onece a day
CI tools
Travis CI
Circle CI
Jenkins
YAGNI
You Aren't Going to Need It
Implement the minimal code
Don't implement features for the future
Implement only necessary codes
Easy to adapt to future spec changes
Programmers and XP
Many programmers liked the idea
Source codes are more important than documents
Small improvements more important than long-term visions
Test-first programming is important
Program testing
http://gyazo.com/5e7a7dba9271d9217245911b716ceb45.png
Test-first programming
Write test codes at the beginning
Write codes after writing test codes
Go to next step if test was okay
Test-Driven Development (TDD)
Test-Driven Development
1. Write test codes before production
2. Write codes which satisfy test conditions
3. Modify codes and repet checking (Refactoring)
4. GOTO 1
Caveats
Programmers may write insufficient tests
Testers and programmers should be different
Test systems
Unit test frameworks
SUnit (Smalltalk)
JUnit (Java)
RubyUnit (Ruby)
RubyUnit
Test system for Ruby
Use assertions
Initial operations
Termination operations
RubyUnit example
code:test.rb
require 'test/unit'
class ArrayTest < Test::Unit::TestCase
def setup
@empty_array = []
end
def test_empty?
assert(@empty_array.empty?)
end
def test_size
assert_equal(0, @empty_array.size)
end
def teardown
@empty_array = nil
end
end
RSpec example
code:rspec.rb
describe Array, "when empty" do
before do
@empty_array = []
end
it "should be empty" do
@empty_array.should be_empty
end
it "should size 0" do
@empty_array.size.should == 0
end
after do
@empty_array = nil
end
end
JUnit example
code:junit.java
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import com.pitecan.gyaim.GoogleIME;
import com.pitecan.gyaim.Message;
public class GoogleIMETest {
String 変換例リスト[][] = {
{"べんきょう", "勉強"},
{"ますい", "増井"},
{"いちのもと", "櫟本"},
{"むさしぼうべんけい","武蔵坊弁慶"},
{"かまくらのかいがん", "鎌倉の海岸"}
};
@Test
public void Google変換チェック() {
for (String[] 変換例: 変換例リスト) {
boolean found = false;
for(String word: GoogleIME.convert(変換例0)){ if(word.equals(変換例1)) found = true; }
assertTrue(found);
}
}
}
Demo: Running tests for Gyaim
Pair programming
Two programmers use one computer
Develop a software together
Complementary roles
e.g. writing test codes and implementation
Advantages
Difficult to be idle
Higher quality expected than single person
Work flow change
Not worry about what to do next
Become tolerant to noises
Changing roles => changing thinking patterns
Advantages (Cont'd)
Some people like working together
Sharing codes
Education effect
Knowledges easily share bay the team
Teamwork
Less interruption
Difficult to interrupt two working person
Less number of computers
Results
Accuracy imprved 15%
Speed improved 20-40%
Total improvement 15-60%
...
Problems
Boring for experienced programmers
Some people hate working together
Efficiency not always improved
Talent unbalance
Coding style conflict
Scheduling
Remote working impossible
Problems
Difficult to have rests
Get tired
Refactoring
Changing codes with same spec
No rigid definition
Refactoring techniques
Extract methods and reuse
Change bidirectional relations to unidirectional
Create small classes
Change switch statement to polymorphism
Clean up class members
Refactoring techniques
Replace subclass with delegation
Encapsulate Downcast
Convert constructors to factory methods
Use objects for arguments
Change names and attributes
70 refactoring methods
http://gyazo.com/ffd3c3be1c703b3daed32ad82e4a451f.png
Test should be complete
Changing code is okay as long as testing done
Design pattern
Catalog of software know-how
"Gang of Four" book
23 patterns listed
http://gyazo.com/c1541545f2bb32608bc10b048075f8c1.png
Patterns on generation
Abstract Factory パターン
関連する一連のインスタンスを状況に応じて適切に生成する方法を提供
Builder パターン
複合化されたインスタンスの生成過程を隠蔽
Factory Method パターン
実際に生成されるインスタンスに依存しない、インスタンスの生成方法を提供
Prototype パターン
同様のインスタンスを生成するために、原型のインスタンスを複製
Singleton パターン
あるクラスについて、インスタンスが単一であることを保証
Patterns on structure
Adapter パターン
元々関連性のない2つのクラスを接続するクラスを作る
Bridge パターン
クラスなどの実装と、呼出し側の間の橋渡しをするクラスを用意し、実装を隠蔽
Composite パターン
再帰的な構造を表現
Decorator パターン
あるインスタンスに対し、動的に付加機能を追加する。Filterとも呼ばれる。
Facade パターン
複数のサブシステムの窓口となる共通のインタフェースを提供
Flyweight パターン
多数のインスタンスを共有し、インスタンスの構築のための負荷を減らす
Proxy パターン
共通のインタフェースをもつインスタンスを内包し、利用者からのアクセスを代理する。Wrapperとも呼ばれる。
Patterns on behavior
Chain of Responsibility パターン
イベントの送受信を行う複数のオブジェクトを鎖状につなぎ、それらの間をイベントが渡されてゆくようにする。
Command パターン
複数の異なる操作について、それぞれに対応するオブジェクトを用意し、オブジェクトを切り替えることで操作の切替えを実現する。
Interpreter パターン
構文解析のために、文法規則を反映するクラス構造を作る。
Iterator パターン
複数の要素を内包するオブジェクトのすべての要素に順にアクセスする方法を提供する。反復子。
Mediator パターン
オブジェクト間の相互作用を仲介するオブジェクトを定義し、オブジェクト間の結合度を低くする。
Memento パターン
データ構造に対する一連の操作のそれぞれを記録しておき、以前の状態の復帰または操作の再現が行えるようにする。
Observer パターン
インスタンスの変化を他のインスタンスから監視できるようにする。Listenerとも呼ばれる。
State パターン
オブジェクトの状態を変化させることで、処理内容を変えられるようにする。
Strategy パターン
データ構造に対して適用する一連のアルゴリズムをカプセル化し、アルゴリズムの切替えを容易にする。
Template Method パターン
あるアルゴリズムの途中経過で必要な処理を抽象メソッドに委ね、その実装を変えることで処理が変えられるようにする。
Visitor パターン
データ構造を保持するクラスと、それに対して処理を行うクラスを分離する。
XP and pattern language
Close relationship
Kent Beck invented XP and pattern language
Christopher Alexander
Invented "Pattern Language"
http://gyazo.com/835dfec1ee8d66545ff1cd4933cfe488.png]
Ward Canninghum
Invented Wiki Wiki Web
Applied pattern language to software
http://gyazo.com/266865260d7551358e88687c2b6e03be.png
Human-powered programming
Human as programming resource
Human power is the ultimate computing resource
Some problems are solved only by humans
Which restaurant is the best
Interesting books?
Using human power for programming
Pair programming
Programming collective intelligence
Merging human power and computing power
Web = platform for using human power
Super parallel computingn by humans
Ultimate computing resource
Explicit human programming
Want to calculate complicated things
⇒ Create a service for gathering info
⇒ Get info from gathered data
Examples of human-powered calculation
Find a good restaurant
Find a influential person
Calculate the beauty of a lady
...
Calculation without notice
People often do something without notice
Boast about something
Correct something
Calculation with joy
Wikipedia
Hotornot
Kotonoha
reCAPTCHA
Wikipedia
World-biggest human calculation project
Calculate how beautiful a person is
http://gyazo.com/2dc0aa135251d5ad4f74c1b9a610c2f2.png
Simple inquiry
http://gyazo.com/d9f0658089b8e185d3005eca5d9f90b4.png
CAPTCHA
Prove that the user is a human
http://gyazo.com/d200e187b92f98e4d639ed8d23d5203f.png
reCAPTCHA
Authentication and character recognition
Not really interesting, though
http://gyazo.com/7c26209860f904a1feff6ca10d0aa073.png
Fixing something without notice
Correcting something if inappropriate
Picking up trash
Power of Low Standards
http://gyazo.com/c9147cba2b040dd25331909d064c0fa5.png
Location registration
Feel uneasy when registration is wrong
http://gyazo.com/bb7642f350fff42abb9f7a61bf31657b.png
After correction
Correct location registered
http://gyazo.com/61289b265ca621d8f77db568c2110c81.png
Feeling uneasy if text is incorrect
Want to fix it
Using curiocity
Mutual proofreading
http://gyazo.com/aa5264fa4e48875c793a1cf9b26e0379.png
Diary example
http://gyazo.com/97901c841fc693ae6d51ce1bac74d54b.png
BabaScript
Explicit human programming
Treat computers and humans the same way
Humans work as computing resources
List of instructions
Sample instruction
http://gyazo.com/f7cb9135ecf7e4bf40aaa43d5e4c724f.png
Script for creating pasta
Instructions for humans are written in JavaScript
http://gyazo.com/af526e4b31557f9b331679fe46eff8fe.png
Application
http://gyazo.com/e6e0d85e04cf4f6e6ed94cccd49d3de0.png
Receiving a request
Select one and reply
http://gyazo.com/58b715a4ed9d9306d78fe34930bc047c.png
Data mining from shared data
Programming collective intelligence
http://gyazo.com/3cfa17df5e77ccc67b7bd5f8f6a8279f.png
Programming collective intelligence
Recommendation systems
Clustering
Decision tree
SVM
Recommendation systems
Collaborative Filtering
e.g. Firefly
Firefly
Register musics you like
Recommend musics based on the data
Registration takes time
Last.fm
Preferences accumulated
No need for manual registration
Extract information from hondana.org data
Invoke methods in Japanese
Various calculation
Visualization by Hayashi's quantification method
Callback programming
Asynchronous programming
Became popular on JavaScript
Run heavy process later
Normal programming
code:funcs.js
func1();
func2();
func3();
Execution stops if func2() is heavy and slow
Browser stalls
Solutions
Parallel programming
Callback programming
Example of callback programming
code:callback.js
func1();
func2(arg1,arg2,function(){
// heavy or slow operations
// e.g. communication with slow server
console.log("end")
});
func3();
func2() finishes immediately
Calculation in background
"end" printed after calculation
Advantages of callback programming
Users should not wait termination
Pseudo parallel programming
Disadvantages of callback programming
Difficult to understand
Process ordering not guaranteed
Guaranteed execution order
Callback nesting
Difficult to understand deep nesting
"Callback hell"
code:async1.js
setTimeout(function(){
console.log('First');
setTimeout(function(){
console.log('Second');
setTimeout(function(){
console.log('Third');
},100);
},500);
},1000);
Guaranteed execution order
Use special libraries
async.js
Parallel/sequential execution
Async library
code:async2.js
var async = require('async');
async.series([
function (callback) {
console.log('series 1');
setTimeout(function () {
console.log('series 1 done.');
callback(null, 1);
}, 500);
},
function (callback) {
console.log('series 2');
setTimeout(function () {
console.log('series 2 done.');
callback(null, 2);
}, 300);
},
function (callback) {
console.log('series 3');
setTimeout(function () {
console.log('series 3 done.');
callback(null, 3);
}, 100);
}
], function (err, results) {
if (err) { throw err; }
console.log('series all done. ' + results);
});
Problems of using libraries
Extra specification
Difficult to handle errors
Explicit parallelism might be better
Promise and async-await
New feature of JavaScript
Handle asynchronus processing easily
Promise
Abstraction of asynchronous operation
Conventional callback
code:callback.js
getAsync("fileA.txt", function(error, result){
if(error){
// do error handling
throw error;
}
// process the result
});
code:promise.js
var promise = getAsyncPromise("fileA.txt");
promise.then(function(result){
// process the result
}).catch(function(error){
// error handling
});
Async-await
Use Promise for synchronous operations
code:async.js
function openFile(url) {
const p = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.addEventListener('load', (e) => resolve(xhr));
xhr.send();
});
return p;
}
async function loadAllFiles() {
const xhr1 = await openFile('foo.txt');
const xhr2 = await openFile('bar.txt');
const xhr3 = await openFile('baz.txt');
console.log('done!');
}
loadAllFiles();
Future of parallel programming
Semaphores, monitors are not convenient
Callback programming is difficult
History of loop processing
Old loop programs
Many GOTO statements
"spaghetti programming"
http://gyazo.com/ba6a0d78000d76428b935d3a1d63c4af.png
Criticism on GOTO statement
Proposed 'structured programing'
Structured programming
Restrict program structure
while, if, etc.
GOTO considered harmful
NS chart
http://gyazo.com/82bd29da9867ec20105a3d7de374441d.png
PAD
http://gyazo.com/127b8cf6de5ffa9915efb63ba2a95520.png
http://gyazo.com/7e50d657a9eade1a7af627a4315fd9c0.png
HCP chart
http://gyazo.com/b220896bd7c83977b8a3afa3bd2fdc37.png
Invention of for statement
for(init;cond;change)
Intruduced in C
Everything related to loop is in one line
Less bugs
XXcharts became extinct
e.g. while statement
code:while.txt
init
while(condition){
(Do something)
change values
}
Removing loops
map function
Apply a function to all elements
No loop statment
map function
code:map.rb
map.rb
puts s
}
Object-oriented programming
History of object-oriented programming
Simula
Smalltalk
C with classes
C++
Objective-C
Modula2
Eiffel
History of objected-oriented programing
Popular since 1980s
Common knowledge these days
Simula
Simulation language
Information hiding
Smalltalk
Developed at PARC
Integration with GUI
ALTO = GUI + OO + network
C++
"C with classes"
Was a front-end processor for C
Very popular
Objective-C
Developed by Brad Cox
http://www.amazon.com/dp/0201548348/ http://gyazo.com/f1929fecb1b6ec2f3e36a10633cbda1c.png
Objective-C
Smalltalk-like extention of C
Used for NeXT workstation
Used for Apple products
Modula2
Developed by DEC
Eiffel
Bertrand Myer
http://gyazo.com/04acd02b4fb4ea7e46c55c3a64c945b3.png
Method call and member access looks the same
Adopted by various recent languages
JavaScript
Prototype-based OO
Widely used recently
Many "ALT-JS"
CoffeeScript
Typescript
Prototype-based OO
Use existing object as 'prototype'
Proposed in the 80s
Essence of OO
Inheritance?
Information hiding?
Data sharing?
Mapping between real objects and computer objects?
Distributed communication?
Essence of OO
Technique for making programs shorter (Prof. Takeuchi)
Is OO really needed?
OO-like programming possible on C
Not as important as structured programming and recursion?
Unix kernel is not implemented in OO language
Compilers are not implemented in OO language
OO thinking
File system and OO
Japanese and OO
File system and OO
No distinction between file and I/O device
/home/masui/xxx (file) and /dev/xxxx (I/O) are treated as the same
Article by Nakajima-san
"Salt to me"
Not "pass me the salt"
Merits of postfix notation
Calculating $ log(1/x) (log of reciprocal)
X.inv.log
log(inv(X))
Conclusions
Various programming pradigms
Enjoy programming anywhere!