Introduction and Context Setting

In software design, understanding and addressing code smells, such as "Feature Envy," is vital for maintaining and improving code quality. Code smells are indicators of potential issues in your codebase that may hinder readability and maintainability. Feature Envy specifically arises when a method in a class has excessive interactions with the data of another class, often leading to a tangled code structure that is difficult to test and maintain.

Refactoring is the process of restructuring existing code to enhance its readability, maintainability, and performance without altering its external behavior. Common refactoring patterns, such as the Move Method, can be employed to address code smells like Feature Envy. This involves relocating methods to the class that holds the data they depend on, reducing unnecessary dependencies and improving cohesion.

In this course, we employ Test Driven Development (TDD) practices using Kotlin, the JUnit framework. Kotlin offers significant advantages with its modern syntax and language features, aiding in reducing runtime errors, while JUnit provides an efficient and comprehensive testing framework. We emphasize the TDD cycle — Red, Green, Refactor — to incrementally evolve the code with confidence, ensuring each step is supported by a comprehensive suite of tests.

Overview of the Current Codebase

In the upcoming practices, we'll focus on a GradeAnalyzer component that analyzes student grades. However, certain methods in the GradeAnalyzer class exhibit Feature Envy by deeply interacting with Student class data.

Example: Identifying Feature Envy

To successfully identify Feature Envy, focus on methods within a class that interact disproportionately with another class’s data. In our case, observe the function calculateFinalGrade() in GradeAnalyzer, which processes the grades owned by the Student class.

Here’s a glimpse of the current method:

class GradeAnalyzer(private val student: Student) {

    fun calculateFinalGrade(): Double {
        val grades = student.grades
        if (grades.isEmpty()) return 0.0

        var totalEarned = 0.0
        var totalPossible = 0.0

        for (grade in grades) {
            totalEarned += if (isLateSubmission(grade)) {
                grade.score * 0.9 // 10% penalty for late submissions
            } else {
                grade.score
            }
            totalPossible += grade.totalPoints
        }

        return (totalEarned / totalPossible) * 100
    }

    // Assume method implementation
    private fun isLateSubmission(grade: Grade): Boolean {
        // Logic to determine late submission
        return false
    }
}

Since calculateFinalGrade() relies on Student data, any changes to Student’s data structure might require updates to calculateFinalGrade(), creating a tight dependency. calculateFinalGrade() frequently accesses student.grades and operates on each grade item’s data, suggesting that it may be better suited to the Student class. Additionally, testing for calculateFinalGrade() may require a Student instance with specific data, complicating the testing process. It can also result in reduced readability. When a method interacts heavily with another class’s data, understanding which class owns the data becomes confusing.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal