Chapter 5: Tools

Chapter 5: Tools

Note of Working Effectively with Legacy Code

Automated Refactoring Tools

Don't trust your refactoring tools.

I think that is what the writer wants us to know.

Here is an example.

// I know that isn't a proper name, I know it.
class A {
    private alpha: number = 0;
    private getValue(): number {
        this.alpha++;
        return 12;
    }
    public doSomething(): number {
        const v = this.getValue();
        let total = 0;
        for( let i = 0; i < 10; i++) {
            total += v;
        }
        return total;
    }

    public getValueOfAlpha() {
        return this.alpha;
    }
}

const a: A = new A();
console.log('result of a: ' + a.doSomething());
console.log('alpha of a: ' + a.getValueOfAlpha());
// [LOG]: "result of a: 120" 
// [LOG]: "alpha of a: 1"

This is just an ugly but very ordinary class, and the refactoring tool gets something for us to fix; it may say: 'Hey, I hate the v variable very much, and my dear master, why don't we just delete it?'

And you probably feel that isn't a big deal; after all, you trust your tools very much.

So this pathetic thing will happen.

// well, It's not my fault; if you feel sike of the class name,
// find the writer himself to blame.
class A {
    private alpha: number = 0;
    private getValue(): number {
        this.alpha++;
        return 12;
    }
    public doSomething(): number {
        let total = 0;
        for( let i = 0; i < 10; i++) {
            // that is exactly why I hate the side effect so much
            total += this.getValue();
        }
        return total;
    }

    public getValueOfAlpha() {
        return this.alpha;
    }
}

const a: A = new A();
console.log('result of a: ' + a.doSomething());
console.log('alpha of a: ' +a.getValueOfAlpha());
// [LOG]: "result of a: 120" 
// [LOG]: "alpha of a: 10"

Look at the poor alpha variable; after the operation of this stupid refactoring tool, it is changed from 1 to 10. By the way, that is the magic of method with the side effect, surprise!

Stop trusting your refactoring tools, even though they have become so bright nowadays.

Mock Objects

You should check this website: mockobjects

Unit-Testing Harnesses

Barely anything to discuss. The author talked about the JUnit, CppUnitLite, and NUnit, but I am a frontend programmer, so unless he talks more about JavaScript Unit Test Harnesses, I don’t care.

General Test Harnesses

Framework for Integrated Test(FIT)

FIT accepts HTML, runs tests defined in HTML tables in it, and produces HTML output

As we all know, HTML is built for people to read quickly, so some guy, maybe your boss, will write the results he wants the app to give him in HTML. (well, I don't think bosses can do that, whatever. ) And you, dear programmer, will take the honor to implement the app to fit that HTML, in other words, passes the FIT.