0% found this document useful (0 votes)
24 views8 pages

C# Clean Code

The document discusses clean coding practices in C#, emphasizing the use of the null conditional operator to simplify code and avoid null reference exceptions. It also recommends returning empty collections instead of null to enhance code safety and readability. Additionally, it highlights the benefits of pattern matching, using tuples, meaningful naming, and avoiding magic strings in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

C# Clean Code

The document discusses clean coding practices in C#, emphasizing the use of the null conditional operator to simplify code and avoid null reference exceptions. It also recommends returning empty collections instead of null to enhance code safety and readability. Additionally, it highlights the benefits of pattern matching, using tuples, meaningful naming, and avoiding magic strings in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C# Clean Code

Clean code:
Replace if statement with Null conditional operator

The 𝗻𝘂𝗹𝗹 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿, also known as the null propagation operator or the safe
navigation operator, is a feature introduced in 𝗖# 𝟲.𝟬 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.

🔥 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳 𝘂𝘀𝗶𝗻𝗴 𝘁𝗵𝗲 𝗻𝘂𝗹𝗹 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿:


◾It can make your code more concise and readable.
◾It can help to avoid null-reference exceptions.
◾It can be used to chain together multiple member or element accesses, even if some of the members
or elements may be null.

https://www.linkedin.com/posts/sina-riyahi_backend-fullstack-developer-activity-
7212108044556517378-Y8GJ?utm_source=share&utm_medium=member_desktop
Return an empty collection instead of null

✅ it is a recommended practice to 𝗿𝗲𝘁𝘂𝗿𝗻 𝗲𝗺𝗽𝘁𝘆 𝗰𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 instead of null when dealing with
methods or properties that return collections such as lists, arrays, dictionaries, etc. The primary reason
for this is to avoid potential null reference exceptions and simplify the client code that consumes these
collections.

❌ When a method 𝗿𝗲𝘁𝘂𝗿𝗻𝘀 𝗻𝘂𝗹𝗹, the caller needs to explicitly check for null before performing any
operations on the returned collection. This additional null check can clutter the client code and
potentially lead to null reference exceptions if it is overlooked.

🔥 On the other hand, returning an empty collection ensures that the client code can safely iterate over
the collection without worrying about null checks. An empty collection is an object, albeit an empty one,
and can be safely used for various operations without any risk of null reference exceptions.

💡𝗖𝗼𝗺𝗺𝗼𝗻 𝗲𝗺𝗽𝘁𝘆 𝗰𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗶𝗻𝗶𝘁𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗖#:


◾For lists: 𝗿𝗲𝘁𝘂𝗿𝗻 𝗻𝗲𝘄 𝗟𝗶𝘀𝘁<𝗧>();
◾For arrays: 𝗿𝗲𝘁𝘂𝗿𝗻 𝗻𝗲𝘄 𝗧[𝟬];
◾For dictionaries: 𝗿𝗲𝘁𝘂𝗿𝗻 𝗻𝗲𝘄 𝗗𝗶𝗰𝘁𝗶𝗼𝗻𝗮𝗿𝘆<𝗧𝗞𝗲𝘆, 𝗧𝗩𝗮𝗹𝘂𝗲>();
◾For sets: 𝗿𝗲𝘁𝘂𝗿𝗻 𝗻𝗲𝘄 𝗛𝗮𝘀𝗵𝗦𝗲𝘁<𝗧>();
◾For enumerable: 𝗿𝗲𝘁𝘂𝗿𝗻 𝗘𝗻𝘂𝗺𝗲𝗿𝗮𝗯𝗹𝗲.𝗘𝗺𝗽𝘁𝘆<𝗧>();

Returning null or an Empty Collection. When returning an array or collection, you must indicate that
there are zero items by returning either null or a collection instance with no items. The better choice, in
general, is to return a collection instance with no items.

https://www.linkedin.com/posts/sina-riyahi_backend-fullstack-developer-activity-
7209948004605218819-Bv-R?utm_source=share&utm_medium=member_desktop
Property Pattern Matching

💡Pattern matching is the process of taking an expression and testing whether it matches certain criteria,
such as ‘being a specified type’ or ‘matching a specified constant value’.

🔦Common to all patterns is the Boolean match/no-match characteristic, so, the specified expression
either matches the pattern or it does not. Pattern matching is a check and so can be used when
branching code.

Advantages of Using Pattern Matching in C#

🔎Enhanced readability.
✏️Reduction in lines of code.
🚀Improved maintainability.

Use Tuples instead of Classes


Don’t throw ex

Use meaningful names


Avoid magic strings

Usage of string concatenation


Using T casting

You might also like