Toronto ECE 450 - Tutorial 4 - More on Refactoring

Unformatted text preview:

Spring 2005 ECE450H1S Software Engineering IITutorial 4More on RefactoringHow to refactoring unstructured code?How to apply refactoring in Eclipse?Spring 2005 ECE450H1S Software Engineering IILast lecture…On refactoring• We explained what is refactoring, what is software refactoring• How are they related to other restructuring techniques?• Examples of refactoring• Refactoring structured source code into goal models• …Spring 2005 ECE450H1S Software Engineering II1. How to refactoring unstructured code into goal models?2. How to use Eclipse to do refactoring?3. Discussions4. Relation to your course projectToday…Spring 2005 ECE450H1S Software Engineering II1. Refactoring an unstructured program• The subject is called “Squirrel Mail”• It has 70 KLOC• Developed in PHPFunction callFoo.php: <?php include(“bar.php”) ?>• 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 IIWhy a PHP program is unstructured?• Every Hyperlink generated from the PHP is an “exit” in the current PHP program• It may call other PHP routines, other web pages, etc. when user click at them• Non-deterministic, how could you tell which link will the user click?• Even “go back” button will change the behaviour of the program• So …Spring 2005 ECE450H1S Software Engineering IIThe process• Structured program is easier to understand     !"# $!%&!'!())* *+,-!+,.!/0 1!(234+56!(27 Spring 2005 ECE450H1S Software Engineering II1.1 Refactoring based on comments    ……   ∪∩ !"!#∧ #∠ # $≠ φ % &∪∩ !"%!#∧ #&∠ #$≠ φ  Spring 2005 ECE450H1S Software Engineering IIExample/** Path for SquirrelMail required files. */define(‘SM_PATH’,’../’);require_once($SM_PATH . ‘functions/strings.php’);require_once($SM_PATH . ‘config/config.php’);require_once($SM_PATH . ‘functions/i18n.php’);require_once($SM_PATH . ‘functions/plugin.php’);require_once($SM_PATH . ‘functions/constants.php’);require_once($SM_PATH . ‘functions/page_header.php’);require_once($SM_PATH . ‘functions/html.php’);require_once($SM_PATH . ‘functions/global.php’);require_once($SM_PATH . ‘functions/imap_general.php’); $SM_PATH=set_path ();Spring 2005 ECE450H1S Software Engineering IIFurther …<?php /* login.php */$SM_PATH=set_path ();$SM_lang=setup_language();$base_uri = findout_base_URI();$logindisabled = detect_imap_server($base_uri);if ($logindisabled) {explain_situation(); exit;}do_hook(‘login_cookie’);$header =onload_function(“redirect.php”);display_header($header);load_theme($theme[$theme_default]);do_hook('login_top');show_logo();show_form($loginname, $mailto, $key);do_hook('login_form');do_hook('login_bottom');?>Spring 2005 ECE450H1S Software Engineering II1.2 Convert into statechart• Statecharts concisely describe behaviour of a system. • No comments now, but we need to understand its behaviour, therefore …$SM_PATH=set_path (); $SM_PATH isundefined$SM_PATH isset/set_path$SM_PATH/set_pathSpring 2005 ECE450H1S Software Engineering II1.3 Statechart refactoringExtract Method -> Extract States and Transitions based on Hammock graphsWhat’s new here? You are refactoring behaviour rather than structures!Spring 2005 ECE450H1S Software Engineering II1.4 Put it together …the high-level statechart of the unstructured programSpring 2005 ECE450H1S Software Engineering II1.4 Now convert statechart back into a program with GOTO’s• FORTRANcall EnterURL10 call Loginif (wrongIMAP) goto 3020 call ShowFormif (wrongKey) goto 20call EnterFormif (wrongForm) goto 30call StartWebMailif (loggedOut) goto 10if (expired) goto 10call SendStop30 call ReportErrorcall GoBackgoto 10end • Rule of thumb: every state is a basic block; adding a label to states with multiple incoming transitions; adding GOTO statements for all outgoing transitions except one; line-up the basic blocksSpring 2005 ECE450H1S Software Engineering II1.4 Eliminate GOTO’s• FPT (Fortran parallelizing transformer, developed at ELIS, GhentUniversity, Belgium)• Result of goto elimination:CALL EnterURLREPEATREPEATCALL LoginIF (.not.wrongIMAP) THENREPEATCALL ShowFormUNTIL (.not.wrongKey)CALL EnterFormIF(.not.wrongForm)THENCALL StartWebmailENDIFENDIFUNTIL (.not.loggedOut.or .not.expired.or.wrongIMAP .or.wrongForm)IF(wrongIMAP.or.wrongForm)THENCALL ReportErrorCALL GoBackENDIFUNTIL (.not.wrongIMAP.and.not.wrongForm)CALL SendENDSpring 2005 ECE450H1S Software Engineering II1.5 Turning structured


View Full Document
Download Tutorial 4 - More on Refactoring
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Tutorial 4 - More on Refactoring and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Tutorial 4 - More on Refactoring 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?