# Duplicated code

Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.

When coding I focus mostly on duplication. When the same thing is done over and over, its a sign that there is an idea in our mind that is no well represented in the code. I try to figure out what it is, then I try to express that idea more clearly. ~ Ron Jeffries

• When you have the same expression in two methods of the same class. (Extract method)
• When you have the same expression in two sibling subclasses. (Pull up method)
• If the code is similar but not the same. (Form template method)
• If the methods do the same thing with different algorithm. (Substitute Algorithm)
• If you have duplicated code in two unrelated classes. (Extract class)

I will show you an example of the first case (Extract Method). Consider the following snippet of code.

@Test
public void shouldFullAParkingLot() throws Exception {
assertFalse(parkingLot.isFull());
for(inti=0;i<MAX_CAPACITY;i++){
parkingLot.park();
}
}

@Test(expected=ParkingLotOverFlowException.class)
public void shouldThrowAnExceptionSinceIsFullAndWantToPark throws Exception {
assertFalse(parkingLot.isFull());
for(inti=0;i<MAX_CAPACITY;i++){
parkingLot.park();
}
parkingLot.park();
}


As you can see the code is almost identical in the two tests, although they test different scenarios, so we applied extract method in order to avoid duplication.

@Test
public void shouldFullAParkingLot() throws Exception {
fullMyParkingLot();
}

@Test(expected=ParkingLotOverFlowException.class)
public void shouldThrowAnExceptionSinceIsFullAndWantToPark throws Exception {
fullMyParkingLot();
parkingLot.park();
}

private void fullMyParkingLot() throws ParkingLotOverFlowException {
assertFalse(parkingLot.isFull());
for(inti=0;i<MAX_CAPACITY;i++){
parkingLot.park();
}
assertTrue(parkingLot.isFull());
}


The biggest problem with extract method is dealing with local variables, and temps are one of the main resources of this issue. ~ Martin Fowler.

git clone https://github.com/josdem/refactoring.git
git fetch
git checkout feature/duplicated-code-setup
git checkout feature/duplicated-code-complete


To run the project:

gradle test


Read more, returning to the main article. Refactoring
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2524549663066864, "perplexity": 2148.786437001052}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-35/segments/1566027316021.66/warc/CC-MAIN-20190821131745-20190821153745-00031.warc.gz"}