Menu

Post image 1
Post image 2
1 / 2
0

Refactoring a Simple C# Method Step by Step

DEV Community·Spyros Ponaris·27 days ago
#xDJ1e7eX
Reading 0:00
15s threshold

Refactoring is not about changing what the code does. It is about improving how the code is written. The goal is to make the code easier to read, easier to test, and easier to maintain. In this tutorial, we will refactor a simple user registration method step by step. The Original Code Imagine we have a method like this: public void RegisterUser ( string username , string password , int age , string email ) { if ( username == null || username == "" ) { throw new Exception ( "Username is required" ); } if ( password == null || password == "" ) { throw new Exception ( "Password is required" ); } if ( password . Length < 8 ) { throw new Exception ( "Password must be at least 8 characters" ); } if ( age < 18 || age > 120 ) { throw new Exception ( "Invalid age" ); } if ( email == null || ! email . Contains ( "@" ) || ! email . Contains ( "." )) { throw new Exception ( "Invalid email" ); } Console .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More