Powered by Blogger.
🌏World roaming Software Technology Evangelist. Proud Indian, Bought up from Coimbatore, Tamilnadu, INDIA. Pointing towards share of Knowledge. 😎
  • Programming ▼
    • DotNet
      • C# Coding Standards
    • Cloud
    • Microsoft 365/ SharePoint
    • SQL
    • Angular / ReactJS / NodeJS
    • Salesforce
    • Magento
    • Python
    • Mobile App Development
    • Database
    • DevOps
    • Automation Testing
    • User Experience
  • Learning ▼
    • Roadmap
    • Trainings
    • E-Books
    • Quick References
    • Certifications
    • Self Improvement
    • Productivity
    • TED Talks
    • Kids Programming
  • Software Engineering ▼
    • Agile
    • Software Design
    • Architecture Samples
    • Best Practises
    • Technologies and Tools
    • Open Sources
    • Free Softwares
  • Leadership ▼
    • Program Management
    • Product Management
    • Project Management
    • People Management
  • Job Search ▼
    • Interview Tips
    • Career Handbook
    • Resume Templates
    • Sample Profiles
    • Cover Letter Samples
    • HR Interview Questions
    • Job Websites List
    • Coding Site Links
    • TedEx Talks
    • International Jobs
  • Emerging Topics ▼
    • Innovation
    • Machine Learning
    • Artificial Intelligence
    • Generative AI
    • AI Tools
    • Big Data
    • Data Science
    • Data Analytics & Visualization
    • Cyber Security
    • Microsoft Azure
    • Amazon Web Services
    • Cryptography
    • ChatBots
    • Internet of Things (IoT)
    • Mixed Reality /AR/VR
  • Misc. ▼
    • Travel
    • Photography
    • Health Tips
    • Medical Tips
    • Home Designs
    • Gardening
  • Favourite Links ▼
    • Saran Kitchen Hut
    • World of Akshu
    • Saran & Akshu - Other Links

Referred Link - https://www.linkedin.com/feed/update/urn:li:activity:7229846743327096832


When it comes to writing good versus bad code in .NET, especially regarding validation (either form validation, business logic validation, or data validation), it’s important to adhere to best practices that enhance maintainability, readability, and performance of your code. Below are examples illustrating the differences between good and bad code in .NET, particularly focusing on a hypothetical Validator class.


❌ Issues with the Bad Code:
Hard-coded error messages: Printing messages directly in the validation method makes it less reusable and inflexible.

Magic numbers and values: Age limits are hard-coded, making it less maintainable.

Single Responsibility Principle violation: This method handles validation and error reporting.

Lack of structured error handling: There’s no way to retrieve error messages in a meaningful way after the validation fails.

✔ Improvements in the Good Code:
Error Collection: Instead of printing errors, we collect and return them as a list. This allows the calling method to decide how to handle the errors (e.g., logging, displaying them to a user).

Use of string.IsNullOrWhiteSpace: This is a more robust method to check for null or empty strings.

Encapsulation of logic: The validation logic is kept modular, which adheres to the Single Responsibility Principle.

Ease of testing: The return type allows easier unit testing by verifying returned errors for given inputs.

💡 Additional Best Practices for Validation in .NET:
🔦 Data Annotations: Utilize built-in data annotations (e.g., [Required], [EmailAddress], etc.) to leverage built-in validation features.

🔎 Fluent Validation: Consider using libraries like FluentValidation to create more complex validation rules in a fluent interface.

🕯 Separation of Concerns: Separate your validation logic from business logic. Consider using a dedicated service or function for validations.

🌠 Asynchronous Validation: If validation includes I/O operations (like checking email availability), consider making your validation methods asynchronous.

⭐ Custom Exceptions: Instead of returning a list of strings for validation errors, consider throwing custom exceptions that can carry context about what went wrong.

By adhering to these practices, you can ensure that your validation logic remains efficient, maintainable, and easy to understand.

Tags:
#backend hashtag#fullStack hashtag#developer hashtag#Csharp hashtag#github hashtag#EFCore
hashtag#dotnet hashtag#dotnetCore hashtag#programmer hashtag#azure hashtag#visualstudio
Newer Posts
Older Posts

Search this Site

Translate Articles

Total Posts

Total Pageviews


Contributors

My photo
Jay Srinivasan
Professional: I'm a Software Techie, Specialized in Microsoft technologies. Worked in CMM Level 5 organizations like EPAM, KPMG, Bosch, Honeywell, ValueLabs, Capgemini and HCL. I have done freelancing. My interests are Software Development, Graphics design and Photography.
Certifications: I hold PMP, SAFe 6, CSPO, CSM, Six Sigma Green Belt, Microsoft and CCNA Certifications.
Academic: All my schooling life was spent in Coimbatore and I have good friends for life. I completed my post graduate in computers(MCA). Plus a lot of self learning, inspirations and perspiration are the ingredients of the person what i am now.
Personal Life: I am a simple person and proud son of Coimbatore. I studied and grew up there. I lost my father at young age. My mom and wife are proud home-makers and greatest cook on earth. My kiddo in her junior school.
Finally: I am a film buff and like to travel a lot. I visited 3 countries - United States of America, Norway and United Kingdom. I believe in honesty after learning a lot of lessons the hard way around. I love to read books & articles, Definitely not journals. :)
View my complete profile

Certifications

Certifications

My Favorite Links

  • Saran & Akshu Links
  • Saran Kitchen Hut
  • World of Akshu
  • Ashok Raja Blog

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Contact Form

Name

Email *

Message *

Connect with Me

Blog Archive

  • ►  2025 (48)
    • ►  June (7)
    • ►  May (26)
    • ►  April (1)
    • ►  March (3)
    • ►  February (1)
    • ►  January (10)
  • ▼  2024 (134)
    • ►  December (3)
    • ►  November (8)
    • ►  October (11)
    • ►  September (2)
    • ▼  August (1)
      • Good Code and Bad Code - Tips for Programmers
    • ►  July (39)
    • ►  June (8)
    • ►  May (4)
    • ►  April (9)
    • ►  March (6)
    • ►  February (33)
    • ►  January (10)
  • ►  2023 (16)
    • ►  December (12)
    • ►  August (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2022 (14)
    • ►  December (1)
    • ►  August (6)
    • ►  July (3)
    • ►  June (2)
    • ►  February (1)
    • ►  January (1)
  • ►  2021 (16)
    • ►  December (1)
    • ►  November (2)
    • ►  October (2)
    • ►  August (1)
    • ►  July (2)
    • ►  June (2)
    • ►  May (2)
    • ►  March (2)
    • ►  February (1)
    • ►  January (1)
  • ►  2020 (36)
    • ►  December (1)
    • ►  November (15)
    • ►  October (2)
    • ►  September (1)
    • ►  July (1)
    • ►  June (2)
    • ►  May (4)
    • ►  March (2)
    • ►  February (6)
    • ►  January (2)
  • ►  2019 (14)
    • ►  December (3)
    • ►  November (1)
    • ►  September (2)
    • ►  August (1)
    • ►  June (1)
    • ►  May (3)
    • ►  March (2)
    • ►  January (1)
  • ►  2018 (61)
    • ►  November (3)
    • ►  October (4)
    • ►  September (4)
    • ►  August (5)
    • ►  July (4)
    • ►  June (4)
    • ►  May (7)
    • ►  April (7)
    • ►  March (5)
    • ►  February (1)
    • ►  January (17)
  • ►  2017 (55)
    • ►  December (1)
    • ►  November (7)
    • ►  October (7)
    • ►  September (8)
    • ►  July (4)
    • ►  June (7)
    • ►  May (4)
    • ►  April (4)
    • ►  March (1)
    • ►  February (2)
    • ►  January (10)
  • ►  2016 (45)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (7)
    • ►  August (3)
    • ►  July (3)
    • ►  June (1)
    • ►  May (3)
    • ►  April (5)
    • ►  March (3)
    • ►  February (3)
    • ►  January (9)
  • ►  2015 (88)
    • ►  December (5)
    • ►  November (2)
    • ►  October (6)
    • ►  September (6)
    • ►  August (3)
    • ►  July (6)
    • ►  June (7)
    • ►  May (12)
    • ►  April (6)
    • ►  March (11)
    • ►  February (10)
    • ►  January (14)
  • ►  2014 (159)
    • ►  December (16)
    • ►  November (13)
    • ►  October (42)
    • ►  September (12)
    • ►  August (19)
    • ►  July (3)
    • ►  June (17)
    • ►  May (10)
    • ►  April (12)
    • ►  March (7)
    • ►  February (4)
    • ►  January (4)
  • ►  2013 (192)
    • ►  December (7)
    • ►  November (2)
    • ►  October (3)
    • ►  September (10)
    • ►  August (25)
    • ►  July (17)
    • ►  June (22)
    • ►  May (22)
    • ►  April (24)
    • ►  March (17)
    • ►  February (22)
    • ►  January (21)
  • ►  2012 (204)
    • ►  December (21)
    • ►  November (35)
    • ►  October (47)
    • ►  September (27)
    • ►  August (6)
    • ►  July (21)
    • ►  June (16)
    • ►  May (7)
    • ►  April (9)
    • ►  March (4)
    • ►  February (3)
    • ►  January (8)
  • ►  2011 (70)
    • ►  December (8)
    • ►  November (5)
    • ►  October (3)
    • ►  September (2)
    • ►  August (7)
    • ►  July (3)
    • ►  June (30)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (3)
    • ►  January (2)
  • ►  2010 (30)
    • ►  December (1)
    • ►  September (4)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (4)
    • ►  April (6)
    • ►  March (5)
    • ►  February (2)
    • ►  January (5)
  • ►  2009 (40)
    • ►  December (4)
    • ►  November (6)
    • ►  October (4)
    • ►  September (5)
    • ►  August (4)
    • ►  July (3)
    • ►  June (4)
    • ►  May (8)
    • ►  March (1)
    • ►  February (1)
  • ►  2008 (6)
    • ►  December (1)
    • ►  September (1)
    • ►  May (1)
    • ►  April (2)
    • ►  February (1)
  • ►  2007 (7)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  July (1)
    • ►  May (2)

Recent Posts

Followers

Report Abuse

FOLLOW ME @INSTAGRAM

Popular Posts

  • Stay Wow - Health Tips from Sapna Vyas Patel
    Referred URL https://www.facebook.com/sapnavyaspatel WATCH WEIGHT LOSS VIDEO: http://www.youtube.com/ watch?v=S_dlkjwVItA ...
  • Calorie Count chart For food and drinks
    Referred URL http://deepthidigvijay.blogspot.co.uk/p/health-diet-calorie-charts.html http://www.nidokidos.org/threads/37834-Food-Calorie-...
  • SharePoint 2010 Interview Questions and Answers
    Referred URL http://www.enjoysharepoint.com/Articles/Details/sharepoint-2010-interview-questions-and-answers-148.aspx 1.What is SharePoint...
  • 150 Best Windows Applications Of Year 2010
    Referred URL : http://www.addictivetips.com/windows-tips/150-best-windows-applications-of-year-2010-editors-pick/?utm_source=feedburner...
  • Web Developer Checklist by Mads Kristensen
    Referred Link -  http://webdevchecklist.com/ Web Developer Checklist Get the extension  Chrome  |  Firefox  |  Edge Menu Bes...
  • WCF and REST Interview Questions
    What is WPF? The Windows Presentation Foundation (WPF) is a next generation graphics platform that is part of...
  • Remove double tap to unlock feature on samsung galaxy core2
    Double tap to unlock is a feature of Talkback, so if your will disable Talkback, double tap to unlock will also be disabled. To disable doub...
  • Difference Between Content Editor and Script Editor webpart
    Referred Link -  http://jeffas.com/content-editor-vs-script-editor-webpart/ Content editor web part is a place holder for creating rich ...
  • SPFolder related operations in SharePoint
      1) Get SPListItem(s) of a particular SPFolder SPList splist; SPFolder spfolder; //Get the required folder instance SPQuery spquery = new ...

Comments

Created with by BeautyTemplates | Distributed by blogger templates