gtag('config', 'G-0PFHD683JR');
Price Prediction

The smell of code 293 โ€“ You should avoid adding IStesting or similar flags

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 Isestingfor Environment == โ€œTestโ€for 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 the fast breakthroughs for the test.

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 the conditional logic mark based on test situations.

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.

Related Articles

Leave a Reply

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

Back to top button