Python – Expect an exception using pytest

When you want to add a test to verify that an exception is raised, you can use pytest.raises() like this: When the code doesn’t raise an exception, you’ll see a test failed message like this: Example – Expect WordCounter to raise an exception I’m implementing the following WordCounter class using a test-driven approach. The first … Read more Python – Expect an exception using pytest

Refactoring the Nested Conditionals code smell

Nested conditionals are a code smell because they make the code harder to understand. They tend to grow more and more complicated over time because developers will keep adding conditions and more levels of nesting. The deeper the nesting, the more time it’ll eventually take to refactor it. Code Smell: Nested conditionals.Definition: A conditional block … Read more Refactoring the Nested Conditionals code smell

C# – Parsing commands and arguments in a console app

In a console app there are two ways to get commands: Command line arguments passed into your program via Main(string[] args). User input from Console.ReadLine() (which you then split into a string[]). After getting a command, you have to parse it to figure out what code to execute. Typically commands have the following format: commandName … Read more C# – Parsing commands and arguments in a console app

C# – Waiting for user input in a Console App

The following code shows how to wait for user input in a Console App: When the user types something in and presses the Enter key, Console.ReadLine() will return what they typed. Console.ReadLine() vs Console.ReadKey() Console.ReadLine() waits for the user to press Enter, and then returns everything they typed in. Console.ReadKey() returns individual key presses. It … Read more C# – Waiting for user input in a Console App

System.Text.Json – How to customize serialization with JsonConverter

Most of the time JsonSerializer will get you want you want. You can pass in options to control serialization to a certain extent. But sometimes you’ll run into scenarios where you need to customize how it handles serialization for a specific type. This is where JsonConverter comes in. To customize serialization for a specific type, … Read more System.Text.Json – How to customize serialization with JsonConverter

System.Text.Json.JsonException: A possible object cycle was detected which is not supported

When you use System.Text.Json.JsonSerializer to serialize an object that has a cycle, you’ll get the following exception: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 0. This is the same problem … Read more System.Text.Json.JsonException: A possible object cycle was detected which is not supported

WinForms – How to programmatically check items in CheckedListBox

A CheckedListBox allows the user to check one or more checkboxes. Sometimes you’ll want to be able to check the boxes programmatically. For example, you may want to allow the user to check or uncheck all boxes at once. Or perhaps you want to persist the values the user checked and load them later. To … Read more WinForms – How to programmatically check items in CheckedListBox

C# – Mapping nullable columns with SqlDataReader

When you have a database table with null columns, and you’re using SqlDataReader to map the results, there are a few things to do: Use nullable types in the model class. Example: instead of int use int? so the value can be null. Use SqlDataReader.IsDBNull() to check if you need to set the value to … Read more C# – Mapping nullable columns with SqlDataReader

C# – Using SqlDataReader to process multiple result sets

In this article I’ll show how to use the SqlDataReader ADO.NET class in two scenarios involving multiple result sets: Batches – When you execute multiple SELECTs in a single query. Each SELECT returns a different result set. You use a single reader to process the batch. Nested Queries – When you use multiple SqlDataReaders on … Read more C# – Using SqlDataReader to process multiple result sets

C# – Use StringAssert when testing a string for substrings

When you’re testing if two strings are equal, you can simply use Assert.AreEqual(). When you’re testing if a string contains a substring or a pattern, typically developers use Assert.IsTrue() with a substring method or regex. You should use StringAssert instead, because it gives better failure messages. The key problem is Assert.IsTrue() gives useless information. You … Read more C# – Use StringAssert when testing a string for substrings