Clean Code: Learn to write clean, maintainable and robust code

Clean Code: Learn to write clean, maintainable and robust code.

In this video, I take a poorly-written piece of code and refactor in a step-by-step fashion. I also teach you some productivity tips along the way that helps you write code fast with Visual Studio.

If you enjoy my teaching style and want to learn more from me, check out my courses on Udemy:

C# Intermediate: Classes, Interfaces and Object-oriented Programming

C# Advanced: Take Your C# Skills to the Next Level

Clean Code: The Art of Writing Beautiful C# Code

C# Developers: Double Your Coding Speed

The Complete Backbone.js Course from Beginner to Advanced

Connect with me on social media:

My blog








34 responses to “Clean Code: Learn to write clean, maintainable and robust code”

  1. ChrisUMB Avatar

    When you were fixing the SQLInjection bit, that was already fixed. He enclosed the entire string entry in a string, both at the beginning AND end, and I feel like you realized that when you went to put the final apostrophe in.

  2. silver1000n2 Avatar

    Your coding isn't any better.

  3. Rabi Moshe Hizskia Avner goldenberg Shekelstein Avatar

    I hate the var thing.
    No reason to use it its much better in my idea to do:
    MemoryReader mem = GetMemory();
    var mem = GetMemory();

    With the var type you look at it and u have no idea what get memory does without look at it which wastes time.

  4. King Parodije Avatar

    call me old fashioned, but I like to have all code on one place.

  5. blipman17 Avatar

    Can I just note that there's an "inline variable" shortcut nowdays. that does exactly what you want to do at 8:50? It's "Ctrl + R & Ctrl + I." This does not seem like much, but it also works for methods.

  6. Unique GOD **i** Avatar

    is there a way to create shortcut codes for example just typing "main" we get "public static void main(String[] args){}"
    like sublimetext 3 in vs code

  7. Nick Cardinal Avatar

    Solid work my friend, thank you!

  8. james Dudley Avatar

    he used or instead of '||'

  9. Joshua Weaver Avatar

    oh did someone mention the Using thing? Everyone trying to be a clean coder making a filthy mess of the comments.

  10. Joshua Weaver Avatar

    Learned some things. I will soon be coding in VS. So thanks. I am not sure I will move away from declaring my variables at the top in one section though. They can be a sectioned outline for proofing the Cleaner work section. I like to set them early which also helps debugging, cleaning, and possibly removing them to make it modular code.

  11. Sudikinoko Avatar

    Is there a reason to not use the service class as a static class? Its a little bit a pain in the eye to see the code seperated just to instantiate a new class or a I missing something?

  12. Saied Hassaninia Avatar

    If the var reader = command.ExecutreReader(); is outside the try, does and an exception is thrown does it still go into the finally block?

  13. Valerii Bobrovskyi Avatar

    індуський гавнокод

  14. Marwan Iddouche Avatar

    i don't know if that is available on visual studio
    i want to have that vertical line that cross conditions
    | code ;
    | code;
    | code;
    | code;
    | code;
    any help pleas !

  15. brockalicous merk Avatar

    Guy's if you're a student go to JetBrains apply for a student license and you can get all kinds of stuff and Resharper is in that pack. So you don't need to buy ReSharper!!

  16. Pramod Sutar Avatar

    Great content with well explanation. Keep it up.

  17. Madolite Avatar

    I generally don't like var (because it's less readable if you have a lot of them), but I realize that it's useful as long as it's used sparingly, e.g. in place of really long class names like SqlConnection. Nice tip. 🙂

  18. Vishal Kotecha Avatar

    Advertising Re-sharper?

  19. kevindt12 Avatar

    Rename is the best thing i have ever found out i justt bined it to F2 use it alease every 10 min

  20. VelezBiH Avatar

    Thank you for sharing this is very informative.

  21. Peter Buchmann Avatar

    Well, not that bad. But how would you make your code testable?
    This would be the next step.
    And one bad habit: Using local variables starting with an underscore is ugly. Underscores are ugly anyway and should not be used in C# (stylecop SA1310:

  22. 楊漢軒 Avatar

    hey yo Mosh! Nice concept and tutorial! Thanks!

  23. Anthony Berent Avatar

    Is there a bug in the final code? What happens if executeReader() throws? It looks to me as if the connection is never closed in this case.

  24. Ronald Mariah Avatar

    Aren't you still executing the command out of the try block. Exception can still occur. And the connection will not be closed.

  25. Gregg Wonderly Avatar

    You should be using the using(){} construct for anything implementing IDisposable. try{}finally{} doesn't work right here if the query is malformed.

    This is a misleading solution because it does not completely cover the needed error checking and handling, at all.

    Practically, you should never teach people to code methods like this. Instead, they should be coding a BaseDAO class for their application which encompasses all of the behaviors they need plus the use of the appropriate database specific details such as the need for "as" on aliases or any other details. Such utility functions make it much easier to then create an interface for your BaseDAO functionality, and then use "unity" to plug in an appropriate implementation for each database that your application might need to use.

  26. Marcsello Hooves Avatar

    You should include into the title "apsx With Visual Studio + Seccurity tips"… You are talking about SQL injection protection, which is a) Have nothing to do with being clean, maintainable or robust, 2) you solved it using something that only works in that aspx thingy. SQL Injection protection sure important, but I'm not came here to study about it in a language that I hope I'll never have to use. (sorry, I stopped watching here)

    This is a good video for sure… but the title is a bit misleading… ^^

    P.S: why do you call someone a hacker, who can use SQL injection?

  27. hojo70 Avatar

    Most of your points are pedantic.

  28. Priya SivaKumar Avatar

    Helped a lot for the freshers like me..Thanks Mosh.

  29. Khalid Mohammad Avatar

    the opening music, It is not strange to me

  30. Christopher Mayer Avatar

    Insightful an useful content. Thank you sir!

  31. mamounfj mamounfj Avatar

    This video is better than paid courses or reading books.

  32. Hazarth Avatar

    To be honest, I hate the "var" thing. I think it makes the code less readable. Sure, it might help with something like var connection = new SqlConnection(…), because that one is obvious, but var reader = command.ExecuteReader(); is a horrible thing to do in my opinion. I now have no idea what type it is that ExecuteReader() even returns. It might be a SqlDataReader, but if i Didn't know that upfront, it might as well be a StreamReader of any other kind. And since the function says "execute" and "Reader" it sounds like some kind of reader was already executed, so do I expect a string? or an IEnumerable<> of some type? Without previous knowledge of SqlDataClient lib I would have no idea what is going on and it would take extra time to process that or google that.
    There are places where var makes sense and saves time and space… but it's not a one in all solution and shouldn't be used that way.
    Who agrees?

  33. krige Avatar

    You can "extract a method" from a selected lines of code using the CTRL+R+M shortcut. You can surround a selected number of rows with a try-finally block using the CTRL+ALT+J shortcut, although I would rather use the "using" statement instead of the try-finally block. I don't see any reason why defining StockService as a class property: I would rather define the method GetProductQuantityFromStock as static method. I don't see any reason why private properties should be prefixed with underscore. Resharper can move a method in another class for you, I don't remember the shortcut, maybe F6?

Leave a Reply

Your email address will not be published. Required fields are marked *