Coder Baba
Відкрити в Telegram
Everything about programming for beginners. 1 and only official telegram channel of CODERBABA India. Content: .NET Developer, Programming (ASP. NET, VB. NET, C#, SQL Server), & Projects follow me https://linktr.ee/coderbaba *Programming *Coding *Note
Показати більше2 319
Підписники
-124 години
-167 днів
-2830 день
Архів дописів
2 319
✔️ 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀
✅ 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀 is a practice of clean coding where you replace complex conditional logic with well-named methods to make your code more readable and maintainable.
🔥 𝗧𝗵𝗲 𝗮𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝗲𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗻𝗴 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀:
◾️ 𝗜𝗺𝗽𝗿𝗼𝘃𝗲𝘀 𝗿𝗲𝗮𝗱𝗮𝗯𝗶𝗹𝗶𝘁𝘆: Code reads more like a high-level language, which improves understanding.
◾️ 𝗥𝗲𝗱𝘂𝗰𝗲𝘀 𝗱𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻: If a complex condition is used in more than one place, it's better to keep the logic in one place.
◾️ 𝗦𝗶𝗺𝗽𝗹𝗶𝗳𝗶𝗲𝘀 𝗰𝗼𝗱𝗲: It's easier to understand a method call with a well-named method than to understand a complex conditional.
◾️ 𝗘𝗮𝘀𝗶𝗲𝗿 𝘁𝗼 𝘁𝗲𝘀𝘁: Encapsulated conditionals can be separately tested, ensuring that all edge-cases are covered.
💻 𝗛𝗼𝘄 𝘁𝗼 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗲𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀:
To implement encapsulate conditionals, simply extract the conditional logic into a private method. The method should have a clear and concise name that describes what it does.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
2 319
✔️ 𝗔𝘃𝗼𝗶𝗱 𝗲𝗹𝘀𝗲 𝗮𝗳𝘁𝗲𝗿 𝗿𝗲𝘁𝘂𝗿𝗻
❌ When a return statement is encountered in a function, it immediately exits the function and returns control to the calling code. Any code following the return statement within the same block will not be executed. In many cases, including an 𝗲𝗹𝘀𝗲 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 after a return is redundant and can be safely removed.
✅ To implement this principle, simply 𝗿𝗲𝗺𝗼𝘃𝗲 𝘁𝗵𝗲 𝗲𝗹𝘀𝗲 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 after any return statement. If you need to execute code if the if condition is not met, you can move that code to the end of the function, outside of the if statement.
💡 𝗧𝗵𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 is a good choice when you have a simple conditional logic. While it can be used to simplify code in some cases, it should be used judiciously to maintain code readability.
🔥 Following the "𝗔𝘃𝗼𝗶𝗱 𝗲𝗹𝘀𝗲 𝗮𝗳𝘁𝗲𝗿 𝗿𝗲𝘁𝘂𝗿𝗻" principle helps in writing cleaner, more maintainable code by simplifying control flow and improving code readability.
❔ 𝗪𝗵𝗶𝗰𝗵 𝗼𝗻𝗲 𝗱𝗼 𝘆𝗼𝘂 𝗽𝗿𝗲𝗳𝗲𝗿?
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
2 319
✔️ 𝗨𝘀𝗲 𝘀𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗧𝗼𝗨𝗽𝗽𝗲𝗿()/𝗧𝗼𝗟𝗼𝘄𝗲𝗿() 𝘄𝗵𝗲𝗻 𝗰𝗼𝗺𝗽𝗮𝗿𝗶𝗻𝗴 𝘀𝘁𝗿𝗶𝗻𝗴𝘀
🐌 Using 𝗧𝗼𝗨𝗽𝗽𝗲𝗿() and 𝗧𝗼𝗟𝗼𝘄𝗲𝗿() for case conversion in C# can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.
🚀 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 is faster than ToUpper() or ToLower() due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.
🔥 To perform string comparison , it's better to use the built-in comparison methods like 𝗦𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀 with appropriate StringComparison options, which handle case-insensitivity and cultural considerations correctly while maintaining better performance and accuracy.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 😊
hashtag#csharp hashtag#dotnet hashtag#programming hashtag#cleancode hashtag
2 319
✔️ 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗱𝗼 𝗼𝗻𝗲 𝘁𝗵𝗶𝗻𝗴
✅ This principle suggests that a function should have only one reason to change, meaning that it should perform a single, well-defined task and not multiple unrelated tasks. This approach increases readability, maintainability, and testability of your code.
❌ A function that does too many things or has too many responsibilities can become difficult to understand, test, and maintain.
𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻:
◾️ Identify the single responsibility of the function. What is the one thing that the function should do?
◾️ Extract all unrelated code from the function. This code can be moved to other functions or classes.
◾️ Give the function a descriptive name that reflects its single responsibility.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗣𝗿𝗲𝗳𝗲𝗿 𝘁𝗵𝗲 𝗶𝘀 𝗞𝗲𝘆𝘄𝗼𝗿𝗱 𝗢𝘃𝗲𝗿 𝘁𝗵𝗲 == 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿 𝘁𝗼 𝗰𝗵𝗲𝗰𝗸 𝗳𝗼𝗿 𝗻𝘂𝗹𝗹
✅ The primary reason to prefer 𝗶𝘀 for null checks is its avoidance of potential operator overloading issues. Operator overloading allows programmers to define custom behavior for operators like == for their classes. While useful, this feature can introduce unexpected behavior when checking for null.
💡 Consider a scenario where a class Student overloads the == operator to compare the values of its properties rather than object references. If you use == to check for null, you might not get the expected result, as the overloaded == would compare property values instead of checking for null.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗡𝘂𝗹𝗹 𝗰𝗵𝗲𝗰𝗸𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗻𝗼𝘁 𝗯𝗲 𝘂𝘀𝗲𝗱 𝘄𝗶𝘁𝗵 𝗶𝘀
✅ In C#, the 𝗶𝘀 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 is used to determine whether an object is compatible with a specific type. It evaluates to true if the object can be cast to the specified type without causing an exception, and false otherwise. It also returns false for null objects.
✅ The 𝗶𝘀 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 can be useful in the following scenarios:
◾️ To check the run-time type of an expression.
◾️ To check for null.
◾️ To check for non-null using a negation pattern.
◾️ Match elements of a list or array using list patterns.
💡 𝗡𝘂𝗹𝗹 𝗰𝗵𝗲𝗰𝗸𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗻𝗼𝘁 𝗯𝗲 𝘂𝘀𝗲𝗱 𝘄𝗶𝘁𝗵 𝗶𝘀
There’s no need to null test in conjunction with an is test. null is not an instance of anything, so a null check is redundant.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝘀𝗵𝗼𝘂𝗹𝗱 𝗻𝗼𝘁 𝗯𝗲 𝗰𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗲𝗱 𝘂𝘀𝗶𝗻𝗴 '+' 𝗶𝗻 𝗮 𝗹𝗼𝗼𝗽
🐌 Strings are immutable, which means that once a string object is created, it cannot be modified. When you concatenate strings using the '+' 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 in a loop, a new string object is created at each iteration, and the previous objects are discarded. This can lead to performance issues, especially when dealing with large strings or a large number of iterations.
🚀 A more efficient approach to string concatenation in C# is to use the 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 𝗰𝗹𝗮𝘀𝘀, which is designed for efficiently building strings in a loop. StringBuilder allows you to append strings without creating new objects each time, which leads to better performance.
💡 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 is more useful when dealing with large strings or a large number of iterations and when we have an unknown amount of strings.
🔥 By using 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿, you can significantly reduce memory allocations and improve the performance of your code when you need to concatenate strings in a loop. It is a best practice to use StringBuilder when working with dynamic string building operations.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗗𝗼𝗻'𝘁 𝘂𝘀𝗲 𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 𝗰𝗮𝗹𝗹𝘀
⚠️ The 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 operator is used to sort a sequence of elements based on a specified key. When multiple OrderBy calls are chained together, each subsequent call completely reorders the list, discarding the results of the previous call. This means that only the last OrderBy call will have any effect on the final ordering of the sequence.
🐌 Using multiple 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 calls can also lead to performance problems. This is because each OrderBy call performs a full sort of the list, which can be expensive. If you are chaining multiple OrderBy calls together, you are essentially performing multiple full sorts, which can significantly slow down your code.
✅ The 𝗧𝗵𝗲𝗻𝗕𝘆 method is used after the initial OrderBy to apply additional sorting conditions. This ensures that the data is sorted first by the first field (Name in this example), and then by the second field (Price in this example).
🚀 The 𝗧𝗵𝗲𝗻𝗕𝘆 method in C# can be used to chain multiple sorting criteria together, without the performance overhead of calling OrderBy multiple times.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴
✅ 𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 is a feature introduced in 𝗖# 𝟵 that enhances the pattern matching capabilities of the language. Pattern matching is a way to compare values against patterns.
✅ It is a 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 programming technique, which means that it focuses on the evaluation of expressions rather than the control flow of your code.
🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝗿𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗽𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗺𝗮𝘁𝗰𝗵𝗶𝗻𝗴:
◾️ 𝗖𝗼𝗻𝗰𝗶𝘀𝗲𝗻𝗲𝘀𝘀: Relational patterns matching can be used to create more concise and readable code.
◾️ 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲𝗻𝗲𝘀𝘀: Relational patterns matching can be used to express more complex conditions.
💡 𝗥𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 is not always the best solution. Sometimes, it is simpler and more efficient to use the traditional way of checking if an expression matches a certain condition.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗣𝗿𝗲𝗳𝗲𝗿 𝗔𝗻𝘆() 𝗼𝘃𝗲𝗿 𝗖𝗼𝘂𝗻𝘁()
✅ The 𝗖𝗼𝘂𝗻𝘁() method is used to get the total number of elements in a collection that satisfy a given condition. It returns an integer value representing the count of matching elements.
✅ The 𝗔𝗻𝘆() method is used to quickly determine if a collection contains any elements that satisfy a given condition. It returns a boolean value (true if any element matches the condition, otherwise false).
🚀 Using 𝗔𝗻𝘆() over 𝗖𝗼𝘂𝗻𝘁() can be more efficient when you are only interested in determining whether any element matching a condition exists in the collection. This is because Any() stops iterating through the collection as soon as it finds the first matching element, while Count() iterates through the entire collection to count all matching elements. In cases where the collection is large, Any() can potentially provide better performance.
🔥 Use 𝗔𝗻𝘆() over 𝗖𝗼𝘂𝗻𝘁() when you only care if there are any elements in a collection, not the exact number of elements. You should also use Any() when the collection is large or lazy-evaluated. And you should use Any() when you want to improve the readability of your code.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗖# 𝟭𝟮 𝗣𝗿𝗶𝗺𝗮𝗿𝘆 𝗖𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀
✅ 𝗖# 𝟭𝟮 introduces 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀, a concise syntax to declare constructors whose parameters are available anywhere in the body of the type.
𝗧𝗵𝗲 𝗺𝗼𝘀𝘁 𝗰𝗼𝗺𝗺𝗼𝗻 𝘂𝘀𝗲𝘀 𝗳𝗼𝗿 𝗮 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿 𝗽𝗮𝗿𝗮𝗺𝗲𝘁𝗲𝗿 𝗮𝗿𝗲:
◾️ As an argument to a base() constructor invocation.
◾️ To initialize a member field or property.
◾️ Referencing the constructor parameter in an instance member.
🔥 You can use the 𝗽𝗿𝗶𝗺𝗮𝗿𝘆 𝗰𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀 as best suits your design. For classes and structs, primary constructor parameters are parameters to a constructor that must be invoked. You can use them to initialize properties. You can initialize fields. Those properties or fields can be immutable, or mutable. You can use them in methods.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
2 319
✔️ 𝗥𝗲𝗽𝗹𝗮𝗰𝗲 𝗶𝗳 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 𝘄𝗶𝘁𝗵 𝗡𝘂𝗹𝗹 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿
✅ The 𝗻𝘂𝗹𝗹 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿, also known as the null propagation operator or the safe navigation operator, is a feature introduced in C# 6.0 that allows you to write cleaner and more concise code when dealing with potentially null reference types.
💡 The 𝗻𝘂𝗹𝗹 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 is represented by a question mark followed by a period (?.) and is used to access members or invoke methods on an object that may be null. If the object is null, the expression returns null instead of throwing a null reference exception.
🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝘂𝘀𝗶𝗻𝗴 𝘁𝗵𝗲 𝗻𝘂𝗹𝗹 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿:
◾️The null conditional operator can make your code more concise and readable.
◾️The null conditional operator can help to avoid null-reference exceptions.
◾️The null conditional operator can be used to chain together multiple member or element accesses, even if some of the members or elements may be null.
𝗧𝗵𝗮𝗻𝗸 𝘆𝗼𝘂 𝗳𝗼𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 📖
Вже доступно! Дослідження Telegram за 2025 — головні інсайти року 
