Seven Deadly Sins of Programming

Jul 15, 2024

Seven Deadly Sins of Programming Lecture Notes

Introduction

  • Focus on improving code quality by avoiding key mistakes.
  • Everyone writes flawed code while learning; improvement is always possible.

1. Not Using Programming Standards

  • Importance: Ensures consistency and readability.
  • Applications: Whitespacing, file structure.
  • Collaboration: Sets shared expectations in team environments.
  • Guidance: Choose from many existing standards if not provided.

2. Ignoring Programming Design Principles

Introduction to SOLID Principles:

Single Responsibility Principle (SRP)

  • Classes should have one job only.
  • Break down large classes performing unrelated tasks.
  • Benefits: Easy identification, clean testing, reusability.

Open-Closed Principle (OCP)

  • Modules should allow extension without modification.
  • Add new functionality by extending, not modifying the existing module.
  • Benefits: Reduces breaking existing code.

Liskov Substitution Principle (LSP)

  • Extend modules only if new type remains compatible with the old type.
  • Example: Extend a hexagon to a six-pointed star but not to a five-pointed star.

Interface Segregation Principle (ISP)

  • Modules shouldn’t need to know about unused functionalities.
  • Split modules into smaller abstractions (interfaces).
  • Benefits: Easier testing and mock setups.

Dependency Inversion Principle (DIP)

  • Communicate abstractly, not directly.
  • Use interfaces to decouple modules.
  • Benefits: Easier swapping of implementation parts.

Benefits of SOLID

  • Decouples code, making it more maintainable, scalable, reusable, and testable.

3. Underusing Programming Design Patterns

  • Importance: Provides real solutions to common code problems.
  • Categories:
    • Creational Patterns: Control instance creation (e.g., factory method pattern).
    • Structural Patterns: Organize and manipulate objects (e.g., adapter pattern).
    • Behavioral Patterns: Define communication (e.g., observer pattern).
  • Benefits: Universal vocabulary for programming, better naming practices.

4. Poor Naming Conventions

  • Problem: Hard to interpret ambiguous code snippets.
  • Solution Steps:
    • Avoid unnecessary encoding.
    • Expand abbreviations and acronyms.
    • Use clear, descriptive names.
    • Replace magic values with named constants.
  • Goal: Balance clear understanding and avoid information overload.
  • Result: Readable and maintainable code.

5. Not Testing Code

  • Types of Testing:
    • End-to-End Testing: Simulates user interactions.
    • Unit Tests: Verify isolated modules.
    • Integration Tests: Examine interaction between modules.
  • Benefit: Ensures code does what is expected.
  • Tip: Apply SOLID principles for easier testability.

6. Poor Time Management

  • Problem: Underestimating task duration.
  • Solution: Double or triple initial time estimates.
  • Example: Longer time taken to make the video than predicted.
  • Benefit: Better to overestimate and deliver early.

7. Rushing Through Projects

  • Advice: Don’t rush; think things through from day one.
  • Application: Avoiding long-term issues by planning properly.
  • Goal: Create a foundation for long-term projects that become easier over time.

Conclusion

  • Encouragement: Continuously improve to become a better programmer.
  • Quote: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler.

Thank You

  • Emphasizes the effort and time put into creating informative content.