Introduction
What is Refactoring?
Refactorings
are changes to a program that improve its
internal design but do not change its behavior. These
include minor, coding style changes (like using IMPLICIT
NONE statements), code readability improvements
(like replacing a variable named N with one called
NUM_POINTS), performance improvements (like
interchanging loops under certain conditions), and
even larger-scale design changes (like moving a
procedure from one module to another).
Although these types of changes can be done by hand,
making them is often tedious and error-prone. Photran
automates many such refactorings. For example,
the Refactor > Rename command can automatically
locate the declaration(s) and uses of a particular subroutine,
and change its name in all of those locations. It is "smart,"
too; if there is a subroutine named d and a variable
named d in a different context, it won't confuse the
two. Moreover, before making such a change, Photran
will attempt to verify that the change is safe to make.
For example, a subroutine A cannot be renamed to
B if there is already a variable named B in a
context where that subroutine is called.
For more information on refactoring, see
M. Fowler, Refactoring: Improving the Design of Existing Code,
Addison-Wesley, 1999.
Refactoring in Photran
Most refactorings can be accessed via the Refactor menu in the
menu bar, as described below. However, the most common
refactorings also have hotkeys (e.g., Alt+Shift+R for Rename;
hotkeys are listed in the Refactoring menu next to each command).
Also, most refactorings can be accessed by right-clicking in an
editor and choosing Refactor from the popup menu.
Some refactorings (such as Introduce Implicit None and
Replace Obsolete Operators) can be applied to several files
at once. As described below, this involves selecting one or more
files in the Fortran Projects view, then right-clicking on any of
the selected filenames and choosing Refactor from the popup
menu.
- Clicking on a filename in the Fortran Projects view selects
that file (and only that file).
- Ctrl+click (Command+click on Mac OS X) can be used
to select or deselect additional files.
- To select a range of files, click on one filename, and
Shift+click on a later filename;
those files and all of the files in between will be selected as
well.
Caution: Photran can only refactor free-format Fortran
source code. It is not possible to refactor fixed-form code.
Make sure that only free-form Fortran files
are selected. The Refactor menu may not be available if any
of the files are fixed-form or non-Fortran files.
Rename
- Description: Rename is essentially a "smart"
search and replace: It allows you to change the name of a variable,
subprogram, etc. It correctly observes scoping and shadowing rules
can also rename subprograms and module entities across files.
- Applies To:
- Local variables1,2
- Subprograms3 (including external and interface declarations)
- Derived types
- Module entities (variables and subprograms)
- Main programs
- Namelists
- Common blocks
- Block data subprograms
- Operation:
- Click on the name of a local variable, subprogram, etc.
- Click Refactor > Rename... The Rename dialog will appear.
- Enter a new name for the variable/subprogram/etc.
- If you are renaming an external subprogram or a subprogram
declared in an interface block, you may want to (un)check the
box labeled Match external subprograms with interfaces and
external declarations. If this is checked, the refactoring
will attempt to find all external subprograms, EXTERNAL
statements, and subprogram declarations in INTERFACE blocks that
have the given name, and they will all be renamed.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
1. Dummy subprogram arguments cannot be renamed
2. Components of derived types cannot be renamed
3. Intrinsic subprograms and type-bound procedures (Fortran 2003) cannot be renamed
Extract Procedure
- Description: Extract Procedure removes a sequence of statements from a procedure,
places them into a new subroutine, and replaces the original statements with a call to that
subroutine. Any local variables used by those statements will be passed as parameters to
the new procedure. This refactoring is generally used to make long procedures shorter.
- Applies To: A sequence of one or more action statements inside a procedure or main program.
- Operation:
- Select a sequence of one or more action statements in the editor. Be sure to include the
newline following the last statement in the selection.
- Click Refactor > Extract Procedure... The Extract Procedure dialog will appear.
- Enter a name for the new procedure that will be created.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Extract Local Variable
- Description: Extract Local Variable removes a subexpression from a larger expression
and assigns that subexpression to a local variable, replacing the original subexpression
with a reference to that variable. This refactoring is usually used to eliminate duplicated
subexpressions or to introduce explanatory variable names into complex expressions.
- Caveats: The refactoring will only be allowed to proceed if extracting the
subexpression will preserve the associativity and precedence of the original expression.
This refactoring assumes that the extracted expression has no side effects; it does
not
check whether moving the computation of the extracted expression will change the
behavior of the program.
- Operation:
- Select an expression in the editor.
- Click Refactor > Extract Local Variable...
The Extract Local Variable dialog will appear.
- Enter the type and name for the new local variable that will be created.
- Click Preview to see what changes will be made, then click OK to apply them.
Subprogram Refactorings
Add Subroutine Parameter
- Description: This refactoring will add a parameter to a subroutine.
- Applies To: A subroutine.
- Operation:
- Select the subroutine name or subroutine statement
- Click Refactoring > Subprogram > Add Subroutine Parameter (or press Shift+Alt+P)
- Use the input page to choose the type, intent, and name of the parameter.
- Type in numbers for the default value and position.
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Permute Subprogram Arguments
- Description: This refactoring will change the order of the arguments to a subroutine and adjust all call sites accordingly.
- Applies To: All files calling the selected subroutine.
- Operation:
- Select the subroutine name or subroutine statement
- Click Refactoring > Subprogram > Change Subroutine Signature.
- Use the input page's up and down buttons to rearrange the order of the arguments for the subroutine.
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Safe Delete
- Description: This refactoring will remove all Internal Subprograms from a given Host. The refactoring fails if there are any references to the subprogram.
- Applies To: A file.
- Operation:
- Select the subroutine name or subroutine statement to remove
- Click Refactoring > Subprogram > Safe Delete
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Module Refactorings
Make Private Entity Public
- Description:
Changes a module variable or subprogram from PRIVATE to PUBLIC visibility, and checks that it won't conflict with any existing name where that module is USEd.
- Applies To:
Variables, subroutines, functions.
- Does Not Apply To:
Intrinsics, Externals, Interfaces.
- Operation:
- Select the name of the private entity you wish to make public.
- Choose Refactor > Module > Make Private Entity Public.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example
Encapsulate Variable
- Description: Encapsulate variable creates getter and setter methods for the selected variable
in the module where it is defined and changes variable's visibility to private.
It also replaces all uses of that variable in all files to use getter and setter method calls1.
- Applies To:
- Variables defined in the module.
- Variables of all basic as well as user-defined types
- Does Not Apply To:
- Arrays
- Variables that are not defined in a module
- Parameters (i.e. integer, parameter :: CANNOT_ENCAPSULATE)
- Pointers (i.e. real, pointer :: CANNOT_ENCAPSULATE)
- Targes (i.e. integer, target :: CANNOT_ENCAPSULATE)
- Operation:
- Click on or select the name of variable you want to encapsulate.
- Click Refactor > Module > Encapsulate Variable. The Encapsulate Variable dialog will appear.
- Enter names for getter and setter methods. You will be warned if the names that you want to assign to your getter and setter methods will be conflicting with some other identifier in any of the involved files.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
1. If a variable is used as a parameter to a function/subroutine call, and that function changes
the value of the variable as a side-effect, that change will not be preserved.
Use Statement Refactorings
Add ONLY Clause to USE Statement
- Description:
Creates a list of the symbols that are being used from a module, and adds it to the USE statement.
- Applies To:
All modules containing public definitions.
- Does not apply to:
Empty modules or modules with only private entities.
- Operation:
- Select the name of the module in the USE statement you wish to add an ONLY clause to.
- Choose Refactor > Use Statement > Add Only Clause to Use Statement.
- Select which module entities you wish to include in the ONLY list. Any entities in an existing ONLY list will already be selected and can be deselected to be removed.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example
Minimize ONLY List
- Description:
Deletes symbols that are not being used from the ONLY list in a USE statement.
- Applies To:
USE statements with an ONLY clause.
- Operation:
- Select the name of the module in the USE statement you wish to minimize the ONLY list for.
- Choose Refactor > > Use Statement > Minimize Only List.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example
Common Block Refactorings
Make Common Block Variable Names Consistent
- Description:
Fortran allows different definitions of a COMMON block to give the same variable different names. This is confusing. This refactoring gives the variables the same names in all definitions of the COMMON block.
- Applies To:
All COMMON blocks with a valid name.
- Does not apply to:
COMMON blocks with a NULL name.
- Operation:
- Select the name of the COMMON block in the editor which you wish to make variable names consistent for.
- Choose Refactor > Common Block > Make Common Block Variable Names Consistent.
- Enter the new names which you wish to give the COMMON variables. The default new names are the original names in the selected block with "_common" appended.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example
Move Saved Variables to Common Block
- Description: Move Saved Variables to Common Block creates a common block for all "saved" variables of a subprogram. Declarations of these variables in the subprogram are transformed such that they are no longer "saved". The generated common block is declared both in the main PROGRAM and in the affected subprogram. Variables placed in the common block are renamed such that they do not conflict or shadow other variables. The current implementation assumes that the subprogram is in the CONTAINS section of the PROGRAM.
- Applies To: Subprograms.
- Operation:
- Click on the declaration statement of a subprogram.
- Click Refactor > Common Block > Move Saved Variables to Common Block.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Loop Refactorings
Interchange Loops
- Description: Swaps inner and outer loops of the selected nested do-loop1. This refactoring merely
switches the inner and outer do-headers. It will not make any changes to the body of the loop.
- Applies To: Selected nested do-loop
- Operation:
- Select the nested loops you wish to interchange
- Click Refactor > Do Loop > Interchange Loops. The Interchange loops dialog will appear.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
1. In order for refactoring to work correctly, there must be no statements
before the second loop. If such statements exist, correctness of the
refactoring is not guaranteed.
Fuse Loops
- Description: Takes two do-loops, normalizes their bounds, and finally puts the loop bodies in a single do-loop.
- Applies To: Find the two do-loops you want to fuse, and select only the first one1. If there are any statements in between the two loops, the statements will remain after the fused do-loop. In order for the loops to be compatible, the loops need to have the same number of iterations (the bounds do not need to be the same, just the number of times the body of the loop is accessed).
- Operation:
- Select the first do-loop to be fused.
- Click Refactor > Do Loop > Fuse Loops.
- Click Preview to see a comparison view of the changes made, and OK to apply the changes.
- Example:
1. The refactoring will find the next listed do-loop in your code (even with other lines of code in between them).
Reverse Loop
- Description: Takes an incrementing or decrementing loop, swaps the lower and upper bounds, and negates the step.
- Applies To: Selected do-loop
- Operation:
- Select full do-loop or do-loop header
- Click Refactor > Do Loop > Reverse Loop.
- Click Preview to see a comparison view of the changes made, and OK to apply the changes.
- Example:
Tile Loop
- Description: This refactoring takes a double nested do-loop, and creates a nested do-loop with four levels of depth. Instead of iterating through a two dimensional array (for example) by going through each row, it will loop over smaller tile blocks
- Applies To: A double nested do-loop where both step values are 1.
- Inputs:
- Tile Size: The size of the accessing block or tile. So, if the tile size is 3, an array will be accessed in 3x3 blocks.
- Tile Offset: Adjusts where the blocks start (all the data will always be covered no matter where it starts).
- Operation:
- Select the whole nested do-loop, or the header of the top nested do-loop.
- Click Refactoring > Do Loop > Tile Loop.
- Enter a tile step and tile offset, then click preview to view changes, or OK to apply changes.
- Example:
Unroll Loop
- Description:
Takes the selected do-loop and either completely or partially unrolls it. This will also optionally include a conditional statement to make sure the loop stays in bounds.
- Applies To:
A do-loop that doesn't contain labels, and never writes to the index variable (Ex: read(*,*) indexVar)
- Operation:
- Select the loop to unroll (or just the header).
- Click Refactoring > Do Loop > Unroll Loop.
- Select either the number of times you would like to unroll the loop, or check the "Complete unrolling" box. Uncheck the "Include bounds checking" box if you don't want a conditional statement to ensure proper loop bounds in numbered loop unrolling.
- Click Preview to see the changes this refactoring will make, and OK to make the changes.
- Example(Numbered):
- Example(Complete):
Refactorings to Remove Obsolete Language Features
Remove Arithmetic If
- Description: This refactoring will remove all arithmetic if statements, a feature that became obsolete in Fortran 90, from a single file or all the files in a project.
- Applies To: A project or file.
- Operation:
- Select the project or file with arithmetic if statements to remove.
- Click Refactoring > Obsolete Language Features > Remove Arithmetic If Statements
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Remove Assigned Goto
- Description: This refactoring will remove all assigned goto statements and replace them with case blocks.
- Applies To: All assigned goto statements.
- Operation:
- Click Refactoring > > Obsolete Language Features > Remove Assigned Goto.
- Choose Yes if you want to add a default case, otherwise choose No.
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Remove Branch to End If
- Description: Removes the branch to END IF statements. The GOTO statements carry outbranching. Branching to end if is replaced with branching to CONTINUE statement that immediately follows the END IF statement.
- Applies To: Selected end if statement
- Operation:
- Select an end if statement with a statement label
- Click Refactor > Obsolete Language Features > Remove Branch to End If
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Replace Character* with Character(len=)
- Description: Replaces character*n declaration with character(len=n) declaration.
- Applies To: All character declarations
- Operation:
- Select one of the character declaration statements
- Click Refactor > Obsolete Language Features > Replace Character* with Character(len=)... The Replace Character* with Character(len=) will appear.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Remove Computed Goto
- Description: Replaces computed goto statement with select case statement
- Applies To: Selected computed goto statement
- Operation:
- Select the computed goto statement you wish to remove
- Click Refactor > Obsolete Language Features > Remove Computed Goto. The Remove Computed Goto dialog will appear.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Replace Obsolete Operators
- Description: Replace Obsolete Operators replaces all uses of old-style
comparison operators (such as .LT. and .EQ.) with their newer equivalents
(symbols such as < and ==).
and adds explicit declarations for all variables that were previously declared implicitly.
- Applies To: All uses of the following operators in one or more files:
.LT. .LE. .EQ. .NE. .GT. .GE.
- Operation:
- This is a multiple-file refactoring.
- To Replace Obsolete Operators in a single file,
open the file in the editor and choose
Refactor > Obsolete Language Features > Replace Obsolete Operators
from the menu bar.
- To Replace Obsolete Operators in multiple files,
select the files in the Fortran Projects view,
right-click on any of the selected filenames,
and choose
Refactor > Obsolete Language Features > Replace Obsolete Operators
from the popup menu.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Replace Old-Style Do Loops
- Description: Replaces old-style do loops with the modern style of do loops
- Applies To: All old-style do loops
- Operation:
- Click Refactor > Obsolete Language Features > Remove Old-Style Do Loops.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Remove Pause Statement
- Description: Replaces pause statement with empty print and read statements
- Applies To: Selected pause statement
- Operation:
- Select the pause statement you wish to remove
- Click Refactor > Obsolete Language Features > Remove Pause Statement.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Remove Real/Double Precision Loop Counter
- Description: Transforms a do or do while loop with control to a do or do while loop without control.
- Applies To: Selected loop
- Operation:
- Select the loop you wish to transform
- Click Refactor > Obsolete Language Features > Remove Real/Double Precision Loop Counter.
- Choose either do loop or do while loop.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Refactorings to Improve Coding Style
Add Identifier to End Statement
- Description: This refactoring will add the program/subprogram name to an end statement.
- Applies To: A file or project.
- Operation:
- Select the file or project.
- Click Refactoring > Coding Style > Add Identifier to End Statement
- Click preview to view the changes without applying, or OK to apply the changes.
- Example:
Change Keyword Case
- Description: Makes all applicable keywords the same case throughout the selected Fortran
program files.
- Applies To: All keywords except those listed below.
- Does not apply to:
- Identifiers
- All constants except for integer constants and real constants
- Operation:
- This is a multiple-file refactoring.
- To Change Keyword Case in a single file,
open the file in the editor and choose Refactor > Coding Style > Change Keyword Case from the menu bar.
- To Introduce Implicit None in multiple files, select the
files in the Fortran Projects view, right-click on any of the selected filenames,
and choose Refactor > Coding Style > Change Keyword Case from the popup menu.
- Select Upper or Lower Case
- Click Preview to see what changes will be made, then click OK to apply them.
- Example
Convert Between If Statement and If Construct
- Description: Converts a simple if statement to an if construct with a then block and possible else block.
- Applies To: Selected if statement.
- Operation:
- Refactor > Coding Style > Convert Between If Statement and If Construct
- Select whether or not you want an empty else block.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Convert Data Statement to Parameter Statements
- Description: When a variable declared in a DATA statement is intended to be a constant, Data to Parameter can be used to change it to a variable with the PARAMETER attribute. Using the PARAMETER attribute makes it more clear which variables are constants and which ones are not; it can also result in performance gains, since it may allow an optimizing compiler to replace some variable accesses with the constant value.
- Applies To: All main programs, subprograms, and modules in one or more files.
- Operation:
- This is a multiple-file refactoring.
- To transform variables declared as data in variables declared with parameter attribute in a single file,
open the file in the editor and choose
Refactor > Coding Style > Convert Data Statement to Parameter Statements from the menu bar.
- To transform variables declared as data in variables declared with parameter attribute in multiple files,
select the files in the Fortran Projects view,
right-click on any of the selected filenames,
and choose
Refactor > Coding Style > Convert Data Statement to Parameter Statements
from the popup menu.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Introduce Implicit None
- Description: Adds IMPLICIT NONE statements to a file
and adds explicit declarations for all variables that were previously declared implicitly.
- Applies To: All main programs, subprograms, and modules in one or more files.
- Operation:
- This is a multiple-file refactoring.
- To Introduce Implicit None in a single file,
open the file in the editor and choose
Refactor > Coding Style > Introduce Implicit None
from the menu bar.
- To Introduce Implicit None in multiple files,
select the files in the Fortran Projects view,
right-click on any of the selected filenames,
and choose
Refactor > Coding Style > Introduce Implicit None
from the popup menu.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Make Save Attributes Explicit
- Description: Makes Save Attributes Explicit.
- Applies To: The entire program.
- Operation:
- Refactor > Coding Style > Make Save Attributes Explicit
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Remove Unreferenced Labels
- Description: Removes any unreferenced labels.
- Applies To: All unreferenced labels.
- Operation:
- Refactor > Coding Style > Remove Unreferenced Labels
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Remove Unused Local Variables
- Description: Remove Unused Variables removes declarations of local variables that are never used.
- Applies To: All main programs, subprograms, and modules in one or more files.
- Operation:
- This is a multiple-file refactoring.
- To Remove Unused Variables in a single file,
open the file in the editor and choose
Refactor > Coding Style > Remove Unused Local Variables
from the menu bar.
- To Remove Unused Variables in multiple files,
select the files in the Fortran Projects view,
right-click on any of the selected filenames,
and choose
Refactor > Coding Style > Remove Unused Local Variables
from the popup menu.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example:
Standardize Statements
- Description: Standardize Statements rewrites all variables declarations, so that
- there is only one variable declaration per line, and
- every variable declaration contains a double colon (::).
This is intended to make the code more readable.
- Applies To: All main programs, subprograms, and modules in one or more files.
- Operation:
- This is a multiple-file refactoring.
- To Standardize Statements in a single file,
open the file in the editor and choose
Refactor > Coding Style > Standardize Statements
from the menu bar.
- To Standardize Statements in multiple files,
select the files in the Fortran Projects view,
right-click on any of the selected filenames,
and choose
Refactor > Coding Style > Standardize Statements
from the popup menu.
- Click Preview to see what changes will be made, then click OK to apply them.
- Example: