C# Anti-Patterns: A List Of Programming Practices To Avoid
4.2 (101 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,303 students enrolled
Wishlisted Wishlist

Please confirm that you want to add C# Anti-Patterns: A List Of Programming Practices To Avoid to your Wishlist.

Add to Wishlist

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.2 (101 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,303 students enrolled
Created by Mark Farragher
Last updated 3/2017
English
Price: $90
30-Day Money-Back Guarantee
Includes:
  • 3 hours on-demand video
  • 1 Article
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I Learn?
  • Learn to identify many common anti-patterns
  • How to abuse the .NET type system
  • The curse of the Swiss-Army Knife
  • Use the YAGNI principle to remove Crystal Balls
  • Real-life bad coding practices from actual code reviews
  • The "Assume the Worst" principle
  • Why is "Pattern Love" a bad thing?
  • Incorrect use of Exceptions
  • ... and much more!
View Curriculum
Requirements
  • You need a Windows, Mac or Linux laptop with Microsoft Visual Studio, Xamarin Studio or Monodevelop
  • You should be a beginner, or intermediate C# developer
  • That's it!
Description

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

Code reviews sometimes expose truly terrible code - mangled object hierarchies, judicious use of Parse and ToString, flow-control logic using Exceptions, and "reinvented wheel" 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 counterproductive.

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. Enroll now to benefit from the current price and get lifelong free access to all future materials.

Who is the target audience?
  • Beginner, and intermediate C# programmers who want to learn how to write clean and well-designed code
  • Developers who are about to take a job interview and need to prepare for code quality-related questions
  • Professionals who are performing code audits on their team
Students Who Viewed This Course Also Viewed
Curriculum For This Course
Expand All 22 Lectures Collapse All 22 Lectures 02:54:11
+
Introduction
5 Lectures 19:31

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

Preview 05:20

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

Preview 03:50

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?

Introduction To Anti-Patterns
05:40

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.

Preview 03:19

Background Info - About Me
01:22
+
This Code Will Fail A Code Review
6 Lectures 45:32

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

Section Introduction
01:12

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.

Too Many Constants
08:57

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#.

Numeric Booleans
08:48

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.

Preview 10:41

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.

Manual Memory Management
11:22

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

Section Recap
04:32

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

Bad Coding Practices Quiz
5 questions
+
Bad Coding Practices From Actual Code Reviews
6 Lectures 51:23

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.

Redundant Loops
07:19

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.

The Anti-Singleton
09:36

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.

Preview 09:37

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.

Spaghetti Code
06:22

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.

Inconsistent Dates
10:37

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.

Magic Properties
07:52
+
Four Classic Anti-Patterns
4 Lectures 55:26

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.

Preview 13:30

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.

The Swiss-Army Knife
14:03

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.

Assume The Worst
13:18

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.


The Crystal Ball
14:35
+
Final Words
1 Lecture 02:27

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

Course Recap
02:27
About the Instructor
Mark Farragher
4.4 Average rating
773 Reviews
5,016 Students
10 Courses
Microsoft Certified Trainer - 700+ reviews, 4.5 rating

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 started using 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.

I have active MCSA and MCSD certifications plus legacy MCAD and MCT certifications. You can verify my credentials on my LinkedIn profile. 

Feel free to check out my course ratings before enrolling. As of today I have 9 published courses, 600+ reviews, and a recent average course rating of 4.41.