tutorial4x4
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
374
On Slideshare
374
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Last lecture… On refactoring • We explained what is refactoring, what is software refactoring Tutorial 4 • How are they related to other restructuring More on Refactoring techniques? • Examples of refactoring How to refactoring unstructured code? • Refactoring structured source code into How to apply refactoring in Eclipse? goal models • … Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II 1. Refactoring an Today… unstructured program 1. How to refactoring unstructured code into • The subject is called “Squirrel Mail” goal models? • It has 70 KLOC 2. How to use Eclipse to do refactoring? • Developed in PHP 3. Discussions Function call Foo.php: <?php include(“bar.php”) ?> 4. Relation to your course project • Why it is unstructured? Foo.php: <a href=“bar.php”/> <a href=“moo.php”/> <?php echo “I won super 7!” ?> Any idea? Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 2. Why a PHP program is The process unstructured? • Every Hyperlink generated from the PHP • Structured program is easier to understand is an “exit” in the current PHP program /HJDF VRXUFH FRGH  ([WUDFW 0HWKRGV 5HIDFWRUHG VRXUFH FRGH 5HIDFWRU • It may call other PHP routines, other web <HV 1R  &RQVWUXFW pages, etc. when user click at them ,V VWUXFWXUHG" +DPPRFN JUDSK • Non-deterministic, how could you tell  ([WUDFW which link will the user click? $67 DQQRWDWHG  3DUVH 6WUXFWXUHG SURJUDP  (OLPLQDWH *272 +LJKOHYHO 6WDWHFKDUW 6WDWHV 7UDQVLWLRQV 6WDWHFKDUWV *RDO JUDSK
  • 3. 5HIDFWRULQJ • Even “go back” button will change the  5HVWUXFWXUH behaviour of the program  7HVW WR  $QDO]H 4XDOLW *RDO 0RGHO *RDO 0RGHO *RDO 0RGHO LGHQWLI PHWULFV WR LGHQWLI ZLWK • So … 1)5V ZLWK 1)5V VRIWJRDOV 6RIWJRDOV Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II 1.1 Refactoring based on Example comments /** Path for SquirrelMail required files. */ define(‘SM_PATH’,’../’); HQW  WKH IROORZLQJ GRHV 6 require_once($SM_PATH . ‘functions/strings.php’); U 6 , 2
  • 4.   UHIDFWRUHG require_once($SM_PATH . ‘config/config.php’); require_once($SM_PATH . ‘functions/i18n.php’); H[LW 6 , 2
  • 5.  6 , 2
  • 6.  require_once($SM_PATH . ‘functions/plugin.php’); require_once($SM_PATH . ‘functions/constants.php’);  RWKHU … require_once($SM_PATH . ‘functions/page_header.php’); require_once($SM_PATH . ‘functions/html.php’); 9DULDEOHV GHILQHG EHIRUH WKH HQWU RI WKH EORFN require_once($SM_PATH . ‘functions/global.php’); , ,∪,
  • 7. ∩ ^ Y _ GHI Y S
  • 8. ∧ S ∠ S HQWU ` ≠ φ require_once($SM_PATH . ‘functions/imap_general.php’); 9DULDEOHV GHILQHG LQ WKH EORFN WKDW ZLOO EH XVHG DIWHU WKH H[LW 2 2∪2
  • 9. ∩^ Y _ XVH Y S
  • 10. ∧ S H[LW ∠ S ` ≠ φ $SM_PATH=set_path (); Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 11. Further … 1.2 Convert into statechart <?php /* login.php */ • Statecharts concisely describe behaviour of a system. $SM_PATH=set_path (); $SM_lang=setup_language(); • No comments now, but we need to understand its $base_uri = findout_base_URI(); $logindisabled = detect_imap_server($base_uri); behaviour, therefore … if ($logindisabled) { explain_situation(); $SM_PATH is exit; undefined } do_hook(‘login_cookie’); $header =onload_function(“redirect.php”); /set_path display_header($header); $SM_PATH=set_path (); /set_path load_theme($theme[$theme_default]); do_hook('login_top'); show_logo(); show_form($loginname, $mailto, $key); $SM_PATH is $SM_PATH set do_hook('login_form'); do_hook('login_bottom'); ?> Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II 1.4 Put it together …the high-level statechart of the 1.3 Statechart refactoring unstructured program Extract Method -> Extract States and Transitions based on Hammock graphs What’s new here? You are refactoring behaviour rather than structures! Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 12. 1.4 Now convert statechart back 1.4 Eliminate GOTO’s into a program with GOTO’s • FORTRAN • FPT (Fortran parallelizing transformer, developed at ELIS, Ghent call EnterURL University, Belgium) 10 call Login • Result of goto elimination: if (wrongIMAP) goto 30 CALL EnterURL 20 call ShowForm REPEAT if (wrongKey) goto 20 REPEAT call EnterForm CALL Login IF (.not.wrongIMAP) THEN if (wrongForm) goto 30 REPEAT call StartWebMail CALL ShowForm if (loggedOut) goto 10 UNTIL (.not.wrongKey) if (expired) goto 10 CALL EnterForm IF(.not.wrongForm)THEN call Send CALL StartWebmail Stop ENDIF 30 call ReportError ENDIF call GoBack UNTIL (.not.loggedOut.or .not.expired.or.wrongIMAP .or.wrongForm) IF(wrongIMAP.or.wrongForm) goto 10 THEN end CALL ReportError • Rule of thumb: every state is a basic block; adding a label to states with CALL GoBack ENDIF multiple incoming transitions; adding GOTO statements for all outgoing UNTIL (.not.wrongIMAP.and.not.wrongForm) transitions except one; line-up the basic blocks CALL Send END Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II 1.5 Turning structured program into 1.6 Turning it into “pure” goal an annotated goal model model (AND/OR graph) •call A … call B AND A B •if (x) then … call A if(x) OR if(!x) else A B call B end if … [topic] AND •call A A repeat until (s) B repeat … call C OR until s s C call B Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 13. 1.7 Introducing softgoals 2. How to refactoring in Eclipse • Identify NFRs • If you are developing in Java, you are LUCKY! • Add softgoals to • The Eclipse IDE, JBuilder IDE are very categorize why comprehensive there are the • Refactoring was developed in Smalltalk, now NFRs moved to Java in Eclipse, it has been told in C# • If possible, one for Visual Studio, etc. can measure the • It should not be long to see open-source degree of programming languages to have them satisfaction for supported, such as PHP the softgoals • Examples, developed by Jing Su Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II Example 1 – extract method void f() { void f() { ... ... // Compute score computeScore(); score = a * b + c; } score -= discount; } void computeScore() { score = a * b + c; score -= discount; } Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 14. Enter a name according to the comment Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II Example 2 – move method class Jar { class Jar { ... bool isFragile() { } switch(material) { class RoboPacker { case GLASS: return true; private bool isFragile(Jar foo) { case WOOD: return true; switch(foo.material) { case TIN: return false; case GLASS: return true; }}} case WOOD: return true; class RoboPacker { case TIN: return false; private bool isFragile(Jar foo) { } return foo.isFragile(); } } } } Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 15. Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II Example 3 – lift method class Jar { class Jar { bool isFragile() { bool isFragile() { switch(material) { return material.isFragile(); case GLASS: }} // complex glass calculation case WOOD: interface Material { ... } // complex wood calculation class GlassMaterial:Material { ... } case TIN: class WoodMaterial:Material { ... } // complex tin calculation class TinMaterial:Material { ... } }}} Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II
  • 16. 3. Think about these … 4. Relation to your project 1. How to extend refactoring tool support to • Opportunities: – You may add junit test cases to the code base to other programming languages such as reveal bugs (publish it to the bug tracking system) PHP? and fix them (+5%) – You may apply design patterns, refactoring 2. Can you extend refactoring to techniques on this legacy code base, showing as an improved complexity metrics (+2.5%) documents, such as in various formats: – You may tune the performance of the system to diagrams, textual, xml, etc.? speed up the display, load/save for scalable graphs (+2.5%) 3. How can know a function is NFR? • Don’t forget your major project task (up to Can you measure the impact of a NFR 100%!) – To study the editor methods in the OpenOME and on a quality attribute? adapt them to the OmniGraphEditor web service. Spring 2005 ECE450H1S Software Engineering II Spring 2005 ECE450H1S Software Engineering II