🚀 𝗠𝗼𝗱𝗲𝗿𝗻 𝗖++ 𝗙𝗲𝗮𝘁𝘂𝗿𝗲: 𝗦𝘁𝗱::𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹 (𝗖++𝟭𝟳) How do you represent 𝗮 𝘃𝗮𝗹𝘂𝗲 𝘁𝗵𝗮𝘁 𝗺𝗶𝗴𝗵𝘁 𝗻𝗼𝘁 𝗲𝘅𝗶𝘀𝘁 in C++? For years developers relied on 𝗼𝗹𝗱 𝗽𝗮𝘁𝘁𝗲𝗿𝗻𝘀 like: ❌ nullptr ❌ 𝗺𝗮𝗴𝗶𝗰 𝘃𝗮𝗹𝘂𝗲𝘀 such as -1 ❌ 𝗰𝗼𝗺𝗽𝗹𝗲𝘅 𝗲𝗿𝗿𝗼𝗿 𝗳𝗹𝗮𝗴𝘀 Modern C++ introduced a 𝗰𝗹𝗲𝗮𝗻𝗲𝗿 𝗮𝗻𝗱 𝘀𝗮𝗳𝗲𝗿 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻. 💡 std::optional 𝗿𝗲𝗽𝗿𝗲𝘀𝗲𝗻𝘁𝘀 𝗮𝗻 𝗼𝗯𝗷𝗲𝗰𝘁 𝘁𝗵𝗮𝘁 𝗺𝗮𝘆 𝗼𝗿 𝗺𝗮𝘆 𝗻𝗼𝘁 𝗰𝗼𝗻𝘁𝗮𝗶𝗻 𝗮 𝘃𝗮𝗹𝘂𝗲. ❌ 𝗢𝗹𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 int findUserId(const std::string& name) { if(name == "Alice") return 42; return -1; // magic value } Here -1 acts as a 𝗵𝗶𝗱𝗱𝗲𝗻 𝗺𝗲𝗮𝗻𝗶𝗻𝗴 value. This leads to: ❌ 𝗺𝗮𝗴𝗶𝗰 𝗻𝘂𝗺𝗯𝗲𝗿𝘀 ❌ 𝗵𝗮𝗿𝗱-𝘁𝗼-𝗿𝗲𝗮𝗱 𝗔𝗣𝗜𝘀 ❌ 𝗽𝗼𝘁𝗲𝗻𝘁𝗶𝗮𝗹 𝗯𝘂𝗴𝘀 ✅ 𝗠𝗼𝗱𝗲𝗿𝗻 𝗖++ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 #include <optional> std::optional<int> findUserId(const std::string& name) { if(name == "Alice") return 42; return std::nullopt; } Now the API 𝗲𝘅𝗽𝗹𝗶𝗰𝗶𝘁𝗹𝘆 𝘀𝗮𝘆𝘀 that the result 𝗺𝗮𝘆 𝗯𝗲 𝗺𝗶𝘀𝘀𝗶𝗻𝗴. 📦 𝗨𝘀𝗮𝗴𝗲 auto id = findUserId("Bob"); if(id) { std::cout << "User ID: " << *id; } else { std::cout << "User not found"; } This makes the code 𝗰𝗹𝗲𝗮𝗿𝗲𝗿, 𝘀𝗮𝗳𝗲𝗿, 𝗮𝗻𝗱 𝗺𝗼𝗿𝗲 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲. ⚡ 𝗪𝗵𝘆 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 𝗹𝗼𝘃𝗲 std::optional ✔ 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗲𝘀 𝗶𝗻𝘁𝗲𝗻𝘁 𝗰𝗹𝗲𝗮𝗿𝗹𝘆 ✔ 𝗘𝗹𝗶𝗺𝗶𝗻𝗮𝘁𝗲𝘀 𝗺𝗮𝗴𝗶𝗰 𝘃𝗮𝗹𝘂𝗲𝘀 ✔ 𝗜𝗺𝗽𝗿𝗼𝘃𝗲𝘀 𝗔𝗣𝗜 𝗱𝗲𝘀𝗶𝗴𝗻 ✔ 𝗠𝗮𝗸𝗲𝘀 𝗰𝗼𝗱𝗲 𝗺𝗼𝗿𝗲 𝗿𝗲𝗮𝗱𝗮𝗯𝗹𝗲 🧠 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 std::optional is not just a container. It is a 𝗱𝗲𝘀𝗶𝗴𝗻 𝘀𝗶𝗴𝗻𝗮𝗹 that clearly communicates: 👉 “𝗔 𝘃𝗮𝗹𝘂𝗲 𝗺𝗮𝘆 𝗼𝗿 𝗺𝗮𝘆 𝗻𝗼𝘁 𝗲𝘅𝗶𝘀𝘁.” And that clarity alone can 𝗽𝗿𝗲𝘃𝗲𝗻𝘁 𝘀𝘂𝗯𝘁𝗹𝗲 𝗯𝘂𝗴𝘀. 🏆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Modern C++ is moving toward 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲 𝗔𝗣𝗜𝘀 and 𝗰𝗹𝗲𝗮𝗿 𝗶𝗻𝘁𝗲𝗻𝘁. std::optional turns 𝗶𝗺𝗽𝗹𝗶𝗰𝗶𝘁 𝗮𝗯𝘀𝗲𝗻𝗰𝗲 into 𝗲𝘅𝗽𝗹𝗶𝗰𝗶𝘁 𝗱𝗲𝘀𝗶𝗴𝗻. Small feature. 𝗛𝘂𝗴𝗲 𝗶𝗺𝗽𝗮𝗰𝘁 𝗼𝗻 𝗰𝗼𝗱𝗲 𝗰𝗹𝗮𝗿𝗶𝘁𝘆. #CPP #ModernCPP #SoftwareEngineering #CPP17 #Programming #CleanCode Follow for more Modern C++ insights. — 𝗔𝗕𝗛𝗜𝗦𝗛𝗘𝗞 𝗦𝗜𝗡𝗛𝗔
C++ std::optional: Representing Optional Values
More Relevant Posts
-
🚀 𝗠𝗼𝗱𝗲𝗿𝗻 𝗖++ 𝗙𝗲𝗮𝘁𝘂𝗿𝗲: 𝗜𝗳 𝘄𝗶𝘁𝗵 𝗜𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗲𝗿 (𝗖++𝟭𝟳) In older C++ code, developers often had to 𝗱𝗲𝗰𝗹𝗮𝗿𝗲 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝗼𝘂𝘁𝘀𝗶𝗱𝗲 the if statement, even when the variable was only needed for a small scope. This created 𝗲𝘅𝘁𝗿𝗮 𝘀𝗰𝗼𝗽𝗲 𝗽𝗼𝗹𝗹𝘂𝘁𝗶𝗼𝗻 and sometimes made the code harder to maintain. Modern C++ introduced a cleaner pattern: 𝗶𝗳 𝘄𝗶𝘁𝗵 𝗶𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗲𝗿. ❌ 𝗢𝗹𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 auto it = users.find("Alice"); if (it != users.end()) { std::cout << it->second; } Here the iterator 𝗶𝘁 𝗹𝗶𝘃𝗲𝘀 𝗯𝗲𝘆𝗼𝗻𝗱 the if block, even though it is not needed elsewhere. ✅ 𝗠𝗼𝗱𝗲𝗿𝗻 𝗖++ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 if (auto it = users.find("Alice"); it != users.end()) { std::cout << it->second; } Now the variable 𝗶𝘁 𝗶𝘀 𝘀𝗰𝗼𝗽𝗲𝗱 𝗶𝗻𝘀𝗶𝗱𝗲 the if statement only. Cleaner. Safer. More readable. ⚡ 𝗪𝗵𝘆 𝗧𝗵𝗶𝘀 𝗙𝗲𝗮𝘁𝘂𝗿𝗲 𝗜𝘀 𝗣𝗼𝘄𝗲𝗿𝗳𝘂𝗹 ✔ Encourages 𝗯𝗲𝘁𝘁𝗲𝗿 𝘀𝗰𝗼𝗽𝗲 𝗺𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁 ✔ Reduces 𝘂𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲 𝗹𝗶𝗳𝗲𝘁𝗶𝗺𝗲 ✔ Makes code 𝗺𝗼𝗿𝗲 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲 ✔ Improves 𝗿𝗲𝗮𝗱𝗮𝗯𝗶𝗹𝗶𝘁𝘆 🧠 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 Modern C++ is not just about performance. It is also about 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲 𝗮𝗻𝗱 𝘀𝗮𝗳𝗲𝗿 𝗰𝗼𝗱𝗲. Features like 𝗶𝗳 𝘄𝗶𝘁𝗵 𝗶𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗲𝗿 encourage developers to 𝗸𝗲𝗲𝗽 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝗶𝗻 𝘁𝗵𝗲𝗶𝗿 𝗻𝗮𝘁𝘂𝗿𝗮𝗹 𝘀𝗰𝗼𝗽𝗲. 🏆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Small syntax improvement. But it leads to 𝗰𝗹𝗲𝗮𝗻𝗲𝗿 𝗮𝗻𝗱 𝗺𝗼𝗿𝗲 𝗺𝗮𝗶𝗻𝘁𝗮𝗶𝗻𝗮𝗯𝗹𝗲 𝗰𝗼𝗱𝗲. Sometimes the 𝗯𝗲𝘀𝘁 𝗹𝗮𝗻𝗴𝘂𝗮𝗴𝗲 𝗳𝗲𝗮𝘁𝘂𝗿𝗲𝘀 are the smallest ones. #CPP #ModernCPP #CPP17 #SoftwareEngineering #Programming #CleanCode — 𝗔𝗕𝗛𝗜𝗦𝗛𝗘𝗞 𝗦𝗜𝗡𝗛𝗔
To view or add a comment, sign in
-
-
C++ has a special kind of technical debt: header include hell. It starts innocently enough. A few includes here, a convenience header there, one “temporary” dependency that nobody cleans up later. Then the project grows. Suddenly: - include order starts to matter - circular dependencies creep in - weird compiler errors appear far away from the real cause - developers start over-including files “just in case” - build times slow to a crawl - even IntelliSense begins to lose its mind And in template-heavy codebases, it gets even worse. Because templates live in headers, every bad dependency decision gets amplified across the entire project. The real fix is not another workaround. It’s discipline. - Treat includes as a tree, not a graph. - Keep headers small and focused. - Split types, functions, classes, and data structures into logical units. - Group code by purpose, not by convenience. And when a header starts becoming a junk drawer, refactor it without hesitation. Yes, forward declarations can help. But too often they’re used as painkillers instead of a cure. If your code constantly needs them just to stay compilable, there’s a good chance your dependency structure is already sick. Bad include hygiene is not a minor inconvenience. It is architecture debt with compound interest. A clean include structure doesn’t just improve compile times. It improves readability, maintainability, tooling, and the team’s ability to change the code without fear. In C++, headers are not just files. They are a map of your design quality. If your include graph looks like spaghetti, your architecture probably does too. #cpp #cplusplus #softwarearchitecture #technicaldebt #buildsystems #programming #gamedev #softwareengineering #cleanCode
To view or add a comment, sign in
-
-
🚀 𝗠𝗼𝗱𝗲𝗿𝗻 𝗖++ 𝗙𝗲𝗮𝘁𝘂𝗿𝗲: 𝗦𝗧𝗗::𝗘𝗫𝗣𝗘𝗖𝗧𝗘𝗗 (𝗖++𝟮𝟯) Error handling has always been an important part of writing 𝗿𝗼𝗯𝘂𝘀𝘁 𝗖++ 𝗰𝗼𝗱𝗲. Traditionally, developers relied on 𝗲𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻𝘀 or 𝗲𝗿𝗿𝗼𝗿 𝗰𝗼𝗱𝗲𝘀. But both approaches come with trade-offs: ⚠️ 𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻𝘀 can hide control flow ⚠️ 𝗘𝗿𝗿𝗼𝗿 𝗰𝗼𝗱𝗲𝘀 often make APIs 𝗵𝗮𝗿𝗱𝗲𝗿 𝘁𝗼 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 C++23 introduces a modern solution: 𝗦𝗧𝗗::𝗘𝗫𝗣𝗘𝗖𝗧𝗘𝗗. It represents 𝗲𝗶𝘁𝗵𝗲𝗿 𝗮 𝘃𝗮𝗹𝘂𝗲 𝗼𝗿 𝗮𝗻 𝗲𝗿𝗿𝗼𝗿 in a 𝗰𝗹𝗲𝗮𝗿 𝗮𝗻𝗱 𝘁𝘆𝗽𝗲-𝘀𝗮𝗳𝗲 𝘄𝗮𝘆. ❌ 𝗢𝗹𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 (Error Codes) Developers often returned special values to indicate failure. int divide(int a, int b) { if (b == 0) return -1; // error code return a / b; } This works — but it creates problems. ❌ 𝗘𝗿𝗿𝗼𝗿 𝗰𝗼𝗱𝗲𝘀 𝗺𝗶𝘅 𝗳𝗮𝗶𝗹𝘂𝗿𝗲 𝗮𝗻𝗱 𝘃𝗮𝗹𝗶𝗱 𝗿𝗲𝘀𝘂𝗹𝘁𝘀 ❌ The caller must 𝗴𝘂𝗲𝘀𝘀 𝘄𝗵𝗮𝘁 -𝟭 𝗺𝗲𝗮𝗻𝘀 ✨ 𝗘𝗻𝘁𝗲𝗿 𝗦𝗧𝗗::𝗘𝗫𝗣𝗘𝗖𝗧𝗘𝗗 C++23 introduces a type that can explicitly represent 𝗲𝗶𝘁𝗵𝗲𝗿 𝗮 𝗿𝗲𝘀𝘂𝗹𝘁 𝗼𝗿 𝗮𝗻 𝗲𝗿𝗿𝗼𝗿. #include <expected> std::expected<int, std::string> divide(int a, int b) { if (b == 0) return std::unexpected("division by zero"); return a / b; } Now the function clearly communicates: ✔ 𝗘𝗶𝘁𝗵𝗲𝗿 𝗮 𝘃𝗮𝗹𝗶𝗱 𝗿𝗲𝘀𝘂𝗹𝘁 ✔ 𝗢𝗿 𝗮𝗻 𝗲𝘅𝗽𝗹𝗶𝗰𝗶𝘁 𝗲𝗿𝗿𝗼𝗿 ⚡ 𝗪𝗵𝘆 𝗦𝗧𝗗::𝗘𝗫𝗣𝗘𝗖𝗧𝗘𝗗 𝗶𝘀 𝗽𝗼𝘄𝗲𝗿𝗳𝘂𝗹 ✔ Enables 𝗲𝘅𝗽𝗹𝗶𝗰𝗶𝘁 𝗲𝗿𝗿𝗼𝗿 𝗵𝗮𝗻𝗱𝗹𝗶𝗻𝗴 ✔ Improves 𝗮𝗽𝗶 𝗰𝗹𝗮𝗿𝗶𝘁𝘆 ✔ Avoids 𝗺𝗮𝗴𝗶𝗰 𝗲𝗿𝗿𝗼𝗿 𝘃𝗮𝗹𝘂𝗲𝘀 ✔ Makes failures 𝗽𝗮𝗿𝘁 𝗼𝗳 𝘁𝗵𝗲 𝘁𝘆𝗽𝗲 𝘀𝘆𝘀𝘁𝗲𝗺 🧠 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 Modern C++ is moving toward 𝗲𝘅𝗽𝗹𝗶𝗰𝗶𝘁 𝗮𝗻𝗱 𝘁𝘆𝗽𝗲-𝘀𝗮𝗳𝗲 𝗔𝗣𝗜 𝗱𝗲𝘀𝗶𝗴𝗻. Instead of hiding failures, modern C++ encourages developers to 𝗺𝗼𝗱𝗲𝗹 𝗲𝗿𝗿𝗼𝗿𝘀 𝗱𝗶𝗿𝗲𝗰𝘁𝗹𝘆 𝗶𝗻 𝘁𝗵𝗲 𝘁𝘆𝗽𝗲 𝘀𝘆𝘀𝘁𝗲𝗺. 🏆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Sometimes the best APIs are the ones that 𝗺𝗮𝗸𝗲 𝗶𝗻𝘁𝗲𝗻𝘁 𝗰𝗹𝗲𝗮𝗿. std::expected helps developers write 𝗺𝗼𝗿𝗲 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲, 𝘀𝗮𝗳𝗲𝗿, and 𝗺𝗮𝗶𝗻𝘁𝗮𝗶𝗻𝗮𝗯𝗹𝗲 𝗖++ 𝗰𝗼𝗱𝗲. #CPP #ModernCPP #CPP23 #SoftwareEngineering #Programming #CleanCode — 𝗔𝗕𝗛𝗜𝗦𝗛𝗘𝗞 𝗦𝗜𝗡𝗛𝗔
To view or add a comment, sign in
-
-
Many developers know the definition of Dependency Injection (DI), but few understand how it actually orchestrates a real-world application. If you're still using the "new" keyword to create services inside your constructors, you're building a "Hardcoded Problem" (Tight Coupling). Here is how we solve that in modern C# development: ⚙️ The 4-Step Mechanism 1. The Contract (Define Interface)Instead of depending on a specific class, we depend on an Interface (e.g., IMessageService). This is the "USB port" of your code—it defines what can be done without worrying about how. 2. The ImplementationWe create the actual logic (e.g., SmtpEmailService). Since it follows the interface, it’s easily swappable later. 3. Constructor InjectionYour classes (like OrderService) simply ask for the interface in their constructor. They don't create the tool; they just use it. This makes your code "Plug-and-Play." 4. The DI Container (Program.cs)This is the "Brain" of the operation. At application startup, we register our services. When the app runs, the container automatically "injects" the right implementation wherever it’s needed. Why does this matter? ✅ Loose Coupling: Change your database or email provider in one line of code. ✅ Unit Testing: Easily "Mock" dependencies to test logic in isolation. ✅ Maintainability: Your code becomes modular, clean, and professional. Are you still using new in your constructors, or have you fully embraced the DI Container? Let’s discuss in the comments! 👇 #CSharp #DotNet #SoftwareEngineering #DependencyInjection #CleanCode #ProgrammingTips #SystemDesign
To view or add a comment, sign in
-
-
Too many if conditions in your service? It’s not bad coding. It’s bad design. I’ve seen this in real systems (and I’ve written it too 👀): 👉 Different user types 👉 Different behaviors 👉 And suddenly… your service becomes a decision machine if (user instanceof PrimeUser) { ... } if (user instanceof InternationalUser) { ... } if (user instanceof GuestUser) { ... } At first, it feels normal. But slowly, your system starts asking: “Which type of user is this?” That’s where things break. Because now, not every user can be safely used everywhere. And that’s exactly what the Liskov Substitution Principle warns us about. 👉 A subclass should replace its parent 👉 Without breaking the system So what’s the fix? Stop checking types. Start trusting behavior. Instead of handling logic in the service, push it into the user itself. Now: ✔ No if conditions ✔ No type checks ✔ Clean, extensible design And most importantly — any User can replace another User safely. That’s real LSP. — If this made you rethink your design, follow for more real-world backend concepts 🚀 Next: Interface Segregation Principle (ISP) #systemdesign #backenddevelopment #java #cleanarchitecture #solidprinciples #softwareengineering
To view or add a comment, sign in
-
🔷 Abstract class vs Interface in modern C# — when does it actually matter? This is one of those questions that comes up in every code review, yet the answer is rarely nuanced enough. Here's my breakdown 👇 ───────────────────────────── ✅ Choose an Abstract Class when: → You have shared logic to reuse across subclasses (concrete methods, fields, constructors) → You need to maintain shared state — interfaces can't hold backing fields → You want to enforce constructor chaining with base(args) → You're modeling a true "is-a" relationship (Dog IS-A Animal) ───────────────────────────── ✅ Choose an Interface when: → You need multiple contracts — C# has no multiple inheritance, but you can implement many interfaces → You're defining a capability, not an identity (IDisposable, ISerializable, ICloneable) → You want maximum testability — interfaces are far easier to mock → You're building a public API for external implementors ───────────────────────────── 💬 Rule of thumb I always come back to: • Shared code/state → Abstract class • Capability across unrelated types → Interface • Constructor enforcement → Abstract class • Multiple "contracts" → Interface • Public API for external implementors → Interface ───────────────────────────── Which pattern do you reach for first? Drop it in the comments 👇 #csharp #dotnet #softwareengineering #cleancode #programming
To view or add a comment, sign in
-
A feature I worked on became difficult to maintain within weeks. Not because of complexity. Because of structure. Here’s what went wrong 👇 Problem: → Large components (400–600 lines) → Mixed UI + logic + API calls → Hard to reuse or test Root cause: ✖ No separation of concerns ✖ Everything tightly coupled ✖ Poor component boundaries What I changed: ✔ Split logic into custom hooks ✔ Separated UI from business logic ✔ Created smaller, focused components Result: → Better readability → Easier debugging → Faster feature updates Key insight: Bad structure doesn’t fail immediately. It fails as the system grows. That’s why architecture matters early. #ReactJS #FrontendArchitecture #SoftwareEngineering #CaseStudy #CleanCode #JavaScript #Engineering #ScalableSystems #Programming #Tech
To view or add a comment, sign in
-
🛑 UNIT TESTS AREN'T ABOUT FINDING BUGS A common misconception I see is that we write Unit Tests to "prove the code works." After several years in the industry, I’ve realized that’s only 20% of the value. The real reason we write them? The freedom to refactor. In a large .NET codebase, you will eventually need to optimize a complex service or upgrade a library. Without a solid test suite, you are "coding in the dark." You’re afraid to touch the logic because you don't know what might break three layers deep. A good test suite is like a safety net. It allows you to: ▪︎ Improve performance without fear. ▪︎ Simplify legacy logic with confidence. ▪︎ Onboard new developers faster because the tests document the intent of the code. If you find yourself skipping tests to "save time," you’re actually just taking out a high-interest loan that your future self will have to pay back. What’s your "Golden Rule" for testing? Do you aim for 100% coverage, or do you focus only on the most critical paths? #UnitTesting #DotNet #SoftwareQuality #CSharp #CodingStandards #SeniorEngineer
To view or add a comment, sign in
-
Delegates in C# every new business rule meant writing another method that make The codebase grew. Everything looked the same. root of the problem was hardcoding behavior instead of designing for flexibility. Instead of writing ten methods that differ by one condition, you write one method and pass the behavior in from outside. The logic stays clean. The caller decides the rule. Nothing needs to be rewritten for every new case. Code Example: static List<Product> Search( List<Product> products, Func<Product, bool> filter ) => products.Where(filter).ToList(); // Here we put a second param takes a Func Delegation Search(catalog, p => p.Category == "Electronics"); Search(catalog, p => p.Price < 50); Search(catalog, p => p.Stock > 20); Search(catalog, p => p.Category == "Clothing" && p.Price < 100); // we search for products with different signatures with the same block of code Delegation achieve 3 critical points : 1. Eliminating repetition — One method handles every rule. No more cloning methods for every new condition. 2. Protecting core logic — Pass new behavior in, never rewrite what already works. 3. Behavior as a value — A rule becomes something you store, pass, and swap at runtime — not something locked inside a method. #CSharp #DotNet #SoftwareEngineering #CleanCode #Programming #BackendDevelopment
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development