Skip to main content

Posts

Think like pro in React

this mindset shift is what makes the difference between just “coding React” and being a React developer who can own features end-to-end . Here’s a structured way to think whenever you’re given new functionality: 🧠 Step 1: Understand the Requirement What exactly is the feature supposed to do? (inputs, outputs, user flow) Who is the user, and where in the app will this be used? Clarify edge cases (e.g., what if API fails, what if no data, what if user is not logged in?). 👉 Example: If asked to “add search to product list,” clarify: Is it local search (filtering existing data) or API search (fetching from server)? Should it be instant (as you type) or only on Enter/click? What should happen if no results are found? 🧠 Step 2: Break Down into Components Which UI components are needed? (buttons, input fields, list, card, modal, etc.) Can I reuse existing components or do I need new ones? Where will state live? (local state with useState , lifted up to parent, or glo...
Recent posts

⚡ Part 1: Introduction to Generics in C#

🌍 Why Do We Need Generics? Imagine you want to create a stack (like a pile of books 📚): You can push items on top You can pop items off the top If we write a stack for integers : public class IntStack { private int[] items = new int[10]; private int index = 0; public void Push(int item) => items[index++] = item; public int Pop() => items[--index]; } 👉 Problem: This only works for int . What if we want a string stack ? Or a Customer stack ? We’d have to duplicate code for every type. 😢 ✅ Solution: Generics Generics let us create type-safe reusable code without duplication. We can say: “I don’t care what type it is yet — I’ll decide later.” 1) Generic Classes Here’s a generic stack : // Generic class "Stack<T>" // The <T> is a placeholder for any type public class Stack<T> { private T[] items = new T[10]; // Array of type T private int index = 0; // Push adds an item of type T public void P...

⚡ Part 4: Complex Scenarios with Generics in C#

In Part 1–3 , we learned: Basics of generics (methods, classes, constraints) Advanced patterns (delegates, factories, strategies) Real-world use cases (repository, service, caching, API responses) Now in Part 4 , we’ll handle trickier topics : Nested Generics Covariance & Contravariance Generic Methods in LINQ-style APIs Performance Considerations with Generics Common Pitfalls and Best Practices 1) Nested Generics Sometimes we need a generic type inside another generic type . This looks confusing at first, but it’s actually quite powerful. Example: Dictionary of Lists // A dictionary where the key is string, and the value is a list of integers Dictionary<string, List<int>> studentMarks = new(); studentMarks["Alice"] = new List<int> { 90, 85, 88 }; studentMarks["Bob"] = new List<int> { 70, 75, 80 }; foreach (var kv in studentMarks) { Console.WriteLine($"{kv.Key}: {string.Join(", ", k...

⚡ Part 3 : Real-World Use Cases of Generics in C#

in Part 3, we’ll apply generics to real-world enterprise scenarios: Generic Repository Pattern Generic Service Layer Generic Caching Utility Generic API Response Wrapper Let’s dive in 🚀 1) Generic Repository Pattern The repository pattern is used to separate data access logic (like Add , Get , Remove ) from the rest of the code. Without a generic repository, you’d write separate repositories for each entity (CustomerRepository, OrderRepository, etc.), which leads to a lot of duplicate code . Generics solve this by creating one reusable repository for all entities. Code Walkthrough // All entities will implement IEntity so that we can guarantee they have an Id property public interface IEntity { int Id { get; set; } } 👉 Why? Because in most systems, every object (Customer, Order, Product) has an Id that uniquely identifies it. By enforcing IEntity , we make sure our repository can always look up objects by Id. // Repository interface with CRUD-like operations p...

⚡ Part 2: Advanced Generics in C#

In Part 1 , we explored the basics of generics : Generic methods Generic classes Constraints Interfaces and real-world examples Now in Part 2 , we’ll dive deeper into advanced scenarios where generics really shine. We’ll cover: Generic Delegates & Events Generic Abstract Classes Generic Comparers & Equality Checkers Factory Pattern with Generics Strategy Pattern with Generics Let’s go step by step 🚀 1) Generic Delegates & Events A delegate is like a pointer to a method — it stores a reference to a function so we can call it later. When we make delegates generic , we can reuse them for multiple data types. Example: Generic Delegate // A delegate that takes two parameters of type T and returns T // This can represent operations like Add, Multiply, etc. public delegate T Operation<T>(T a, T b); class Calculator { // A generic Add method that works for numbers // Constraint: "where T : struct" ensures only...

🧱 Part 17 — Advanced TypeScript Patterns (Branded Types, Results, Exhaustive Checks) 🧠

When your app grows, types become your safety net . Here are advanced—but approachable—TypeScript patterns that prevent entire classes of bugs. Every snippet is commented and copy‑paste ready. 1) Branded (Opaque) Types — prevent mixing IDs/units Avoid passing a ProjectId where a TaskId is expected. // src/types/brand.ts // Create an opaque subtype of T (compile-time only) export type Brand<T, B extends string> = T & { readonly __brand: B }; export type TaskId = Brand<string, 'TaskId'>; export type ProjectId = Brand<string, 'ProjectId'>; export const TaskId = (s: string): TaskId => s as TaskId; export const ProjectId = (s: string): ProjectId => s as ProjectId; // Usage function loadTask(id: TaskId) {/* impl */} // loadTask('123'); // ❌ plain string not allowed loadTask(TaskId('123')); // ✅ branded Brands exist only at compile time; runtime values are plain strings. 2) Discriminated Unions + Exhaustiv...

🧱 Part 16 — Accessibility First (Semantics, Keyboard, ARIA, Focus, Color)

Accessibility (a11y) is not optional : it helps keyboard users, screen‑reader users, and frankly everyone (think: low contrast screens, bright sun, fatigue). This part is beginner‑friendly and packed with commented patterns you can paste in today. 🎯 Goals Semantic HTML & landmarks (nav, main, header, footer) Keyboard support (Tab order, focus outlines, ESC to close) Visible focus with :focus-visible Skip link , visually hidden text, live regions Readable colors (contrast), reduced motion ARIA for dialog , tabs , and form errors 1) Baseline: semantic layout + skip link Make the structure obvious to assistive tech and provide a quick jump to content. // src/app/Layout.tsx import { SkipLink } from '@/components/a11y/SkipLink'; export default function Layout({ children }: { children: React.ReactNode }) { return ( <div> <SkipLink target="#main" /> <header role="banner" className="p-4 border-b...