CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

55
CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses

Transcript of CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

Page 1: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

CS5103 Software

Engineering

Lecture 13Software Refactoring

Software Licenses

Page 2: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

2

Last class

Issue tracking system Type of issues Process of issues Resolution of issues

Coding styles Code

Variables and constants

Expressions

Statements and Lines

Blocks

Methods and Classes

Page 3: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

3

Today’s class

API comments and Documentation Javadoc

Software refactoring Why software refactoring? Types of refactoring Tool supports Behind refactoring tools

Software license Proprietary licenses Open source software licenses

Page 4: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

4

Coding styles

Variable/Constant Meaningful name, follow conventions, externalize

constants

Expression Break complex ones, avoid corner cases, more brackets

Statements/line One statement per line, break long lines

Blocks Logic blocks, Space indent, Curly brace for singletons, No

copy-paste

Methods / Classes Break to pieces when too large, smaller method

signatures, follow common structures

Page 5: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

5

JavaDoc A JavaDoc Example

Ant 1.6.5

http://api.dpml.net/ant/1.6.5/overview-summary.html

Page 6: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

6

JavaDoc Structure

Overview

Packages

Classes / Interfaces / Exceptions

Fields / Constructors / Methods Hierarchy

Deprecated

Index

Page 7: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

7

JavaDoc-Tags Tags are meta-data put in the comments to

guide Javadoc in document generation

Starting with “@” @author : author of the class

@version: current version of the software

@since: the software version when the class is added

@see: a link to another class / method / …

@param: parameters of a method

@return: return value of a method

@exception: exceptions thrown from a method

@deprecated: an outdated method

Page 8: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

8

Javadoc: Mapping Commenting methods for documentation

/** * Constructs a WordTree node, specify its * data with the parameter word, both children * of the WordTree node are null * * @param word the string value stored as the data of the node, cannot be null * @exception NullWordException an exception raised when the parameter word is null */ public WordTree(String word) throws NullWordException{ ... /** * fetch the data of current WordTree node * @return the data stored at current node */ public String getData() ...

Page 9: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

9

Javadoc: Mapping Javadoc generated

Page 10: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

10

JavaDoc Demo for generating Javadoc in eclipse

Right click on your project Goto Export…, and choose Javadoc You will need a Java SDK (not only JRE) to run

Javadoc Set the Javadoc command to the Javadoc

executable Set the destination to the place Click on finish

Page 11: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

11

Today’s class

API comments and Documentation Javadoc

Software refactoring Why software refactoring? Types of refactoring Tool supports Behind refactoring tools

Software license Proprietary licenses Open source software licenses

Page 12: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

12

Software Refactoring, why?

So far we talked about issues to be considered when design and implementing software Design patterns

Coding Styles

Considering these, you can try to find a best implementation for the current requirements

But requirements change quickly…

Page 13: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

13

Software Refactoring

Definition: Software refactoring the process of restructuring a

software to improve its readability or extensibility, while keeping its behavior Restructuring

Keeping its behavior

book

Refactoring- Improving the Design of Existing Code, Addison Wesley, 1999. Martin Fowler et al.

Page 14: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

14

When to apply refactorings

Bad smells Code duplication Long method / Huge Class Long parameter list ….

As the software evolve, these smells automatically appears

Especially when you do extreme programming, which tries to ignore design issues at the beginning

Page 15: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

15

Refactoring for design patterns

Consider the design patterns we learned Composite Pattern

At the beginning, maybe simple document hierarchy (doc, line, glyph), no plan for changing the hierarchy

So fine without composite pattern

Factory Pattern Support only one style at beginning

So fine without factory pattern

Visitor Pattern Support only spell checking at beginning So no need to bother for visitor patterns

Page 16: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

16

Types of refactorings

Add/Remove methods / fields

Move methods / fields

Extract methods / interface

Rename

Replace temp with query

Pull up / Push down methods / fields

Page 17: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

17

An example from Martin Fowler’s book

A program to calculate the bill of a video-store customer

The total amount depends on the movie renting prices, the number of movies rented and the renting dates

Class diagram

Page 18: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

Example: Movie classpublic class Movie {

private String title;private int priceCode;

public Movie(String title, int priceCode) {this.title=title;this.priceCode = priceCode;

}public String getTitle() {

return this.title;}public int getPriceCode() {

return this.priceCode;}

public void setPriceCode(int priceCode) {this.priceCode = priceCode;

}}

Page 19: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

Example: Rental classpublic class RentalRental {

private Movie movie;private int rentDay;

public Rental(Movie movie, int rentDay) {this.movie = movie;this.rentDay = rentDay;

}public int getDaysRented() {

return Util.getCurrentDay() – this.rentDay ;}public Movie getMovie() {

return this.movie;}

}

Page 20: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

Example: Customer classpublic class CustomerCustomer {

private String name;private List rentals =new ArrayList<Rental>();

public Customer(String name) { this.name = name;}public void addRental(Rental rental) {

this.rentals.addElement(rental);}public String getName() {

return this.name;}

Page 21: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

Example: Customer class(2)public class CustomerCustomer ...

public String statement() {double totalAmount = 0;String result = “Rental Record for “ + getName() + “\n”;

for (Rental each : this.rentals) {double thisAmount = 0;

// determine amounts for each line thisAmount = each.getMovie.getPriceCode() * each.getDaysRented(); totalAmount = totalAmount + thisAmount; result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + thisAmount + “\n”; } // add footer lines

result += “Amount owed is “+ totalAmount + “\n”; return result; }}

Page 22: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

22

Here comes the change

Add discount code for movies Old – 80% price Regular – 90% price Popular – 100% price

Add a html statement for sending online bills

Page 23: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

23

What to do?

Add conditional statements…for (Rental each : this.rentals) {

double thisAmount = 0;// determine amounts for each line

double discount = 1.0 if(each.getMovie().getDiscountCode() == DISCOUNT_OLD){ discount = 0.8; }else if(each.getMovie().getDiscountCode() == DISCOUNT_REG){ discount = 0.9 } thisAmount = each.getMovie().getPriceCode() * each.getDaysRented() * discount; totalAmount = totalAmount + thisAmount; result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + thisAmount + “\n”; }

Page 24: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

24

What to do?

Copy and paste statement() and revise it to generate a htmlStatement() method

The code looks much worse now compared with the start point Long statement method Duplicate code

Page 25: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

25

So after adding the two features, you plan to do refactoring

The main reason of the ugly code is ? Statement() method is doing too many things

(which are actually not parts of printing a statement)

Split it to three Calculation of the amount on each line Calculation of the amount sum Print lines

Page 26: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

26

Refactoring 1 1a: Extract method to getItemAmount()

It is actually something related to a rental

So 1b: Move it to rental, to reduce duty of Customer

1c: Rename it to getAmount()

double discount = 1.0 if(each.getMovie().getDiscountCode() == DISCOUNT_OLD){ discount = 0.8; }else if(each.getMovie().getDiscountCode() == DISCOUNT_REG){ discount = 0.9 } thisAmount = each.getMovie().getPriceCode() * each.getDaysRented() * discount;

Page 27: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

27

Refactoring 2

Extract method to getSumAmount()

It is relatively small, but since we need to copy the statement(), we should still extract this part to reduce duplication

double totalAmount = 0;...totalAmount = totalAmount + thisAmount;

Page 28: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

28

Results So statement() becomes

Looks much cleaner, less harmful to copy

May do some further refactoring by extract the header, footer, but may cause more complexity

No perfect solutions, just trade off

public String statement() { String result = “Rental Record for “ + getName() + “\n”; for (Rental each : this.rentals) { result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + each.getAmount() + “\n”; } // add footer lines result = result + “Amount owed is “+ getSumAmount() + “\n”; return result; }

Page 29: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

29

Before applying refactoring

Finish the work at your hand Usually bad smells come after you added a new

feature or fixed a bug

Have a test suite

Make sure your code pass all test cases

Find out to what extent you can use automatic refactoring tools (which guarantees perserving behaviors)

Page 30: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

30

Automatic refactoring

Refactoring involves lots of changes

Any change may cause bugs in the software

So, Most IDEs provide automatic refactoring tools Make sure that the program behavior does not

change after the refactoring Use automatic refactoring whenever possible Don’t do it by yourself

Page 31: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

31

Behind automatic software refactoring tools

Automatic refactoring

Pre-conditions

e.g., the class you rename does not have a main method

e.g, the method you move should not have a side-effect on the class’s fields, otherwise the field must be visible by the moving destination

The transform

When pre-conditions are not satisfied, tools may refuse refactoring or give you choices

Page 32: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

32

Today’s class

API comments and Documentation Javadoc

Software refactoring Why software refactoring? Types of refactoring Tool supports Behind refactoring tools

Software license Proprietary licenses Open source software licenses

Page 33: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

33

Software License

What is a license A document accompanying with the software about

an agreement of the user’s rights for using or doing other things to the software

For proprietary software End-user license agreements (EULAs)

For open source software GPL / LGPL

Apache

BSD

MIT

Page 34: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

34

Why licenses?

The specialty of software, compared to other publications You often have to copy the software to use it, consider

a multiple-CD/floppy disk game, hardware disposal, … Section 117 of copyright Act: the owner of a software

copy have the rights to copy the software to a computer for using purpose

General principle of copyrights: the owner of a copy can resell the copy without the agreement of the copyright owner: resell a book

So, basically, you can copy the software from the CD to your computer, and resell the CD…

Page 35: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

35

How software companies handle this?

We sell licenses, not software copies So the users are not “owners of copies” And they do not have the rights from section 117 of

the Copyright Act They only have the rights described in the license

That’s how software licenses come

A lot of legal issues, and cases Apple vs. Franklin MS vs. Harmony MS vs. DAK …

Page 36: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

36

Why care about licenses?

A recent news about

the page 46 of ios7

user agreement

Turn out to be

a funny fake

But should we care

about licenses?

Page 37: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

37

Why care about software licenses? If you work for a software company

As a developer, ask the department of legal issues before Using any new tools for your software development, e.g.,

editing, checking, analysis, visualization, version control, …

Incorporating any code, or using any third-party library in the software you are developing

Especially careful about free and open source software

As a manager May further need to consider what license to buy, how many

to buy, according to the permissions in the licenses

How to design the license of your product

Page 38: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

38

Proprietary licenses

A agreement on how users can use the software Who can use

certain person, the buyer: personal software

Any personal in a company/org: business software

How many users 1 installation

Fixed number of installations

Contract licenses

Period Permanent

Annual

Page 39: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

39

Volume Licenses

A license given to a large company, government, or education institute Allows large number of copies within the scope of

the customer A large fixed number or unlimited installations Installation with one license key The customer should keep license key confidential And software provider may request to audit the

usage of licenses

Page 40: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

40

License mechanisms

License keys

Server authentication

Hardware ID authentication after the first usage

Protecting the Licensing mechanism

Code obfuscation

Dynamic loaded code

encrypted code

Page 41: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

41

Open source licenses

A document accompany with the source code of open source software

Documenting the rights and responsibility of users or adapters

Users are required to accept the license before using the software

But usually, there is no mechanism to guarantee it

Page 42: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

42

Open source licenses

User rights Use, copy, incorporate, revise, re-distribute, …

User responsibilities Announce changes Include license when re-distribute Use certain license Providing revised source code

Page 43: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

43

GPL (General Public License)

Most widely used license

Rights All mentioned rights, including sell

Responsibilities Providing source code when re-distribute Re-distribute (any code including or revised from

GPL licensed code) under GPL license Ignore patents and regional rules (liberty or death)

So what will happen if I want to revise a GPL-licensed software, and then sell the revised software?

Page 44: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

44

GPL (General Public License)

Who is using GPL? About 45% all os projects according to Black Duck GNU software

gcc, glibc, emacs, …

Linux kernel MySQL OpenJDK (with classpath exception, why?)

Page 45: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

45

LGPL (Lesser-GPL)

A variant of GPL Mostly the same with GPL Dynamic and static link to LGPL libraries will not

enforce your software to be LGPL

Who is using LGPL QT KDE OpenOffice (once) JBoss

Page 46: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

46

BSD

A license used for Berkeley Software Distribution (Free BSD operating system)

Rights: all mentioned rights, including sell

Responsibilities: When re-distribution:

Including a copyright notice (both source and binary distributions)

Including an acknowledgement in ads

Needs permission to use the authors of the included BSD-software for promotion

Page 47: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

47

BSD

Who is using BSD? Free BSD operating system

Google Chrome

OpenSSH

Bzip

Page 48: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

48

Apache

A license proposed by apache software foundation

Rights Any mentioned rights, including sell

Responsibilities Include apache license in the distributed software

(both source and binary) State changes in the changed source files Including of the notice file if the Apache software

you use includes one

Page 49: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

49

Apache

Who is using apache?

Apache software foundation Ant, maven, log4j, tomcat, …

OpenOffice (now)

Android system The Java part

Most open-source android apps

Page 50: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

50

Eclipse Public License

Software license used by eclipse, and many of its plugins If just link to a EPL software library

Can use any license

No other requirements

If change and redistribute the code of EPL software The new program must be open source, and can be

distributed as binary code under any license compatible with EPL

The source code must under EPL

Page 51: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

51

Mozilla public license

A software license used by Mozilla software projects, firefox, thunderbird, etc.

Allow mixing of source files under different licenses

Changed MPL source files must still under MPL

CDDL (Common Development and Distribution License) is based on MPL Open Solaris, Netbeans, Glassfish, …

Page 52: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

52

Types of licenses

Permissive licenses / business-friendly licenses BSD

Apache

MIT

Copyleft licenses / business-unfriendly licenses GPL

Somewhat between the two LGPL

EPL

MPL

Page 53: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

53

Today’s class

API comments and Documentation Javadoc

Software license Proprietary licenses Open source software licenses

Software refactoring Why software refactoring? Types of refactoring Tool supports Behind refactoring tools

Page 54: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

54

Next class

Unit testing Drivers

Assertions

Documentation

Frameworks

JUnit and Demo

Parameterized Unit Testing

Page 55: CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.

55

Thanks!