www.buslab.orgPB173 1/11 PB173 - Tématický vývoj aplikací v C/C++ (podzim 2012) Skupina: Aplikovaná kryptografie a bezpečné programování https://minotaur.fi.muni.cz:8443/pb173_crypto Petr Švenda, svenda@fi.muni.cz Konzultace: G201, Pondělí 16-16:50 www.buslab.orgPB173 2/11 Refactoring Refactoring is process of restructuralization of source code to make it easier to understand and modify in future without changing its observable behaviour. No new functionality is added Existing code is rewritten, split, erased and otherwise modified to improve code quality www.buslab.orgPB173 3/11 Refactoring (2) Coding can be divided into two parts ● adding code for new functionality ● refactoring existing code Refactoring is necessary to keep code maintainable ● spaghetti code will sooner or later consume more time to maintain that to rewrite it Be aware – code rewrite might introduce new bugs Proper (automated) testing is required ● that’s why you have unit tests for! ● run these tests during refactoring www.buslab.orgPB173 4/11 Refactoring – refactoring techniques (De-)Composing methods properly Moving code between modules/classes Change data organization Making conditional expressions simpler Making API clearer … See http://sourcemaking.com/refactoring ● detailed explanation of many techniques with examples ● principles and practical tips how to solve problems www.buslab.orgPB173 5/11 Code extraction into separate function http://sourcemaking.com/refactoring/extract-method Locate function doing multiple functionalities Identify logical blocks of functionality Create new function ● with name describing What not How ● move code there, replace by function call ● think about others also using new function Take care of local variables ● pass them as function arguments www.buslab.orgPB173 6/11 Additional explanation variable http://sourcemaking.com/refactoring/introduce- explaining-variable Add additional well-named variable to hold intermediate value ● even when such variable is not necessary in principle Improve readability of code Increase possibility for debugging ● you can watch and conditionally break on variable www.buslab.orgPB173 7/11 Separate work done by single module/class http://sourcemaking.com/refactoring/extract-class Over the time, your module/class will grow ● one module/class is doing multiple functionalities Violation of several design principles Identify distinct functionalities ● usually set of methods and attributes responsible for single functionality Create new class(es) and move functions there ● separate interfaces for separate functionalities ● use multiple inheritance or aggregation to glue together www.buslab.orgPB173 8/11 Refactoring - tools Most of the work with refactoring is “manual” ● find out how to refactor and write simpler code Tools can still help ● identify problematic areas (Code metrics, SourceMonitor) ● provide call graph and data flow (Doxygen, VS Profiler) ● apply transformation consistently in all project files www.buslab.orgPB173 9/11 Refactoring – tools (2) No real build-in refact. tool for C/C++ in VS 2010 ● requires complete understanding of C/C++ code by refactoring tool Refactoring support for C/C++ in VS 2012 ● http://www.kunal-chowdhury.com/2012/06/refactor-your-code- easily-using-visual.html 3rd party add-ons like Visual Assist X / VSCommands ● not only refactoring support, but also code completition… ● http://www.agile-code.com/blog/list-of-visual-studio-code- refactoring-tools/ NetBeans (and others) have refactoring support ● http://wiki.netbeans.org/Refactoring ● variable renaming, code extraction… www.buslab.orgPB173 10/11 Source monitor Create new project ● File → New project ● language, directory with sources *.c / *.cpp ● initial ‘Baseline’ After code update ● Checkpoint → New checkpoint Details on particular checkpoint and file ● RClick → Display Function Metrics Details... www.buslab.orgPB173 11/11 Source monitor – example outputs Complexity: 1-10 (OK), 11-20 (sometimes), > 20 (BAD) www.buslab.orgPB173 12/11 Antipatterns Common defective process and implementation within organization Opposite to design patterns ● see http://sourcemaking.com/design_patterns Read http://sourcemaking.com/antipatterns ● good description, examples and how to solve Not limited to object oriented programming! Software development antipatterns ● http://sourcemaking.com/antipatterns/software- development-antipatterns www.buslab.orgPB173 13/11 Practical assignment - refactoring Use code metric tool to analyze your sources ● http://www.campwoodsw.com/sourcemonitor.html Find and refactor all functions ● with complexity more then 15 ● with Maximum Depth more then 4 Read and use refactoring techniques ● http://sourcemaking.com/refactoring