Code Refactoring into Smaller Modules Quiz Quiz

Explore core concepts in refactoring code into smaller, modular components. This quiz assesses your understanding of modular design, function splitting, dependency management, and common pitfalls in breaking down large codebases for improved maintainability and testability.

  1. Identifying Beneficial Refactoring

    Given a function that handles reading input, processing data, and displaying results, which refactoring step best improves the code's modularity?

    1. Move the entire function into a larger class
    2. Remove all comments within the function
    3. Separate the function into three distinct modules: input, processing, and output
    4. Simply rename the function to something more descriptive

    Explanation: Breaking up the original function into separate modules for input, processing, and output aligns with the principle of single responsibility, directly improving modularity. Renaming the function does not change its internal structure or modularity. Removing comments only affects documentation, not code organization. Moving the function into a larger class can decrease modularity by increasing complexity and coupling.

  2. Understanding Single Responsibility

    Why is it important for each module or function to have a single, well-defined responsibility when refactoring code?

    1. It reduces dependencies and makes code easier to test and maintain
    2. It decreases the number of files in the project
    3. It helps in writing longer functions
    4. It ensures faster execution of the program

    Explanation: Assigning a single responsibility to each module minimizes dependencies and simplifies both testing and maintenance. Reducing file count is not a relevant goal for modularity. Neither single responsibility nor modularization directly impacts program execution speed. Writing longer functions generally contradicts the aim of single responsibility and improved modularity.

  3. Avoiding Tight Coupling

    While splitting a large module into smaller ones, what should you avoid to maintain loose coupling between modules?

    1. Limiting direct access to internal module data
    2. Defining clear interfaces between modules
    3. Sharing global variables across modules
    4. Documenting dependencies in a readme

    Explanation: Global variables shared across modules create tight coupling, which makes code harder to modify and maintain. Defining interfaces and limiting direct access to internal data are good practices that promote loose coupling. Documentation, while valuable, does not directly influence coupling but improves team understanding.

  4. Recognizing Code Smells

    If you notice a function with repeated code blocks performing similar tasks, what is the best approach to refactor for modularity in this scenario?

    1. Rename variables in each block to be consistent
    2. Increase the size of the existing function
    3. Extract the repeated code into a separate helper function called by the main function
    4. Copy and paste the repeated code into new places

    Explanation: Extracting repeated code into a helper function eliminates redundancy and enhances modularity and maintainability. Copying code increases duplication and potential errors. Renaming variables without reorganizing the code does not address repetition. Increasing function size with repeated code reduces readability and modularity.

  5. Testing After Refactoring

    What is the primary reason to run tests after refactoring a large function into multiple smaller modules?

    1. To verify code formatting matches the style guide
    2. To check if function names are long enough
    3. To ensure the refactored code maintains original functionality and no bugs were introduced
    4. To find opportunities to increase code comments

    Explanation: Running tests after refactoring confirms that the new modular code produces the same correct results and that no bugs have appeared during the process. Tests do not check function name length or comment opportunities. While code formatting is important, it is not the main reason for running functional tests after a refactor.