DEV Community

Parzival
Parzival

Posted on

YAGNI (You Aren't Gonna Need It)

Core Principle

Only implement features when you actually need them, not when you think you might need them in the future.

Examples

Premature Abstraction (Bad)

// Over-engineered solution anticipating future needs
class UserService {
    private $database;
    private $cache;
    private $logger;
    private $notifications;
    private $analytics;

    public function __construct(
        DatabaseInterface $database,
        CacheInterface $cache,
        LoggerInterface $logger,
        NotificationService $notifications,
        AnalyticsService $analytics
    ) {
        $this->database = $database;
        $this->cache = $cache;
        $this->logger = $logger;
        $this->notifications = $notifications;
        $this->analytics = $analytics;
    }

    public function createUser($data) {
        $this->logger->info('Creating user');
        $user = $this->database->insert('users', $data);
        $this->cache->set('user_' . $user->id, $user);
        $this->notifications->sendWelcomeEmail($user);
        $this->analytics->trackUserCreation($user);
        return $user;
    }
}
Enter fullscreen mode Exit fullscreen mode

Simple Implementation (Good)

class UserService {
    private $database;

    public function __construct(DatabaseInterface $database) {
        $this->database = $database;
    }

    public function createUser($data) {
        return $this->database->insert('users', $data);
    }
}
Enter fullscreen mode Exit fullscreen mode

Real-World Scenarios

E-commerce Example

// Bad: Implementing unused features
class Product {
    private $name;
    private $price;
    private $stock;
    private $weight;          // Not needed yet
    private $dimensions;      // Not needed yet
    private $shippingZones;   // Not needed yet
    private $taxCategories;   // Not needed yet
    private $customFields;    // Not needed yet

    public function calculateShipping($address) {
        // Complex shipping calculation that isn't required
    }

    public function calculateTax($country) {
        // Complex tax calculation that isn't required
    }
}

// Good: Implementing what's needed now
class Product {
    private $name;
    private $price;
    private $stock;

    public function __construct(string $name, float $price, int $stock) {
        $this->name = $name;
        $this->price = $price;
        $this->stock = $stock;
    }
}
Enter fullscreen mode Exit fullscreen mode

Common Anti-Patterns

  1. Building complex configuration systems before needed
  2. Creating elaborate inheritance hierarchies for future extensibility
  3. Adding database fields for potential future features
  4. Implementing unneeded API endpoints
  5. Creating generic solutions for specific problems

Benefits

  1. Reduced codebase complexity
  2. Lower maintenance cost
  3. Faster development cycles
  4. Better focus on current requirements
  5. Less technical debt

Exception Cases

  1. Core architecture decisions
  2. Data structures that are expensive to change later
  3. Security considerations
  4. Regulatory compliance requirements

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay