Question

Find a source code of a program or developed software system. You can select any source...

Find a source code of a program or developed software system. You can select any source code in any programming language. Provide a brief description of the source code you chose and properly cite the source.
Find two code “bad smells” on the selected code, and then refactor the code with an appropriate refactoring method. You should specify the bad smells that you found and the refactoring method that used. You should provide the original code and the Refactored code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 //Source code with code smell class Person { // code smell - Temporary Field public int getScore(int a, int b){ int score = a * b; if(score > 100){ return score * 0.95; } else { return score * 0.25; } } } // code smell - duplicate code class Customer extends Person { // calculate age public function getAge() { return date('Y') - date('Y', strtotime("2012-10-5")); } } class Vendor extends Person { // calculate age public function getAge() { return date('Y') - date('Y', strtotime("2012-10-5")); } }

Refactored code:

 // Refactored code class Person { // removed temporary variable public int getScore(int a, int b) { if(a * b > 100){ return a * b * 0.95; } else { return a * b * 0.25; } } // removed duplicate code and moved to parent class public function getAge() { return date('Y') - date('Y', strtotime("2000-11-25")); } } class Customer extends Person { } class Vendor extends Person { }

I have added my custom method for another code smell.

Two code smells found in code:

  1. Temporary Field
  2. Duplicated code

1. Temporary field :

There was temporary field score in getScore(int,int) method

2. Duplicated code:

There was duplicate code for getting age in Customer and Vendor class in getAge() method.

Refactoring method used:

  1. Temporary field: removed temporary variable score and reaplced with expression
  2. Duplicated code: We have two duplicated methods in different classes that extend the same class, we can move the method in the parent class and delete it from the other two subclasses.

Note : Source code is written in Java

Add a comment
Know the answer?
Add Answer to:
Find a source code of a program or developed software system. You can select any source...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design Java (source code) a program (name it IndexOfLargest) to find the index of the first...

    Design Java (source code) a program (name it IndexOfLargest) to find the index of the first largest value in the array. Note that the largest value may appear more than once in the array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method findIndex() that takes a single-dimensional array of integer values and return the index of the first...

  • Program by force brute in (c++ or java language )

    The question of validity is to answer the question of whether for a Boolean expression consisting of n variables . There is a combination of values of variables that make the result of the expression true or not. In this phrase each The variable can be simple or contradictory. Force brute method of all compounds . Creates and evaluates the possible and the first time the result is true (if it is) the answer to the problem Come and stop...

  • If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry...

    If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry store and some of the Inventory (rings/necklaces/earrings) purchased there (The Jewelry store can be real or not) using Object-Oriented Programming (OOP). The important aspect of object-oriented programming is modular development and testing of reusable software modules. You love selling rings, necklaces, and earrings as well as programming and have decided to open a Jewelry store. To save...

  • Write a software key-logger and test it while you fill out a web form or type...

    Write a software key-logger and test it while you fill out a web form or type in the contents of a document using your favorite document preparation software. You can use Java or Python to do this assignment. C and C related programming language is not allowed for this assignment. Hints: You can find a demo and API for key-logger hook in the following link for JAVA. https://github.com/kwhat/jnativehook This project can only be used to learn how to use JNI...

  • PSEUDOCODE AND JAVA SOURCE CODE PLEASE: Program 1 (60 pts): Find the Rabbit! For this assignment,...

    PSEUDOCODE AND JAVA SOURCE CODE PLEASE: Program 1 (60 pts): Find the Rabbit! For this assignment, you will write a program that asks the user to find a rabbit in one of 10 holes (0-9). The holes and guesses are visualized on the screen (as shown below), but as the user guesses, you should not show where the rabbit is hidden. The rabbit is assigned a random hole each time the program is run. You must write and use at...

  • Write pseudocode and the write the source code in C++, thanks! Program 0: Insulted yet? Most...

    Write pseudocode and the write the source code in C++, thanks! Program 0: Insulted yet? Most people get harassed by telemarketers, so for your warmup question, you're going to design (pseudocode) and write (source code) a program that generates (clean) insults at random. The program must have a function that takes in the name of a person and prints out an insult directed to them. Further, the program should ask if the person has had enough insults and continue until...

  • PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of...

    PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work is the same as...

  • help me Problem: Write a simple C program that calculates the corresponding y-coordinate for any x-coordinate...

    help me Problem: Write a simple C program that calculates the corresponding y-coordinate for any x-coordinate onaline defined by two points. These points areinputspecifiedas pairsofxandycoordinates in the two-dimensional Cartesian coordinate system. Youneed to determine the slope ofthe line and they-intercept for the line plotted by these two points and store this data in memory. The programmustpromptthe userforthe coordinates of the first point X1 and then Y1, and then prompt the user for the second point, X2 and then Y2. After...

  • I need this Program code made for Java please with the sample results. Echo Input from...

    I need this Program code made for Java please with the sample results. Echo Input from the Console to the Screen Using Methods Review the resources and instructions in the Discussion Prep Study before completing this discussion. For this discussion, you practiced using Java methods to echo input from the console to the screen. Create a Java program that reads in any value entered at the console and then prints it out to the screen. The program must have at...

  • Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...

    Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT