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 ๐
- Eliminating IFS behavior
- Use dependent injection
- Typical external services (do not mock them)
- Separate configurations
- Insulation of the logic of the test
- 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.