Software Construction
Software Construction
Mapping Design to
Code
1
Pla
n
• Introduction
• M a pping
designs to code
• Conclusion
2
Introduction
3
Definitio
n
«Code Construction is the detailed creation of working meaningful
software through a combination of coding, verification, unit testing,
integration testing, and debugging.» [SWEBOK]
4
Software Construction
AD Construction
Requirement Software
Software
Construction
Specification
Design Model
7
Automatic Code Generation
8
Design by Contract
• Design-by-Contract
• Assertive programming
• Test-driven development 9
Assertion
s
• Ensure the state of • Improve error
variables localization
1 class Assertion {
2 public double calculate(int x, int y)
{ 3 assertThat(x).isNotZero();
4 assertThat(y).isLessThanOrEqualTo(10);
5 // (...)
6 }
Assertions with AssertJ
10
Build
Management
• Build is the task that generates executable software
from source code.
• Avoids regression
• Test-driven development
13
Code Refactoring
14
Reverse
Engineering
15
StackOverflow-
based
• Development
Discussion groups (from Usenet to Stack Overflow) are
an important source of information:
• code excerpts
• alternative solutions
• coding tips 16
Continuous Integration
17
Construction Tools
• Programming
languages:
• GPL: Java,
Scala, C++
• IDE: Eclipse,
NetBeans, IntelliJ
19
Roadma
p
1. Identifycoding standards
1.Implement component
1.Implement class
• Sun/Oracle Code
Conventions
• Project specific:
24
Attribute Implementation
HTML Page
+ title : String {readOnly}
+ /size : Integer
~ version : Integer
# contents :
String
- visibility :
Boolean = true
- tags : String
[1..5]
+ render()
+ save()
- optimize()
25
Attribute Accessors
1 public class HTMLPage {
2 public String getTitle()
{ 3 return title;
4 }
5 publicInteger getSize() { HTML Page
6 return + title : String {readOnly}
contents.size(); 7 } + /size : Integer
~ version : Integer
8 Integer getVersion() { # contents :
9 return version; String
- visibility :
10 } Boolean = true
11 void - tags : String
[1..5]
setVersion(Integer + render()
aVersion) { + save()
- optimize()
12 version =
aVersion; 13 }
14 // (...)
15 }
26
Reflexive
Accessors
1 interface ReflexiveAccessors {
2 public void set(String anAttr, Object value);
3 Object get(String anAttr);
public 4
}
27
Class
Implementation
Design Model Implementation
Model
HTMLPage
HTML Page
HTMLPageImpl
HTMLPageUser
User modified
Common
1 interface Common {
2 Common copy();
3 Common deepCopy();
4 boolean equals(Common);
5 String
toString(); 6 }
HTMLPage
30
Association
Implementation
1 interface Folder {
2 FileList
getFiles(); 3 }
4
Folder 5 class FileList implements List<File> {
6 add(File) {...};
1
7 {...};
remove(File) 8
}
* 1 interface File {
File 2 Folder getFolder();
3 void setFolder(Folder aFolder);
4}
31
Handshake Problem
project : Folder readme : File
archive :
doc : File
Folder
doc.setFolder(archive) OR
archive.getFiles().add(doc)
project : Folder readme : File project : Folder readme : File
archive : archive :
doc : File doc : File
Folder Folder
32
Alternative Implementation
[Harrison et al. 2000]
1 interface Folder {
Folder 2 getFiles();
FileIterator 3
1 }
4 extends Iterator, File {
5 interface
FileIterator
6 remove();
*
7
File
insert(File);
8 next();
9 /* File methods /*
10 }
33
1 interface File {
2 FolderIterator
Folder getFolder(); 3 }
1 4
5 interface FolderIteratorextends Iterator,
6 Folder {
7 remove();
8 insert(Folder);
* 9 next();
File 10 /* Folder methods /*
11 }
34
Class-
Association
User Workstation
name : String
* authorization on * name : String
profile station shutdown()
outForCoffee()
Authorization
priority
shell
35
1 class AuthorizationFactory {
2 private static
AuthorizationFactory 3
instance = new AuthorizationFactory();
4 private AuthorizationFactory() {}
5 public static AuthorizationFactory
6 getInstance() {
7 return
instance; 8 }
9 Authorization link(Workstation w, User u) {
10 Authorization a = new AuthorizationImpl();
11 a.setStation(w);
12 a.setUser(u);
13 }
14 }
User Workstation
name : String
* authorization on * name : String
outForCoffee() profile station shutdown()
Authorization
priority
shell
36
1 interface StationListIterator
2 extends Workstation, Authorization {
3 void next();
4 void remove();
5 insert(Workstation);
void 6
}
User Workstation
name : String
* authorization on * name : String
outForCoffee() profile station shutdown()
Authorization
priority
shell
37
Component
Implementation
Antonyms
Synonyms
«component»
Dictionary
38
Component
Implementation
Antonyms
Synonyms
«component»
Dictionary
• General idea:
40
Avoid Repetition
41
Avoid
Complexity
float InvSqrt (float x){
float xhalf =
int0.5f*x;
i =
i *(int*)&x;
= 0x5f3759df -
(i>>1);
x = *(float*)&i;
x = x*(1.5f -
return xhalf*x*x);
}
x;
The inverse square root implementation in Quake 3
http://www.gamedev.net
42
Avoid Unnecessary Optimizations
• Profile
first!
43
Respect the Law of Demeter
• Any method of a class should call only methods belonging
to:
• Its class
• Global variables
44
Learn Code
Smells
1 class Dependency {
2 Collection<String> names = new LinkedList<
3 String>();
4 Collection<String> getNames(){}
5 void addMoreNames(Collection<String> others)
{} 6 }
47
Conclusio
n
48
References
49