DEV Community

Cover image for 5 Lines of Code That Will Make Your Business Logic Beautiful

5 Lines of Code That Will Make Your Business Logic Beautiful

Crafts 69 Guy on August 19, 2025

Table of Contents The Before/After That Will Blow Your Mind We've All Been Here The Magic Moment That Changed Everything Try This Right...
Collapse
 
ravavyr profile image
Ravavyr

I seriously do not see how this is any more useful than writing the if/else logic.

You have to learn new syntax for some wrapper function.
You have to install it via npm
You will have to update it as it changes, probably at some point causing some conflict with some other terribly useless npm lib

It doesn't save you any time.
In fact, you have to learn more stuff to use it, so it's gonna slow you down more.

Collapse
 
crafts69guy profile image
Crafts 69 Guy

Hey! Thanks for the honest feedback - you're right that for most if/else logic, this would be overkill and just add complexity.
The main problem I was solving wasn't really about syntax or saving lines of code. It was this specific pain point: business rules that need to change without code deployments.
Here's the scenario that drove me to build this:

E-commerce site with discount rules that change every week
Business team: "Can you make it so we can update pricing rules without waiting for deployments?"
Me: sweating because all the logic was hardcoded in if/else statements

The key insight was JSON serializable rules. Instead of:

// This lives in your code and requires deployment to change
if (customer.type === 'vip' && order.total > 100) {
  applyDiscount(20);
} else if (customer.loyaltyPoints > 1000) {
  applyDiscount(15);
}
Enter fullscreen mode Exit fullscreen mode

you get:

// This JSON lives in your database and can be updated via admin panel
const ruleFromDatabase = {
  "or": [
    {"and": [{"eq": ["customer.type", "vip"]}, {"gte": ["order.total", 100]}]},
    {"gte": ["customer.loyaltyPoints", 1000]}
  ]
};

engine.evaluate(ruleFromDatabase, customerData);
Enter fullscreen mode Exit fullscreen mode

Now, business people can update rules through an admin interface, no developer needed, no deployment required.
You're 100% right that if your business logic is stable and you don't need database-driven rules, plain if/else is simpler and better. This is really for the specific case where rules need to be dynamic and configurable.
Thanks for keeping me honest about not overselling the use case! 🤠

Collapse
 
rkristelijn profile image
Remi Kristelijn

Yeah, that makes sense!

Thread Thread
 
crafts69guy profile image
Crafts 69 Guy

Thanks bro 🤠

Collapse
 
cyber8080 profile image
Cyber Safety Zone

Really enjoyed this! It’s impressive how just a few lines of clean, well-structured code can completely change the readability and maintainability of business logic. Often, simplicity is the hardest thing to achieve in coding. Thanks for sharing these insights—I’ll definitely try applying this approach in my next project!

Collapse
 
crafts69guy profile image
Crafts 69 Guy

Thanks! 😊 Excited to see how you use it. Feel free to drop any feedback or open issues on GitHub if you run into anything!

Collapse
 
prime_1 profile image
Roshan Sharma

Great job! It's always impressive to see how a few clean lines of code can replace a stack of messy logic. This type of refactoring not only makes the code shorter but also much easier to maintain.
I also checked the comments and noticed how you responded to them.

Collapse
 
crafts69guy profile image
Crafts 69 Guy

Thank you so much! 🙏 I appreciate you taking the time to check it out.

Collapse
 
rkristelijn profile image
Remi Kristelijn

This is awesome!

I do see a use case for this if you manage a lot of complex rules or the rules change a lot. Especially if it is JSON and can be managed without having to deploy new code.

However in this specific case I would settle for the data-hiding (or implementation hiding) pattern; using plain functions that can be unit tested:

if( (isActiveUser() && isAdult() ) || isAdmin())

What could be a good threshold when to switch to a rule based lib? More than 3 conditions?

Collapse
 
crafts69guy profile image
Crafts 69 Guy • Edited

Great question! You've hit on exactly the right trade-off analysis, bro.
Your approach with isActiveUser() && isAdult() || isAdmin() is actually perfect for most cases - cleaner, testable, and no dependencies.
But, I'd suggest considering a rule engine when you hit any of these thresholds:

Rules need to be configurable - Business team wants to change logic without deployments (like my business).
Cross-platform sharing - Same rules used in frontend, backend, and mobile as JSON
Visual rule builders - Non-technical people need to create/modify rules
Audit trails - Need to track "what rule was applied when"
Complex nesting - More than 3-4 levels deep of AND/OR combinations

The "3 conditions" threshold is interesting 😄, but I think it's less about quantity and more about changeability. If your 10 conditions are stable, keep the functions 😅. If your 2 conditions change weekly, JSON rules might save you.
Your pattern is definitely the sweet spot for most business logic. Rule engines shine specifically when the logic needs to live outside your codebase.
Really appreciate the thoughtful comparison! 🙏

Collapse
 
niradler profile image
Nir Adler

amm I think u nned to add benefits over zod and other schemes validator they will give much more but maybe u can be faster or other usecase where u can have edge on existing libs

Collapse
 
crafts69guy profile image
Crafts 69 Guy

You're spot on! Zod and others are incredible for schema validation - much more mature ecosystem.
Rule Engine JS tackles a different pain point: business logic that needs to live in databases. When your startup pivots and suddenly the discount rules change, or when compliance requires different validation in different regions.
I'd actually love to explore integrations - imagine Zod handling the data structure validation while Rule Engine JS manages the business rules. Best of both worlds!

Collapse
 
phantom0121 profile image
phantom

nice code

Collapse
 
crafts69guy profile image
Crafts 69 Guy

thanks bro 😎