C# Anti-Patterns: A List Of Programming Practices To Avoid

This course will teach you many bad programming practices that every professional developer must know, but never use
4.4 (61 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
1,017 students enrolled
$19
$50
62% off
Take This Course
  • Lectures 23
  • Length 3 hours
  • Skill Level All Levels
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 7/2015 English

Course Description

Updated on September 1, 2015. Added two new anti-patterns: Avoid The Worst and The Crystal Ball.

BEST COURSE CHOICE for beginners and intermediate C# developers who want to take their programming skills to the next level.

Do you know that feeling?

A new developer has joined your team. You assign a short task to get started, and a couple of days later you do a quick code review to check if everything is ok.

As your eyes glance over the first few lines of code you spray your Cola all over your keyboard, and all you can think is:

"What is this I don't even?"

We've all been there. Code reviews sometimes expose truly terrible code - mangled object hierarchies, judicious use of Parse and ToString, and reinvented standard framework classes littered with bugs.

In fact the same bad code tends to crop up over and over, which is why we call them "Anti-Patterns".

Anti-patterns are common responses to a recurring problem that are ineffective and highly counter-productive.

In a series of short lectures I am going to show you the most common C# anti-patterns. I will introduce each anti-pattern, show you why the pattern is bad, and how you can refactor the code to resolve the problem.

Why should you take this course?

You should take this course if you are a beginner or intermediate C# developer and want to take your skills to the next level. A thorough understanding of all common anti-patterns will help you enormously in your understanding of C# and the .NET Framework.

Or you might be working in a developer team and about to perform your first code review. The examples in this course will help you spot bad code quickly, and give you an idea what senior developers like to see at when they review code.

Or maybe you're preparing for a C# related job interview? This course will give you an excellent foundation to answer any questions about code quality they might throw at you.

30 day money-back guarantee

This course comes with an unconditional, Udemy backed, 30-day money-back guarantee. If you are dissatisfied with the course for any reason, simply request a refund and get your full purchase amount back, no questions asked.

Act now

This course contains over 3 hours of video content and I am adding new material every month. Enrol now to benefit from the current price and get lifelong free access to all future materials.

As a course student you can contact me and ask me for help with any performance-related C# problem you might have. I am available on weekdays during office hours (Central European Time).

Enrol now!

What are the requirements?

  • You should have a Windows, Mac or Linux laptop with Microsoft Visual Studio, Xamarin Studio or Monodevelop installed
  • You should be familiar with the C# language
  • That's it!

What am I going to get from this course?

  • Learn from a certified Microsoft trainer from your own desk
  • Many hours of video content showing you code that abuses the .NET type system, incorrectly uses exceptions, suffers from bad object design, does not perform, and much more!
  • Learn how to identify a Swiss-Army Knife in your code
  • Apply the YAGNI principle to remove Crystal Balls from your code
  • See real-life bad coding practices from actual code reviews
  • Learn what kind of code would never pass a code review
  • What is "Pattern Love", and why is it a bad thing?

What is the target audience?

  • This course is for beginner and intermediate C# programmers who want to learn how to write clean and well-designed code
  • Do you need to perform a code review on a new member of your development team? Then this course is for you too!
  • Ideal for students who are about to take a job interview, and need to prepare for questions related to code quality

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction
05:20

In this lecture I explain how this course is organized and I describe each of the upcoming sections in detail.

03:50

In this lecture I will tell a bit more about myself, my career, and my motivation to become an online trainer.

05:40

In this lecture we're going to look at the theory behind anti-patterns. What exactly is an anti-pattern, what can we do about it, and why does this kind of code keep showing up in code reviews?

03:19

Many lectures in this course contain source code examples. Feel free to download the code and follow along. And here's the good news: it doesn't matter if you have a Window, Mac or Linux computer. The code will run on all three operating systems.

In this lecture I demonstrate how my solutions and projects run on all operating systems. I will show you how to build and run the source code on a Mac, on Linux and in Visual Studio running on Windows 8.

At the end of this lecture you will have learned that .NET code is portable and can run on at least five different operating systems.

Section 2: This Code Will Fail A Code Review
01:12

Welcome to the Basic Anti-Patterns section. I will give a quick introduction on how the section is organized before we get started.

08:57

The correct use of constants helps a lot to make your code more easily readable. Also, constants are super-handy when you need to make changes in the future. Instead of having to hunt through your code, finding each occurrence of a specific literal and changing it, you simply change the constant directly.

However, sometimes during a code review you encounter two extremes: code without any constants whatsoever, and code where literally everything has been turned into a constant.

In this lecture I will show you some code on the extreme ends of the scale, and I'll teach you some handy tips to find the sweet spot in the middle between these two extremes.

08:48

In the .NET Framework the Boolean type is a first class citizen. Both Boolean 'true' and 'false' values are explicitly defined and cannot be cast to integers. This is very different from the C language, where boolean 'true' and 'false' values correspond to certain integer values, and boolean values can be freely cast to integers and back.

C programmers making the transition to C# often retain certain defensive programming practices. These practices work well in C, but result in very strange C# code.

In this lecture I will teach you how to spot these practices. I will show you the line of thinking behind each defensive practice, and then teach you the correct way to program in C#.

10:41

The .NET Framework provides a very powerful DateTime type with lots of helper methods to manipulate date and time values, calculate timespans, find specific calendar dates, and much more.

But sometimes developers come from a much more restrictive language which offers almost no support for date and time operations. They develop convoluted workarounds to get around this limitation, and bring these code patterns to C#. This can result in some very weird code.

In this lecture I will show you an extremely bad and convoluted way to calculate this week's date range. Then I'll show you how to do it right by fully relying on the DateTime helper methods.

11:22

The .NET Framework features a Garbage Collector: an automatic process that identifies all variables that have gone out of scope and deallocates their corresponding objects. A C# developer never has to worry about deallocating variables. This happens automatically in the background, and the process is highly reliable.

But sometimes developers come from a more restrictive environment that doesn't have a Garbage Collector, and they get into the habit of manually deallocating all their variables. When they bring this practice to C#, the result is lots of redundant and unnecessary code.

In this lecture I will show you some code from a developer performing manual memory management, I will explain why the code is bad, and I'll show you what the correct equivalent code in C# should be.

04:32

Congratulations on finishing this section. This is a recap of what we have learned.

5 questions

Test your knowledge of bad coding practices with this short quiz.

Section 3: Bad Coding Practices From Actual Code Reviews
07:19

Sometimes a code review will expose very strange code. What would you say about a loop that only loops once, by design? What could possibly be the reason for writing code that way? Isn't a loop that loops only once every time completely redundant?

After completing this lecture you will have learned what the developers were trying to achieve with the loop, and where their line of thinking went wrong.

09:36

The singleton pattern is the black sheep of the patterns & practices community. Singletons were popular in the past, but today most developers agree that they are best avoided. That does not deter everybody though.

In this lecture I am going to show you a singleton from a code review that wraps the SmtpClient class. I'll demonstrate why this is a terrible idea, I will point out a nasty bug in the code, and I'll provide you with a scenario where the use of a singleton is still acceptable.

After completing this lecture you will have learned when using singletons is allowed, and how to correctly implement one yourself.

09:37

Sometimes we see expressions crop up in code reviews that look like this:

if (1 == variable) { ... }

The variable appears on the right hand side and the argument on the left hand side of the equals operator. We call this a 'Yoda expressions' after the famous Star Wars character who flips words around in his speech.

In this lecture you will learn why some developers use Yoda expressions, you will see why we consider them anti-patterns in C#, and I will show you a niche case where a Yoda expression is very useful and saves you an extra if statement.

06:22

Spaghetti code is very convoluted code that lacks a clear control structure. So obviously spaghetti code is considered an anti-pattern because the code is unreadable and hard to maintain.

But did you know that anyone can fall into the trap of producing unreadable spaghetti code? In this lecture I will show you how a simple process of repetitive code refactoring leads to spaghetti code, and how the code can be fixed by undoing the refactoring steps.

10:37

In this lecture I'm going to show you some bad date and time handling code that occasionally show up in code reviews. Sometimes a programmer will go to great trouble to simply copy one date into another. I'll show you the reasoning behind this practice, and why we consider it an anti-pattern in C#.

Other developers will repeatedly read from DateTime.Now or DateTime.Today to perform a date calculation. I will show you how this practice introduces a nasty bug where the calculation breaks down in a specific edge case.

07:52

In this lecture I will show you code from a real-life code review that does some very strange things with a property. At first glance the code appears to be completely broken. But then I'll show you some hidden functionality inside the property, and it turns out that all of our assumptions were incorrect.

By the end of the lecture you will have learned that properties with unexpected hidden functionality are considered anti-patterns, because they produce code that is extremely hard to maintain.

Section 4: Four Classic Anti-Patterns
13:30

This classic anti-pattern is a very interesting one, because it involves the overuse of actual correct design patterns. A developer afflicted by Pattern Love will implement design patterns everywhere, including in places where they are completely inappropriate.

In this lecture I will show you how to spot Pattern Love in your own code. I will also show you a very interesting and somewhat convoluted application affected by Pattern Love, and I will teach you how to refactor Pattern Love code to resolve the anti-pattern.

14:03

Swiss-Army Knives are objects that try to do too many things at the same time. This leads to very confusing code that is hard to maintain.

In this lecture I will show you how to spot a Swiss-Army Knife object in your own code. I will demonstrate a stock trading application that contains a Swiss-Army Knife object, and I'll show you how you can easily recognise these objects in a class diagram.

Then we will refactor the code and resolve the anti-pattern by using the healthy 'facade' pattern. I will show you modified mutual fund code that accomplishes the same objective, but does not rely on a Swiss-Army Knife.

13:18

The Assume The Worst anti-pattern springs from an overly defensive mindset. In this pattern a developer is constantly writing defensive code that assumes an absolute worst-case scenario. This leads to code with an irrationally high amount of exception handling, or an architecture that is ridiculously over-engineered for its intended purpose.

In this lecture I will show you several examples of Assume The Worst code. Then I'll teach you which red flags to look out for, and how you can refactor Assume The Worst code to resolve the anti-pattern.

14:35

Crystal Balls are blocks of code that are overly generic and make wild assumptions about future developments, with little or no justification for the generic architecture.

In this lecture I will show you an order processing module that has been reused too many times and has turned into a crystal ball. I will teach you how to recognise crystal balls in your own code, and how you can use a very simple design principle from Extreme Programming to resolve this anti-pattern.


Section 5: Final Words
02:27

In this lecture I would like to thank you for finishing the course and offer some final words.

Section 6: Bonus Section
02:34

In this bonus lecture I would like to offer you an exclusive discount on another of my courses that teaches you how to radically speed up your C# code.

02:38

In this bonus lecture I would like to offer you an exclusive discount on another of my courses that teaches you how to write robust and "bulletproof" multi-threaded C# code.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Mark Farragher, Microsoft Trainer

Hi I'm Mark, and I am very pleased to meet you!

My IT career spans 2 decades and I've worn many different hats over the years. I am a serial entrepreneur and have launched two startups in The Netherlands. I've also been a CTO three times.

I started working with C# and the .NET framework 15 years ago. Today I live in Barcelona and spend my time creating online courses to share my knowledge of professional C# programming with a wide audience.

Ready to start learning?
Take This Course