Download presentation
Presentation is loading. Please wait.
Published byAleesha Walsh Modified over 9 years ago
1
CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses
2
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
3
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
4
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
5
5 JavaDoc A JavaDoc Example Ant 1.6.5 http://api.dpml.net/ant/1.6.5/overview-summary.html http://api.dpml.net/ant/1.6.5/overview-summary.html
6
6 JavaDoc Structure Overview Packages Classes / Interfaces / Exceptions Fields / Constructors / Methods Hierarchy Deprecated Index
7
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
8
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()...
9
9 Javadoc: Mapping Javadoc generated
10
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
11
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
12
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…
13
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.
14
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
15
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
16
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 …
17
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
18
Example: Movie class public 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; }
19
Example: Rental class Rental public class Rental { 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; }
20
Example: Customer class Customer public class Customer { private String name; private List rentals =new ArrayList (); public Customer(String name) { this.name = name; } public void addRental(Rental rental) { this.rentals.addElement(rental); } public String getName() { return this.name; }
21
Example: Customer class(2) Customer public class Customer... 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; }
22
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
23
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”; }
24
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
25
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
26
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;
27
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;
28
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; }
29
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)
30
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
31
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
32
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
33
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
34
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…
35
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 …
36
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?
37
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
38
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
39
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
40
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
41
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
42
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
43
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?
44
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?)
45
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
46
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
47
47 BSD Who is using BSD? Free BSD operating system Google Chrome OpenSSH Bzip
48
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
49
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
50
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
51
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, …
52
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 …
53
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
54
54 Next class Unit testing Drivers Assertions Documentation Frameworks JUnit and Demo Parameterized Unit Testing
55
55 Thanks!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.