From 5a146694fba09fa492362d37b4a884227589fa45 Mon Sep 17 00:00:00 2001 From: tbrar06 Date: Wed, 15 Jan 2025 08:58:27 -0500 Subject: [PATCH 1/3] Added MIS for LongParameterListRefactorer --- docs/Design/SoftDetailedDes/MIS.tex | 94 +++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index b09df2c1..dd16441c 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -156,6 +156,100 @@ \section{Module Decomposition} \newpage ~\newpage +\section{MIS of LongParameterListRefactorer} \label{Module} \wss{Use labels for + cross-referencing} + +\wss{You can reference SRS labels, such as R\ref{R_Inputs}.} + +\wss{It is also possible to use \LaTeX for hypperlinks to external documents.} + +\subsection{Module} + +LongParameterListRefactorer is a module that identifies and refactors functions or methods with long parameter lists(detected beyond configured threshold) in Python code. The refactoring aims to improve code readability, maintainability, and energy efficiency by encapsulating related parameters into objects and removing unused ones. + +\subsection{Uses} + +Simplify function or method signature by (1) removing unused parameters (2) encapsulating related parameter groups into class instances. Update function body and corresponding function calls to match the refactored signature. Improve code maintainability and potentially reduce energy consumption associated with analyzing and maintaining long parameter lists. + +\subsection{Syntax} + +\subsubsection{Exported Constants} + +None + +\subsubsection{Exported Access Programs} + +\begin{center} +\begin{tabular}{|p{3cm}|p{5cm}|p{2cm}|p{3cm}|} +\hline +\textbf{Name} & \textbf{In} & \textbf{Out} & \textbf{Exceptions} \\ +\hline +\texttt{\_\_init\_\_} & \texttt{output\_dir: Path} & None & None \\ +\hline +\texttt{refactor} & \texttt{file\_path: Path, pylint\_smell: Smell, initial\_emissions: float} & None & Logging exceptions \\ +\hline +\end{tabular} +\end{center} + +\subsection{Semantics} + +\subsubsection{State Variables} + +\begin{itemize} + \item \texttt{parameter\_analyzer}: An instance of \texttt{ParameterAnalayzer} used to analyze parameter usage. + \item \texttt{parameter\_encapsulator}: An instance of \texttt{ParameterEncapsulator} used to create encapsulated parameter classes. + \item \texttt{function\_updater}: An instance of \texttt{FunctionCallUpdater} used to update function signatures and calls sites. +\end{itemize} + +\subsubsection{Environment Variables} + +\begin{itemize} +\item \textbf{File system}: Used for reading, writing, and storing temporary and refactored files. +\item \textbf{Logger}: Logs details of the refactoring process. +\end{itemize} + +\subsubsection{Assumptions} + +\begin{itemize} + \item Input files are valid Python scripts. + \item Smells identified by \texttt{pylint\_smell} include valid line numbers. + \item Refactored code must pass the provided test suite. +\end{itemize} + +\subsubsection{Access Routine Semantics} + +\paragraph{\texttt{init(output\_dir: Path)}} +\begin{itemize} +\item \textbf{Transition}: Initializes the refactorer with the specified output directory. +\item \textbf{Output}: None. +\item \textbf{Exception}: None. +\end{itemize} + +\paragraph{\texttt{refactor(file\_path: Path, pylint\_smell: Smell, initial\_emissions: float)}} +\begin{itemize} +\item \textbf{Transition}: +\begin{enumerate} +\item Reads the file at \texttt{file\_path} and locates the target function using \texttt{pylint\_smell}. +\item Analyzes function body to remove unused parameters. Updates the function signature and references in the code accordingly. +\item If number of used parameters also exceeds the maximum configured limit, encapsulates related parameters into classes. Updates the function signature and references in the code accordingly. +\item Writes the refactored code to a temporary file. +\end{enumerate} +\item \textbf{Output}: None.Refactored file is saved if improvements are validated. +\item \textbf{Exception}: Logs exceptions during file operations or the refactoring process. +\end{itemize} + +\subsubsection{Local Functions} + +\begin{enumerate} +\item \texttt{get\_used\_parameters(function\_node: ast.FunctionDef, params: list[str]) -> set[str]}: Identifies parameters used within the function body. +\item \texttt{get\_parameters\_with\_default\_value(default\_values: list[ast.Constant], params: list[str]) -> dict}: Maps parameter names to their default values. +\item \texttt{classify\_parameters(params: list[str]) -> dict}: Classifies parameters into \texttt{data} and \texttt{config} groups based on naming conventions. +\item \texttt{create\_parameter\_object\_class(param\_names: list[str], default\_value\_params: dict, class\_name: str) -> str}: Generates class definitions for encapsulating parameters. +\item \texttt{update\_function\_signature(function\_node: ast.FunctionDef, params: dict) -> ast.FunctionDef}: Updates function signatures to use encapsulated parameter objects. +\item \texttt{update\_parameter\_usages(function\_node: ast.FunctionDef, params: dict) -> ast.FunctionDef}: Replaces parameter usages within the function body with attributes of encapsulated objects. +\item \texttt{update\_function\_calls(tree: ast.Module, function\_node: ast.FunctionDef, params: dict) -> ast.Module}: Updates all calls to the refactored function. +\end{enumerate} + \section{MIS of \wss{Module Name}} \label{Module} \wss{Use labels for cross-referencing} From 0bb531d28eee60fd26359ede39d59892a23dc3fd Mon Sep 17 00:00:00 2001 From: tbrar06 Date: Fri, 17 Jan 2025 11:50:35 -0500 Subject: [PATCH 2/3] Updated MG and MIS for LPL --- docs/Design/SoftArchitecture/MG.tex | 244 +++++++++++++++++++++++----- docs/Design/SoftDetailedDes/MIS.tex | 6 +- 2 files changed, 204 insertions(+), 46 deletions(-) diff --git a/docs/Design/SoftArchitecture/MG.tex b/docs/Design/SoftArchitecture/MG.tex index 86f8d76f..20c157ff 100644 --- a/docs/Design/SoftArchitecture/MG.tex +++ b/docs/Design/SoftArchitecture/MG.tex @@ -157,12 +157,56 @@ \subsection{Anticipated Changes} \label{SecAchange} change. \begin{description} -\item[\refstepcounter{acnum} \actheacnum \label{acHardware}:] The specific - hardware on which the software is running. -\item[\refstepcounter{acnum} \actheacnum \label{acInput}:] The format of the - initial input data. -\item ... -\end{description} + \item[\refstepcounter{acnum} \actheacnum \label{acUserInterface}:] The user interface of the plugin. Enhancements may be required to improve usability or accommodate new features. Specific anticipated changes include: + \begin{itemize} + \item \textbf{Refactoring Suggestions Display}: Updates to how refactoring suggestions are presented, such as side-by-side views of original and refactored code. + \item \textbf{Theme Support}: Adding compatibility with various VS Code themes, including light and dark modes. + \item \textbf{Visual Indicators}: Implementing color-coded indicators to highlight the impact of energy savings for each refactoring suggestion. + \item \textbf{Interactive Elements}: Introducing interactive components like tooltips or progress indicators to guide users during the refactoring process. + \item \textbf{Customization Options}: Allowing users to configure UI elements, such as adjusting the sensitivity of code smell detection or selecting preferred refactoring styles. + \end{itemize} + + \item[\refstepcounter{acnum} \actheacnum \label{acVSCodePlugin}:] The VS Code plugin's + functionality. Future versions may expand to support more complex refactorings + or additional code smells that users can address with minimal setup. Changes + may involve adding more customizable user settings. + + \item[\refstepcounter{acnum} \actheacnum \label{acRefactorers}:] The refactorers responsible + for detecting and fixing specific code smells. As more code smells are identified + and refactoring techniques are developed, new modules may be added or existing + ones may evolve. For example: + \begin{itemize} + \item \textbf{Base Refactorer} \label{acBaseRefactorer}: Updates to the base refactorer module to + support new refactoring patterns or improved algorithms. + \item \textbf{Complex List Comprehension} \label{acComplexListComprehension}: Adding or modifying + the logic for simplifying complex list comprehensions. + \item \textbf{Long Element Chain} \label{acLongElementChain}: Refining the logic to handle longer + chains of elements and optimize their readability and performance. + \item \textbf{Long Lambda Function} \label{acLongLambdaFunction}: Improvements to better handle + long lambda functions, making them more efficient and readable. + \item \textbf{Long Message Chain} \label{acLongMessageChain}: Extending the module's ability + to identify and refactor long message chains. + \item \textbf{Member Ignoring Method} \label{acMemberIgnoringMethod}: Enhancements to the + module for detecting methods that ignore members, optimizing the code structure. + \item \textbf{Repeated Calls} \label{acRepeatedCalls}: Optimizing detection and handling + of repeated function calls to improve performance. + \item \textbf{String Concatenation in Loop} \label{acStringConcatenationInLoop}: Adjusting + the refactorer's logic to improve handling of string concatenation within loops. + \item \textbf{Long Parameter List} \label{acLongParameterList}: Future extensions to handle + complex parameter lists in a more structured manner, perhaps allowing for simplifications. + \end{itemize} + + \item[\refstepcounter{acnum} \actheacnum \label{acSmell}:] The core logic + for identifying specific code smells. As the system evolves, new code smells may be added to the system’s detection capabilities, necessitating changes to this module. + + \item[\refstepcounter{acnum} \actheacnum \label{acAnalyzer}:] The analyzers used to + gather metrics and assess code quality. + + \item[\refstepcounter{acnum} \actheacnum \label{acTesting}:] The testing module + responsible for ensuring the correct functionality of the refactorers. As new + features and code smells are added, this module may need updates to test these + changes thoroughly. +\end{description} \wss{Anticipated changes relate to changes that would be made in requirements, design or implementation choices. They are not related to changes that are made @@ -178,9 +222,9 @@ \subsection{Unlikely Changes} \label{SecUchange} decisions will be changed. \begin{description} -\item[\refstepcounter{ucnum} \uctheucnum \label{ucIO}:] Input/Output devices - (Input: File and/or Keyboard, Output: File, Memory, and/or Screen). -\item ... + \item[\refstepcounter{ucnum} \uctheucnum \label{ucPlatform}:] Transitioning from VS Code to another IDE. The plugin is tightly integrated with VS Code's API, making such a change complex. + \item[\refstepcounter{ucnum} \uctheucnum \label{ucCoreLogic}:] Fundamental changes to the core logic of code smell detection. The current architecture is designed around widely accepted principles of software quality. + \item[\refstepcounter{ucnum} \uctheucnum \label{ucPluginType}:] Changing from a plugin-based architecture to a standalone application. This would require rethinking the entire deployment and user interaction model. \end{description} \section{Module Hierarchy} \label{SecMH} @@ -192,7 +236,17 @@ \section{Module Hierarchy} \label{SecMH} \begin{description} \item [\refstepcounter{mnum} \mthemnum \label{mHH}:] Hardware-Hiding Module -\item ... +\item [\refstepcounter{mnum} \mthemnum \label{mM}:] Measurements Module +\item [\refstepcounter{mnum} \mthemnum \label{mSmell}:] Smell Module +\item [\refstepcounter{mnum} \mthemnum \label{mBR}:] BaseRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mMIMR}:] MakeStaticRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mSCLR}:] UseListAccumulationRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mUGENR}:] UseAGeneratorRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mCRC}:] CacheRepeatedCallsRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mLEC}:] LongElementChainRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mLPL}:] LongParameterListRefactorer Module +\item [\refstepcounter{mnum} \mthemnum \label{mPyA}:] Pylint Analyzer Module +\item [\refstepcounter{mnum} \mthemnum \label{mTest}:] Testing Functionality Module \end{description} @@ -203,22 +257,30 @@ \section{Module Hierarchy} \label{SecMH} \textbf{Level 1} & \textbf{Level 2}\\ \midrule -{Hardware-Hiding Module} & ~ \\ +{Hardware-Hiding Module} & None \\ \midrule -\multirow{7}{0.3\textwidth}{Behaviour-Hiding Module} & ?\\ -& ?\\ -& ?\\ +\multirow{7}{0.3\textwidth}{Behaviour-Hiding Module} & Smell Module\\ +& BaseRefactorer Module\\ +& CacheRepeatedCallsRefactorer Module\\ +& MakeStaticRefactorer Module\\ +& UseListAccumulationRefactorer Module\\ & ?\\ +& UseAGeneratorRefactorer Module\\ & ?\\ +& LongElementChainRefactorer Module\\ +& LongParameterListRefactorer Module\\ & ?\\ & ?\\ & ?\\ \midrule \multirow{3}{0.3\textwidth}{Software Decision Module} & {?}\\ +& Measurements Module\\ & ?\\ -& ?\\ +\multirow{3}{0.3\textwidth}{Software Decision Module} & Pylint Analyzer Module\\ +& Testing Functionality Module\\ +& Measurements Module\\ \bottomrule \end{tabular} @@ -255,42 +317,88 @@ \section{Module Decomposition} \label{SecMD} (\emph{--}) is shown, this means that the module is not a leaf and will not have to be implemented. -\subsection{Hardware Hiding Modules (\mref{mHH})} +\subsection{Hardware Hiding Modules} + +This system has no hardware components. + +\subsection{Behaviour-Hiding Module} + +\subsubsection{Smell Module (\mref{mSmell})} \begin{description} -\item[Secrets:]The data structure and algorithm used to implement the virtual - hardware. -\item[Services:]Serves as a virtual hardware used by the rest of the - system. This module provides the interface between the hardware and the - software. So, the system can use it to display outputs or to accept inputs. -\item[Implemented By:] OS +\item[Secrets:] Data structure of a code smell. +\item[Services:] Provides an interface for other modules to access information of a smell object. +\item[Implemented By:] EcoOptimizer +\item[Type of Module:] Abstract Data Type \end{description} -\subsection{Behaviour-Hiding Module} +\subsubsection{Base Refactorer Module (\mref{mBR})} + +% [Record, Library, Abstract Object, or Abstract Data Type] \begin{description} -\item[Secrets:]The contents of the required behaviours. -\item[Services:]Includes programs that provide externally visible behaviour of - the system as specified in the software requirements specification (SRS) - documents. This module serves as a communication layer between the - hardware-hiding module and the software decision module. The programs in this - module will need to change if there are changes in the SRS. -\item[Implemented By:] -- + \item[Secrets:] None + \item[Services:] Offers an interface for other refactoring modules to implement. + \item[Implemented By:] EcoOptimizer \end{description} -\subsubsection{Input Format Module (\mref{mInput})} +\subsubsection{MakeStaticRefactorer Module (\mref{mMIMR})} +% [Record, Library, Abstract Object, or Abstract Data Type] \begin{description} -\item[Secrets:]The format and structure of the input data. -\item[Services:]Converts the input data into the data structure used by the - input parameters module. -\item[Implemented By:] [Your Program Name Here] -\item[Type of Module:] [Record, Library, Abstract Object, or Abstract Data Type] - [Information to include for leaf modules in the decomposition by secrets tree.] +\item[Secrets:] How to parse a given code file to its AST representation, how to traverse the AST tree, how to modify specific nodes in the AST tree, how to convert the modified AST tree back to source code and write it to an output file. +\item[Services:] Refactors the \textit{\textbf{Member Ignoring Method (MIM)}} smell in a provided code file to improve energy efficiency. +\item[Implemented By:] EcoOptimizer \end{description} -\subsubsection{Etc.} +\subsubsection{UseListAccumulationRefactorer Module (\mref{mSCLR})} + +% [Record, Library, Abstract Object, or Abstract Data Type] + +\begin{description} +\item[Secrets:] How to parse a given code file into its AST representation, how to traverse the AST tree, how to find the initializing variable of the string concatenation, how to find the scope of the concatenation, how to modify the given code file in plain text and write it back to an output file. +\item[Services:] Refactors the \textbf{\textit{String Concatenation Inside Loop (SCL)}} smell in a provided code file to improve energy efficiency. +\item[Implemented By:] EcoOptimizer +\end{description} + +\subsubsection{UseAGeneratorRefactorer Module (\mref{mUGENR})} +\begin{description} + \item[Secrets:] How to parse a given code file to its AST representation, how to traverse the AST tree, how to modify specific nodes in the AST tree, how to convert the modified AST tree back to source code and write it to an output file. + \item[Services:] Refactors the \textit{List Comprehension Instead of a Generator} smell in a provided code file to improve energy efficiency. + \item[Implemented By:] EcoOptimizer +\end{description} + +\subsubsection{CacheRepeatedCallsRefactorer Module (\mref{mCRC})} +\begin{description} + \item[Secrets:] How to parse a given code file to its AST representation, how to traverse the AST tree, how to modify specific nodes in the AST tree, how to convert the modified AST tree back to source code and write it to an output file. + \item[Services:] Refactors the \textit{Repeated Function Calls} smell in a provided code file to improve energy efficiency and performance. + \item[Implemented By:] EcoOptimizer +\end{description} + +\subsubsection{Long Element Chain Module (\mref{mLEC})} +% [Record, Library, Abstract Object, or Abstract Data Type] + +\begin{description} + \item[Secrets:] How to parse a given code file to its AST representation, traverse the + AST tree to identify dictionary assignments, analyze the structure of nested dictionaries, + and flatten them. Additionally, it identifies all access calls associated with these dictionaries + in the source code and determines how to update them to reflect the new flattened structure. + \item[Services:] Detects nested dictionaries in the source code using AST parsing, simplifies their + structure by flattening them, and updates all associated access calls throughout the file. This improves + code readability, reduces complexity, and ensures correctness while maintaining the program's intended behavior. + \item[Implemented By:] EcoOptimizer +\end{description} + +\subsubsection{Long Parameter List Module (\mref{mLPL})} + +% [Record, Library, Abstract Object, or Abstract Data Type] + +\begin{description} + \item[Secrets:] How to parse a given code file to its AST representation, traverse the AST tree to identify functions or methods with long parameter lists, and encapsulate related parameters into objects or structures. Additionally, it identifies all function or method calls associated with these functions and updates their arguments to align with the refactored signature. + \item[Services:] Detects long parameter lists in functions or methods using AST parsing, simplifies their structure by grouping related parameters into objects or structures, and updates all associated function or method calls throughout the file. This improves code readability, reduces complexity, and ensures correctness while maintaining the program’s intended behavior. + \item[Implemented By:] EcoOptimizer +\end{description} \subsection{Software Decision Module} @@ -305,7 +413,40 @@ \subsection{Software Decision Module} \item[Implemented By:] -- \end{description} +\subsection{Measurements Module} + +\begin{description} +\item[Secrets:] How to measure energy consumption and carbon emissions of a given Python program using the CodeCarbon library, including managing temporary directories for storing output, executing the program, and processing the emissions data from a CSV file. +\item[Services:] Provides functionality for measuring the energy consumption and carbon emissions of a provided code file. This module handles execution, tracking, and data extraction, ensuring that the emissions data is available for further analysis. +\item[Implemented By:] CodeCarbonEnergyMeter +\end{description} + \subsubsection{Etc.} +\subsubsection{Pylint Analyzer Module} + +\subsubsection{Pylint Analyzer Module (\mref{mPyA})} + +\begin{description} +\item[Secrets:] The internal design and execution of static code analysis using Pylint and AST parsing, including custom detection and structuring of smells. These details are hidden from external modules. +\item[Services:] The module provides the following services: + \begin{itemize} + \item Executes Pylint analysis on Python source code files. + \item Performs AST-based custom checks for specific code smells. + \item Filters and structures the analysis results into a standardized format for further processing. + \end{itemize} +\item[Implemented By:] \texttt{EcoOptimizer} +\end{description} + +\subsubsection{Testing Functionality Module (\mref{mTest})} + +% [Record, Library, Abstract Object, or Abstract Data Type] + +\begin{description} + \item[Secrets:] Runs testing suites in a subprocess. + \item[Services:] Checks whether the functionality of a given source code has changed. + \item[Implemented By:] EcoOptimizer +\end{description} + \section{Traceability Matrix} \label{SecTM} @@ -385,7 +526,7 @@ \section{Use Hierarchy Between Modules} \label{SecUse} \begin{figure}[H] \centering -%\includegraphics[width=0.7\textwidth]{UsesHierarchy.png} +\includegraphics[width=\textwidth]{../../Images/use_hierarchy_modules.png} \caption{Use hierarchy among modules} \label{FigUH} \end{figure} @@ -403,9 +544,30 @@ \section{Design of Communication Protocols} \section{Timeline} -\wss{Schedule of tasks and who is responsible} +All code and corresponding documentation was aimed to be completed before Jan 6th our Demo date for our supervisor. -\wss{You can point to GitHub if this information is included there} +\begin{table}[h!] + \centering + \caption{Timeline} + \begin{tabular}{|l|l|l|} + \hline + \textbf{Module Name} & \textbf{Team Member} & \textbf{Due Date} \\ \hline + Base Refactorer & Sevhena Walker & Jan 6, 2025 \\ \hline + Complex List Comprehension & Nivetha Kuruparan & Jan 6, 2025 \\ \hline + Long Element Chain & Ayushi Amin & Jan 6, 2025 \\ \hline + Long Lambda Function & Mya Hussain & Jan 6, 2025 \\ \hline + Long Message Chain & Mya Hussain & Jan 6, 2025 \\ \hline + Member Ignoring Method & Sevhena Walker & Jan 6, 2025 \\ \hline + Repeated Calls & Nivetha Kuruparan & Jan 6, 2025 \\ \hline + String Concatenation in Loop & Sevhena Walker & Jan 6, 2025 \\ \hline + Long Parameter List & Tanveer Brar & Jan 6, 2025 \\ \hline + Smell & All & Jan 31, 2025 \\ \hline + Analyzers & All & Jan 31, 2025 \\ \hline + Measurements & All & Jan 31, 2025 \\ \hline + Testing Functionality & All & Jan 31, 2025 \\ \hline + \end{tabular} + \end{table} + \bibliographystyle {plainnat} \bibliography{../../../refs/References} diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index dd16441c..8f6c38ee 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -195,11 +195,7 @@ \subsection{Semantics} \subsubsection{State Variables} -\begin{itemize} - \item \texttt{parameter\_analyzer}: An instance of \texttt{ParameterAnalayzer} used to analyze parameter usage. - \item \texttt{parameter\_encapsulator}: An instance of \texttt{ParameterEncapsulator} used to create encapsulated parameter classes. - \item \texttt{function\_updater}: An instance of \texttt{FunctionCallUpdater} used to update function signatures and calls sites. -\end{itemize} +None \subsubsection{Environment Variables} From 773f8fc227c280f7884a518a34ca6ddb24bc3ce8 Mon Sep 17 00:00:00 2001 From: tbrar06 Date: Fri, 17 Jan 2025 12:12:30 -0500 Subject: [PATCH 3/3] Updated uses to reflect dependencies --- docs/Design/SoftDetailedDes/MIS.tex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index e8d092ab..f449536e 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -168,7 +168,11 @@ \subsection{Module} \subsection{Uses} -Simplify function or method signature by (1) removing unused parameters (2) encapsulating related parameter groups into class instances. Update function body and corresponding function calls to match the refactored signature. Improve code maintainability and potentially reduce energy consumption associated with analyzing and maintaining long parameter lists. +\begin{itemize} + \item Uses \texttt{Smell} interface for data access + \item Inherits from \texttt{BaseRefactorer} + \item Inherits from Python's \texttt{ast} module's \texttt{NodeTransformer} +\end{itemize} \subsection{Syntax}