e49d125d77968c389c02092ef6f15b3a.ppt
- Количество слайдов: 55
CS 5103 Software Engineering Lecture 13 Software Refactoring Software Licenses
Last class Ø Issue tracking system Ø Ø Process of issues Ø Ø Type of issues Resolution of issues Coding styles Ø Code Ø Ø Expressions Ø Statements and Lines Ø Blocks Ø 2 Variables and constants Methods and Classes
Today’s class Ø API comments and Documentation Ø Ø Javadoc Software refactoring Ø Ø Types of refactoring Ø Tool supports Ø Ø Why software refactoring? Behind refactoring tools Software license Ø Ø 3 Proprietary licenses Open source software licenses
Coding styles Ø Variable/Constant Ø Ø Expression Ø Ø Logic blocks, Space indent, Curly brace for singletons, No copy-paste Methods / Classes Ø 4 One statement per line, break long lines Blocks Ø Ø Break complex ones, avoid corner cases, more brackets Statements/line Ø Ø Meaningful name, follow conventions, externalize constants Break to pieces when too large, smaller method signatures, follow common structures
Java. Doc Ø Ø Ant 1. 6. 5 Ø 5 A Java. Doc Example http: //api. dpml. net/ant/1. 6. 5/overview-summary. html
Java. Doc Ø Structure Ø Overview Ø Packages Ø Classes / Interfaces / Exceptions Ø Fields / Constructors / Methods Ø Ø Deprecated Ø 6 Hierarchy Index
Java. Doc-Tags Ø Ø Tags are meta-data put in the comments to guide Javadoc in document generation Starting with “@” Ø Ø @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 Ø 7 @author : author of the class @deprecated: an outdated method
Javadoc: Mapping Ø Commenting methods for documentation /** * Constructs a Word. Tree node, specify its * data with the parameter word, both children * of the Word. Tree node are null * * @param word the string value stored as the data of the node, cannot be null * @exception Null. Word. Exception an exception raised when the parameter word is null */ public Word. Tree(String word) throws Null. Word. Exception{. . . /** * fetch the data of current Word. Tree node * @return the data stored at current node */ public String get. Data(). . . 8
Javadoc: Mapping Ø 9 Javadoc generated
Java. Doc Ø Demo for generating Javadoc in eclipse Ø Ø 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 Ø 10 Right click on your project Click on finish
Today’s class Ø API comments and Documentation Ø Ø Javadoc Software refactoring Ø Ø Types of refactoring Ø Tool supports Ø Ø Why software refactoring? Behind refactoring tools Software license Ø Ø 11 Proprietary licenses Open source software licenses
Software Refactoring, why? Ø So far we talked about issues to be considered when design and implementing software Ø Ø 12 Design patterns Coding Styles Considering these, you can try to find a best implementation for the current requirements But requirements change quickly…
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 Ø 13 Refactoring- Improving the Design of Existing Code, Addison Wesley, 1999. Martin Fowler et al.
When to apply refactorings Ø Bad smells Ø Ø 14 Long parameter list Ø Ø Long method / Huge Class Ø Ø Code duplication …. As the software evolve, these smells automatically appears Especially when you do extreme programming, which tries to ignore design issues at the beginning
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 Ø Ø 15 Support only spell checking at beginning So no need to bother for visitor patterns
Types of refactorings Ø Ø Move methods / fields Ø Extract methods / interface Ø Rename Ø Replace temp with query Ø Pull up / Push down methods / fields Ø 16 Add/Remove methods / fields …
An example from Martin Fowler’s book Ø Ø Ø 17 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
Example: Movie class public class Movie { private String title; private int price. Code; public Movie(String title, int price. Code) { this. title=title; this. price. Code = price. Code; } public String get. Title() { return this. title; } public int get. Price. Code() { return this. price. Code; } public void set. Price. Code(int price. Code) { this. price. Code = price. Code; } }
Example: Rental class public class Rental { private Movie movie; private int rent. Day; public Rental(Movie movie, int rent. Day) { this. movie = movie; this. rent. Day = rent. Day; } public int get. Days. Rented() { return Util. get. Current. Day() – this. rent. Day ; } public Movie get. Movie() { return this. movie; } }
Example: Customer class public class Customer { private String name; private List rentals =new Array. List
Example: Customer class(2) public class Customer. . . public String statement() { double total. Amount = 0; String result = “Rental Record for “ + get. Name() + for (Rental each : this. rentals) { double this. Amount = 0; // determine amounts for each line this. Amount = each. get. Movie. get. Price. Code() * each. get. Days. Rented(); total. Amount = total. Amount + this. Amount; result = result + each. get. Movie. get. Name() + “ ” + each. get. Days. Rented() + “ ” + this. Amount + “n”; } // add footer lines result += “Amount owed is “+ total. Amount + “n”; return result; } } “n”;
Here comes the change Ø Add discount code for movies Ø Ø 22 Regular – 90% price Ø Ø Old – 80% price Popular – 100% price Add a html statement for sending online bills
What to do? Ø Add conditional statements… for (Rental each : this. rentals) { double this. Amount = 0; // determine amounts for each line double discount = 1. 0 if(each. get. Movie(). get. Discount. Code() == DISCOUNT_OLD){ discount = 0. 8; }else if(each. get. Movie(). get. Discount. Code() == DISCOUNT_REG){ discount = 0. 9 } this. Amount = each. get. Movie(). get. Price. Code() * each. get. Days. Rented() * discount; total. Amount = total. Amount + this. Amount; result = result + each. get. Movie. get. Name() + “ ” + each. get. Days. Rented() + “ ” + this. Amount + “n”; } 23
What to do? Ø Ø Copy and paste statement() and revise it to generate a html. Statement() method The code looks much worse now compared with the start point Ø Ø 24 Long statement method Duplicate code
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 sum Ø 25 Calculation of the amount on each line Print lines
Refactoring 1 Ø 1 a: Extract method to get. Item. Amount() double discount = 1. 0 if(each. get. Movie(). get. Discount. Code() == DISCOUNT_OLD){ discount = 0. 8; }else if(each. get. Movie(). get. Discount. Code() == DISCOUNT_REG){ discount = 0. 9 } this. Amount = each. get. Movie(). get. Price. Code() * each. get. Days. Rented() * discount; Ø Ø So 1 b: Move it to rental, to reduce duty of Customer Ø 26 It is actually something related to a rental 1 c: Rename it to get. Amount()
Refactoring 2 Ø Extract method to get. Sum. Amount() double total. Amount = 0; . . . total. Amount = total. Amount + this. Amount; Ø 27 It is relatively small, but since we need to copy the statement(), we should still extract this part to reduce duplication
Results Ø So statement() becomes public String statement() { String result = “Rental Record for “ + get. Name() + “n”; for (Rental each : this. rentals) { result = result + each. get. Movie. get. Name() + “ ” + each. get. Days. Rented() + “ ” + each. get. Amount() + “n”; } // add footer lines result = result + “Amount owed is “+ get. Sum. Amount() + “n”; return result; } Ø Looks much cleaner, less harmful to copy Ø Ø 28 May do some further refactoring by extract the header, footer, but may cause more complexity No perfect solutions, just trade off
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 Ø 29 Find out to what extent you can use automatic refactoring tools (which guarantees perserving behaviors)
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 Ø Ø 30 Use automatic refactoring whenever possible Don’t do it by yourself
Behind automatic software refactoring tools Ø Automatic refactoring Ø Pre-conditions Ø Ø 31 e. g. , the class you rename does not have a main method e. g, the method you move should not have a sideeffect 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
Today’s class Ø API comments and Documentation Ø Ø Javadoc Software refactoring Ø Ø Types of refactoring Ø Tool supports Ø Ø Why software refactoring? Behind refactoring tools Software license Ø Ø 32 Proprietary licenses Open source software licenses
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 Ø Ø Apache Ø BSD Ø 33 GPL / LGPL MIT
Why licenses? Ø The specialty of software, compared to other publications Ø Ø 34 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…
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 Ø Ø MS vs. Harmony Ø 35 Apple vs. Franklin MS vs. DAK Ø …
Why care about licenses? Ø A recent news about the page 46 of ios 7 user agreement Turn out to be a funny fake But should we care about licenses? 36
Why care about software licenses? Ø If you work for a software company Ø As a developer, ask the department of legal issues before Ø Ø 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 Ø Ø 37 Using any new tools for your software development, e. g. , editing, checking, analysis, visualization, version control, … 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
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 Ø Ø Fixed number of installations Ø Ø 1 installation Contract licenses Period Ø Ø 38 Permanent Annual
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 Ø 39 And software provider may request to audit the usage of licenses
License mechanisms Ø License keys Ø Ø Ø Server authentication Hardware ID authentication after the first usage Protecting the Licensing mechanism Ø Ø Dynamic loaded code Ø 40 Code obfuscation encrypted code
Open source licenses Ø Ø 41 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
Open source licenses Ø User rights Ø Ø Use, copy, incorporate, revise, re-distribute, … User responsibilities Ø Ø Include license when re-distribute Ø Use certain license Ø 42 Announce changes Providing revised source code
GPL (General Public License) Ø Most widely used license Ø Rights Ø Ø Responsibilities Ø Ø 43 All mentioned rights, including sell 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?
GPL (General Public License) Ø Who is using GPL? Ø Ø About 45% all open source projects according to Black Duck GNU software Ø gcc, glibc, emacs, … Ø Ø My. SQL Ø 44 Linux kernel Open. JDK (with classpath exception, why? )
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 Ø Ø KDE Ø Open. Office (once) Ø 45 QT JBoss
BSD Ø A license used for Berkeley Software Distribution (Free BSD operating system) Ø Rights: all mentioned rights, including sell Ø Responsibilities: Ø When re-distribution: Ø Ø Ø 46 Including a copyright notice (both source and binary distributions) Including an acknowledgement in ads Needs permission to use the authors of the included BSDsoftware for promotion
BSD Ø Who is using BSD? Ø Ø Google Chrome Ø Open. SSH Ø 47 Free BSD operating system Bzip
Apache Ø A license proposed by apache software foundation Ø Rights Ø Ø Responsibilities Ø Ø Ø 48 Any mentioned rights, including sell 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
Apache Ø Who is using apache? Ø Apache software foundation Ø Ant, maven, log 4 j, tomcat, … Ø Open. Office (now) Ø Android system Ø Ø 49 The Java part Most open-source android apps
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 Ø Ø 50 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
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 Ø 51 Open Solaris, Netbeans, Glassfish, …
Types of licenses Ø Permissive licenses / business-friendly licenses Ø Ø Apache Ø MIT Ø Ø BSD … Copyleft licenses / business-unfriendly licenses Ø Ø GPL Somewhat between the two Ø Ø EPL Ø MPL Ø 52 LGPL …
Today’s class Ø API comments and Documentation Ø Ø Javadoc Software license Ø Ø Ø Proprietary licenses Open source software licenses Software refactoring Ø Ø Types of refactoring Ø Tool supports Ø 53 Why software refactoring? Behind refactoring tools
Next class Ø Unit testing Ø Drivers Ø Assertions Ø Documentation Ø Frameworks Ø Ø 54 JUnit and Demo Parameterized Unit Testing
Thanks! 55