Tred Code Analysis & Refactoring Documentation
Total Page:16
File Type:pdf, Size:1020Kb
Charles University in Prague Faculty of Mathematics and Physics MASTER THESIS Peter Fabian Refactoring tree editor TrEd Institute of Formal and Applied Linguistics Supervisor of the master thesis: doc. Ing. Zdenˇek Zabokrtsk´y,ˇ Ph.D. Study programme: Computer Science Specialization: Mathematical Linguistics Prague 2011 I would like to thank my supervisor, Mr. Zabokrtsk´yforˇ the inspirations and his patience. I would also like to thank my family for their endless support. I declare that I carried out this master thesis independently, and only with the cited sources, literature and other professional sources. I understand that my work relates to the rights and obligations under the Act No. 121/2000 Coll., the Copyright Act, as amended, in particular the fact that the Charles University in Prague has the right to conclude a license agreement on the use of this work as a school work pursuant to Section 60 paragraph 1 of the Copyright Act. In ................... date ..................... N´azev pr´ace: Refaktorizace editoru strom˚uTrEd Autor: Peter Fabian Katedra: Ustav´ form´aln´ıa aplikovan´elingvistiky Vedouc´ıdiplomov´epr´ace: doc. Ing. Zdenˇek Zabokrtsk´y,ˇ Ph.D., Ustav´ form´aln´ı a aplikovan´elingvistiky Abstrakt: C´ılem pr´ace bylo refaktorizovat editor strom˚uTrEd, zlepˇsit t´ımjeho modularitu, usnadnit ´udrˇzbu a dalˇs´ıv´yvoj aplikace. D˚uraz byl tak´ekladen na zachov´an´ırychlosti programu. Zdrojov´yk´od TrEdu byl prozkoum´an metodami statick´ea dynamick´eanal´yzy, kter´epomohly identifikovat probl´emov´am´ısta. Bylo vytvoˇreno 50 nov´ych modul˚ua pˇresunuto znaˇcn´emnoˇzstv´ı k´odu. Byla tak´evytvoˇrena sada test˚u, zvolena pravidla pro psan´ınov´eho k´odu a seps´ana dokumentace ˇc´asti st´avaj´ıc´ıho k´odu. K´od byl po refaktorizaci opˇetovnˇepodroben kvalitativn´ıi kvantitativn´ıanal´yze a jej´ıv´ysledky byly porovn´any se stavem pˇred refaktorizac´ı. Kl´ıˇcov´aslova: Editor strom˚uTrEd, Perl, refaktorizace, anal´yza k´odu Title: Refactoring tree editor TrEd Author: Peter Fabian Department: Institute of Formal and Applied Linguistics Supervisor: doc. Ing. Zdenˇek Zabokrtsk´y,ˇ Ph.D., Institute of Formal and Applied Linguistics Abstract: The main goal of the thesis was to refactor tree editor TrEd, improve its modularity, maintainability and make its further development less difficult. Static and dynamic analysis of TrEd have been performed in order to help us find acrid spots in the source code. More than 230 subroutines and data structures have been moved between packages, 50 new packages and a test suite with more than 1,300 tests have been created. A new coding style have been chosen for further development and most severe violations of this standard have been fixed. After the changes done on the source code, it have been analyzed again and the results have been compared with the previous state. Keywords: Tree Editor TrEd, Perl, code refactoring, code analysis Contents 1 Introduction 7 2 Code Analysis 8 2.1 Codesmells .............................. 8 2.1.1 DuplicatedCode ....................... 8 2.1.2 LongSubroutines ....................... 9 2.1.3 LargeClass .......................... 9 2.1.4 LongParameterList ..................... 9 2.1.5 DivergentChange....................... 10 2.1.6 ShotgunSurgery ....................... 10 2.1.7 FeatureEnvy ......................... 10 2.1.8 DataClumps ......................... 10 2.1.9 Primitive Obsession . 11 2.1.10 Switch(Case)Statements . 11 2.1.11 Parallel Inheritance Hierarchies . 11 2.1.12 LazyClass........................... 11 2.1.13 Speculative Generality . 11 2.1.14 TemporaryField ....................... 11 2.1.15 MessageChains........................ 12 2.1.16 MiddleMan.......................... 12 2.1.17 InappropriateIntimacy. 12 2.1.18 Alternative Classes with Different Interfaces . 12 2.1.19 Incomplete Library Class . 13 2.1.20 DataClass........................... 13 2.1.21 RefusedBequest ....................... 13 2.1.22 Comments........................... 13 2.2 Staticcodeanalysis.......................... 13 2.2.1 Overview ........................... 14 2.2.2 CodeMetrics ......................... 15 2.2.3 Perl::Critic . 22 2.2.4 CCFinderX .......................... 24 2.3 DynamicCodeAnalysis ....................... 26 2.3.1 bTrEdEvaluation....................... 29 2.3.2 TrEdStart........................... 29 2.3.3 BrowsinginTrEd....................... 32 3 Design of TrEd 33 3.1 Overview................................ 33 3.2 Libraries................................ 34 3.3 TrEdstart-up ............................. 34 3.4 TrEd::File . 38 3.5 Filelists . 39 3.5.1 Filelist . 39 3.5.2 TrEd::Bookmarks....................... 40 3.6 TrEd::FileLock . 40 1 3.7 TrEd::Undo .............................. 41 3.8 TrEd::Config.............................. 41 3.9 Converting............................... 42 3.10AnnotationModes .......................... 42 3.11TrEd::Stylesheet............................ 42 3.12TrEd::Window............................. 42 3.13BindingSystem ............................ 43 3.14MacroSystem............................. 43 3.14.1 Macros............................. 43 3.14.2 Hooks ............................. 45 3.14.3 Extensions........................... 46 3.14.4 MinorModes ......................... 46 4 Coding style 47 4.1 CodeLayout.............................. 48 4.1.1 Bracketing........................... 48 4.1.2 Keywords ........................... 48 4.1.3 Subroutines and Variables . 48 4.1.4 Builtins . 49 4.1.5 KeysandIndices ....................... 49 4.1.6 Operators ........................... 49 4.1.7 Semicolons........................... 49 4.1.8 Commas............................ 50 4.1.9 LineLengths ......................... 50 4.1.10 Indentation .......................... 50 4.1.11 Tabs .............................. 50 4.1.12 Blocks ............................. 51 4.1.13 Chunking ........................... 51 4.1.14 Elses.............................. 51 4.1.15 Vertical Alignment . 51 4.1.16 BreakingLongLines . .. 52 4.1.17 Non-terminalExpressions . 52 4.1.18 BreakingbyPrecedence . 52 4.1.19 Assignments.......................... 53 4.1.20 Ternaries............................ 53 4.1.21 Lists .............................. 53 4.1.22 AutomatedLayout . 53 4.2 NamingConventions ......................... 54 4.2.1 Identifiers ........................... 54 4.2.2 Booleans............................ 55 4.2.3 ReferenceVariables. .. 55 4.2.4 ArraysandHashes ...................... 55 4.2.5 Underscores .......................... 55 4.2.6 Capitalization . 55 4.2.7 Abbreviation ......................... 56 4.2.8 Ambiguous Abbreviations . 56 4.2.9 AmbiguousNames ...................... 56 4.2.10 Utility Subroutines . 56 2 4.3 ValuesandExpressions. .. .. 57 4.3.1 String Delimiters . 57 4.3.2 EmptyStrings......................... 57 4.3.3 Single-Character Strings . 57 4.3.4 EscapedCharacters. 57 4.3.5 Constants ........................... 58 4.3.6 LeadingZeros......................... 58 4.3.7 LongNumbers ........................ 58 4.3.8 Multi-line strings . 59 4.3.9 HereDocuments ....................... 59 4.3.10 HeredocIndentation . 59 4.3.11 HeredocTerminators . 59 4.3.12 HeredocQuoters ....................... 60 4.3.13 Barewords........................... 60 4.3.14 FatCommas.......................... 60 4.3.15 ThinCommas......................... 60 4.3.16 Low-PrecedenceOperators . 61 4.3.17 Lists .............................. 61 4.3.18 ListMembership ....................... 61 4.4 Variables................................ 62 4.4.1 Lexical Variables . 62 4.4.2 PackageVariables....................... 62 4.4.3 Localization . 62 4.4.4 Initialization . 63 4.4.5 Punctuation Variables . 63 4.4.6 Localizing Punctuation Variables . 63 4.4.7 MatchVariables........................ 63 4.4.8 Dollar-Underscore.. .. 64 4.4.9 ArrayIndices ......................... 64 4.4.10 Slicing . 64 4.4.11 SliceLayout.......................... 65 4.4.12 Slice Factoring . 65 4.5 ControlStructures .......................... 65 4.5.1 IfBlocks............................ 65 4.5.2 PostfixSelectors ....................... 66 4.5.3 OtherPostfixModifiers. 66 4.5.4 NegativeControlStatements. 66 4.5.5 C-styleLoops ......................... 66 4.5.6 Unnecessary Subscripting . 67 4.5.7 NecessarySubscripting . 67 4.5.8 IteratorVariables ....................... 67 4.5.9 Non-LexicalLoopIterators. 67 4.5.10 ListGeneration ........................ 67 4.5.11 List Selection . 68 4.5.12 ListTransformation. 68 4.5.13 ComplexMappings ...................... 68 4.5.14 List Processing Side Effects . 68 4.5.15 Multipart Selections . 69 3 4.5.16 ValueSwithces ........................ 69 4.5.17 TabularTernaries. 69 4.5.18 do-whileLoops ........................ 69 4.5.19 LinearCoding......................... 70 4.5.20 DistributedControl. 70 4.5.21 Redoing ............................ 70 4.5.22 LoopLabels.......................... 70 4.6 Documentation ............................ 71 4.6.1 TypesofDocumentation . 71 4.6.2 Boilerplates . 71 4.6.3 Extended Boilerplates . 73 4.6.4 Location............................ 73 4.6.5 Contiguity........................... 74 4.6.6 Position ............................ 74 4.6.7 TechnicalDocumentation . 74 4.6.8 Comments........................... 74 4.6.9 Algorithmic Documentation . 75 4.6.10 Elucidating Documentation . 75 4.6.11 DefenseDocumentation . 75 4.6.12 Indicative Documentation . 76 4.6.13 Discursive Documentation . 76 4.6.14 Proofreading ......................... 76 4.7 Built-in Functions . 76 4.7.1 Sorting............................. 77