Do not let the infiltration code test to production

TL; Dr: Avoid adding Isesting Or similar flags.

Problems πŸ˜”

Solutions πŸ˜ƒ

  1. Eliminating IFS behavior
  2. Use dependent injection
  3. Typical external services (do not mock them)
  4. Separate configurations
  5. Insulation of the logic of the test
  6. Maintaining the limits of clean behavior

Refactorings βš™

Context πŸ’¬

When flags add like IsestingYou can mix the test code and production code.

This creates active hidden paths only in the tests.

Also, do not cover the real production code.

You risk the behavior of the shipping test to production, which leads to unexpected errors and behavior.

Code πŸ“–

Error ❌

struct PaymentService {
    is_testing: bool,
}

impl PaymentService {
    fn process_payment(&self, amount: f64) {
        if self.is_testing {
            println!("Testing mode: Skipping real payment");
            return;
        }
        println!("Processing payment of ${}", amount);
    }
}

Right πŸ‘‰

trait PaymentProcessor {
    fn process(&self, amount: f64);
}

struct RealPaymentProcessor;
impl PaymentProcessor for RealPaymentProcessor {
    fn process(&self, amount: f64) {
        println!("Processing payment of ${}", amount);
    }
}

struct TestingPaymentProcessor;
impl PaymentProcessor for TestingPaymentProcessor {
    // Notice this is not a mock
    fn process(&self, _: f64) {
        println!("No payment: Skipping real transaction");
    }
}

struct PaymentService {
    processor: T,
}

impl PaymentService {
    fn process_payment(&self, amount: f64) {
        self.processor.process(amount);
    }
}

Detection πŸ”

You can discover this smell by searching for conditional flags such as Isestingand Environment == “Test”and Debug_modeAnd expressions like this.

This indicates that the test behavior leaks into the production code.

Level πŸ”‹

Why the objection is important πŸ—Ί

You need a clear separation between the production test and the production code.

When mixing it, you break the single objection between real behavior and the program.

Since environments are entities in the real world, you need to be explicitly designed on the map.

Amnesty International Generation πŸ€–

The software instructions created from artificial intelligence often provides this smell when using fast tested breakthroughs.

Some tools suggest flags like Isesting Because it gives priority to ease on the appropriate design.

Discover artificial intelligence πŸ₯ƒ

Artificial intelligence tools can hunt this smell if you create it for a conditional logic mark based on test cases.

Try them! πŸ› 

Remember: Artificial Intelligence Assistants make many mistakes

A proposal router: Remove and replace the ISTISTING method with the modeling of environments

Conclusion 🏁

Avoid using Isesting information.

Using dependency injections and an environment model to maintain the logic of testing and production separate.

Relationships πŸ‘©β€β€

https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxii

https://haackernoon.com/how-to-find-the-stinky-barts-of-your-code-part-xiii

https://haackernoon.com/how-to-find-the-stinky-parts-of-your-code-bart-vi-cmj31om

Leave responsibility πŸ“˜

The smell of the code is my opinion.

Credit πŸ™

Photo by Christian Gertnbach on non -earthquakes


When adding the test flags, it undermines confidence in production.

Ward Knngham


This article is part of the Codesmell series.

By BBC

Leave a Reply

Your email address will not be published. Required fields are marked *