0% found this document useful (0 votes)
31 views

Software Construction

The document summarizes software construction techniques. It discusses mapping designs to code, including defining coding standards, implementing detailed designs, performing unit testing, and releasing for integration. It also covers coding conventions, implementation strategies, attribute and association implementation, and tools like build management, revision control, and automatic testing.

Uploaded by

M Awi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Software Construction

The document summarizes software construction techniques. It discusses mapping designs to code, including defining coding standards, implementing detailed designs, performing unit testing, and releasing for integration. It also covers coding conventions, implementation strategies, attribute and association implementation, and tools like build management, revision control, and automatic testing.

Uploaded by

M Awi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

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

• An activityis not a phase


• Referring tothe construction as an activity
does not imply a distinct phase
5
Main
Activities
Software construction is more than just
programming:
• Coding and debugging • Integration Testing

• Unit Testing • Construction Planning


• Integration •
Implementatio
6 n modeling
Construction Technologies

7
Automatic Code Generation

• Code generate (partof) the code from the design


model.

• Several tools available: Xtend, Acceleo, etc.

8
Design by Contract
• Design-by-Contract

• Specification of class invariants and pre and


post conditions of methods.

• Eiffel, JML, jContract.

• Assertive programming

• Use of assertions inside methods.

• Test-driven development 9
Assertion
s
• Ensure the state of • Improve error
variables localization

• Improve code • Can be


readability disabled

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.

• Building software is not only compilation:


dependency management, code generation, unit
testing, configurations, etc.

• Maven, Gradle (Java), Grails (Ruby), Gem


(Javascript), etc. 11
Revision Control

• Revision control tracks and controls changes in the


source code.

• If something goes wrong, it can determine


what was changed and by who.

• CVS, SVN, Git, Mercurial, etc.


12
Automatic
Testing
• Automatic execution of unit tests:

• Shortens time to defect


detection

• Increases personal discipline

• Avoids regression

• Test-driven development
13
Code Refactoring

• Refactoring improves the design of existing code


without changing its behavior

• Examples: change of a class name, creation of


an abstract class, extraction of the interface of
class, etc.

14
Reverse
Engineering

• Legacy code (or other software) may be an


important source of information

• Automatic analysis/modification of legacy


code: MoDisco, Spoon, etc.

15
StackOverflow-
based
• Development
Discussion groups (from Usenet to Stack Overflow) are
an important source of information:

• code excerpts

• advantages and drawbacks

• alternative solutions

• coding tips 16
Continuous Integration

17
Construction Tools
• Programming
languages:

• GPL: Java,
Scala, C++

• DSL: Protobuf, Haxe

• IDE: Eclipse,
NetBeans, IntelliJ

• Build: Ant, Maven,


Gradle 18
MappingDesigns to Code

19
Roadma
p
1. Identifycoding standards

2. Identifyan implementation strategy

3. Implement detailed design

4. Perform unit test

5. Release for integration


20
Implement detailed
design
1.For each component:

1.Implement component

2.Implement unit test

3.For each class in component:

1.Implement class

2.Implement unit test


21
Coding Conventions
• Generic conventions (for Java):

• Sun/Oracle Code
Conventions

• Google Java Style Guide

• Project specific:

• GNU Coding Standards


22
Implementation
Strategy
• UML lacks semantics: there is no universal rule
for mapping design to code

• The strategy specifies how to do it.

• Different parts: components, classes,


attributes, operations, state charts, etc.
23
Type Correspondance Table
UML Java MySQL TypeScript

Integer java.lang.Integer BIGINT number

Another approach: add new datatypes to UML

24
Attribute Implementation
HTML Page
+ title : String {readOnly}
+ /size : Integer
~ version : Integer
# contents :
String
- visibility :
Boolean = true
- tags : String
[1..5]
+ render()
+ save()
- optimize()

1 public class HTMLPage {


2 private final String title;
3 private Integer version;
4 private Stringcontents;
5 private visibility = new Boolean(true);
6 Boolean
List<String> tags = new ArrayList();
private 7
}

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

For each class, create a pair class,


interface 28
Class
Implementation
Design Implementation
Model Model
HTMLPageImpl
Regenerated
HTML Page

HTMLPageUser

User modified

Implementation Gap Pattern


29
Minimal
Interface

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:

• Only the interfaces are public.

• The component inner classes should not be


visible (outside their package).
39
Coding
Tips

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

• The parameters that were passed in to the method

• Any objects created by the class

• Any attribute referenced object

• Global variables
44
Learn Code
Smells

• Code smells are “warnings signs” of deeper


problems:

• God classes, feature envy, long methods,


too manyparameters, nested if statements,
etc.
45
Think Globally, Program
Locally
• Make variables as local as possible and as invisible
as possible

• If you have the choice between protected or private


visibility, choose private.

• Making attributes protected gives objects of


subclasses access to attributes of their super-classe.
46
Depend on Interfaces, not on
Implementations

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

• Code construction is not only


implementation.

• It is not a phase of the development process.

48
References

• “Code Complete”. Steve McConnell. 2nd


edition. Microsoft Press.

49

You might also like