Categories
Coding

Don’t forget about character casing when comparing strings!

There are many issues I’ve experienced during the many, many years I’ve worked as a software developer. But one of the most recurring issues is, without a doubt, the mismatching of words due to character casing.

There are solutions to the character casing mismatch problem. For example, you can make your strings all lower case or upper case before comparing them. There are also many programming languages that have features to help with string comparisons.

The issue is no longer the lack of solutions to avoid this problem. The problem is that these solutions require that you, the developer, be proactive by being alert and aware of case sensitivity when making string comparisons. For example, in C#, you have the StringComparer class, which includes properties like StringComparer.OrdinalIgnoreCase to help you ignore the character’s casing when comparing strings.

As a developer, you have to be alert and know when to ignore character casing. While there are many simple ways and tools built into programming languages, sometimes knowing when to do this might not be obvious.

For example, if you call GroupBy in C# and select the value you want your list to be grouped by, it will consider values such as “Abc” and “ABC” as unique, which might not be what you want to do.

In most cases, if you group a list of items by a specific string value, your intention is probably to treat the same values “Abc” and “ABC” as the same. Therefore, you’ll want to ignore the casing as the values are the same in this context.

Issues like the one with GroupBy in C# can go unnoticed until it causes problems. For example, I ran into this issue and didn’t realize the mistake until I tried to add the values of that grouped list to a dictionary and failed. The dictionary attempted to use the values “Abc” and “ABC” as the dictionary key, but it failed since these aren’t unique.

So what can you do about this? Code defensively. Every time you compare strings, consider character casing sensitivity and avoid it easily by converting all your strings to upper case or lower case before comparing. Second, be aware of the use cases where you are calling a built-in function such as GroupBy or ToDictionary as functions like this might be case-sensitive within your programming language.

With the programming language C#, you can use overloads that explicitly specify the string comparison rules for string comparisons. It works in this language by calling a method overload that has a parameter of type StringComparison.

In the example below, I’ll be using StringComparison.OrdinalIgnoreCase for comparisons for culture-agnostic string matching. The example shows you how not ignoring case sensitivity might give you unexpected results.

Examples in C#

Let’s declare a list of books with author names written using different casing

var books = new List<Book>()
{
new Book { Name = "Programa en donde sea", Author = "Ricardo" },
new Book { Name = "Empieza a programar", Author = "ricardo" },
new Book { Name = "Xyz", Author = "Joe" },
new Book { Name = "Despues de la programacion", Author = "RICARDO" },
new Book { Name = "Blah", Author = "Foo" }
};

Let’s group the list of books by Author, but since we are not doing anything to ignore case sensitivity, the result is not what’s expected – It returns five records instead of three as it treats all variations of the name Ricardo as unique values.

var notAUniqueListOfBooks = books.GroupBy(b => b.Author);

Now let’s group the same list of books by author, but this time let’s add a parameter to make the string comparison case insensitive. The result is only three records, that’s because it treats all the variations of the Author name Ricardo as the same value.

var aUniqueListOfBooks = books.GroupBy(b => b.Author, StringComparer.OrdinalIgnoreCase);

Let’s now create a dictionary from the list of books. This dictionary will use the Author value as the key, and both the book’s name and author as the value. The result is five items in the dictionary, again, because it treats the each instance of the author name Ricardo as a unique value due to the difference in casing.

var notAUniqueBookDictionary = books.ToDictionary(b => b.Author, b => b);

Finally, we’ll try to create a dictionary following the same attributes above, but this time, we’ll pass the parameter StringComparer.OrdinalIgnoreCase to make sure the comparison is case insensitive.

The result if this last one is an error with the following message:

“An item with the same key has already been added. Key: ricardo”

This is because since we are ignoring the casing in Author, we cannot create a dictionary as the key values are required to be unique and by ignoring the case of the different variations of the value Ricardo, these are no longer unique. They all end up being the same exact value.

var aUniqueBookDictionary = books.ToDictionary(b => b.Author, b => b, StringComparer.OrdinalIgnoreCase);

Finally, using the examples above, if you wanted to group by Author, and then create a list of all of their books including the name and author values then you could try using ToLookup, and pass the StringComparer parameter to make sure the string comparison in case insensitive.

var aUniqueLookup = books.ToLookup(b => b.Author, b => b, StringComparer.OrdinalIgnoreCase);

The above will give you a dictionary where the key is the Author name and the value is a list of books including name and author. Also, by passing the StringComparer.OrdinalIgnoreCase parameter, we are making sure that the result is a unique list of values.

This the result of our book list when converted into a Lookup object in C#. There are three keys, all unique, and under each key we have a list of books that corresponds to the book’s author representing the Key value.

I hope this is useful, the code I used to test the examples above is all available here if you want to play with it and explore changing the values, parameters, etc. Cheers and happy coding!

Categories
How-To Notes

Focus and avoid context switching

My day job as a software engineer requires a great deal of focus and organization. Writing code is one of the last steps you do in software development. Writing code only comes after gathering enough information and understanding what changes or features we need in an application.

Focusing on one task, but more importantly, one project at a time is critical. When working on more than one project at a time, there is a lot of context switching, which is terrible for you and your productivity.

Focusing on one task also allows you to fully immerse into the details of the project, expand your domain knowledge about the work area, and as a result, be thoughtful about the way you approach the project, its problems, and the solutions.

Context switching while doing any work that requires concentration decreases the project’s opportunity to succeed and your productivity as well. The result is a net negative, and I don’t see any reason to do it. The only reason we do it might be due to our inability to focus on one task or the constant interruptions that are common in the workplace.

Focusing today is more challenging than ever. We have many tools around us that trick us and push us to pursue distractions. The handheld devices we all have are the number one reason for this, in my opinion. These devices are the window into a lot of addictive content out there, and trying to stay focus while having these devices next to us requires a lot of discipline.

It’s not all our fault. We are the victims of advanced algorithms that know us well and learn how to get our attention. It takes a lot to turn off notifications and not open our favorite apps to see the latest micro-piece of content. But, it’s a very effective drug that works against us.

Of course, there are simple ways to minimize this. I, for example, have most of my notifications off. The only place where I turn notifications on is on my family chat. Other than that, I never get pulled in by an app since I don’t get notified about anything. So that works, but just a little.

There are other things I do to keep myself focused and stick to one task at a time. For example, I set up specific times (timeboxing) to do the other distracting tasks such as checking email, social media, the news, etc. I timebox these tasks and try hard not to allow myself to break that rule.

Timeboxing helps a lot. In the past, I spent a lot of time checking, reading, and replying to emails. Nowadays, I check email maybe once a day, and I do not respond to emails unless strictly necessary. I do the same for social media apps, and if you use an iPhone, the Screen Time feature can help you a lot with this.

For non-digital distractions, you can also use the concept of timeboxing. For example, at work, I set up “focus time” in my calendar to make sure people know when I will be available to join a meeting and when I am not. Setting focus time in your calendar sets the expectations of other people around you. It will make it easier for them to know when you might be free to talk, have a meeting, or help with some other task.

I break my workday into four areas, the first block of time, 7-9 am is OK for meetings and open tasks, from 9-12, my time is blocked for deep/focused work, lunch from 12-1 pm, and finally from 1-4 pm I have open it up for meetings and time to reply to email and other office messaging apps.

I started doing this more than a year ago, and it works very well. I get very few if any distractions during my “focus time,” which is enough to make me productive and more relaxed. Before doing that, I checked email constantly, responded as soon as possible to any message, and accepted all meetings at any time. Unfortunately, that caused my productivity to drop considerably and, with it, my motivation and energy.

Context switching is tough for tasks that require concentration. You cannot concentrate with the endless amount of notifications coming from our devices, from people around us, and from our lacking ability to concentrate. We have to set clear boundaries with ourselves and be assertive and disciplined to make sure we and others around us respect them.

Setting limits and structures around my life is not something I often do. I prefer to live the moment, be casual with what I do in life, and allow myself to do unexpected things. However, when it comes to working, whether it is personal or professional, setting boundaries and allowing yourself to focus on tasks is a game-changer.

This advice is not only about being more productive. Focusing on tasks is also less stressful and much more enjoyable once you learn how to do it. I hope this is helpful for you, and please, if you have any other suggestions, questions, or any feedback, please let me know here in the comments. Cheers.

Categories
Coding Notes

Enabling software engineering teams for success.

Software development is hard, and it isn’t always the programming language or the framework you use, it’s the people who work on it.

People are an essential part of a team; everything can be easily changed and fixed, but you need to make sure people work well together to achieve effective communication and a great culture. In my software development career of over 20 years, the critical difference between successful software projects and failed ones has been how engineering teams are created and how they are allowed to function over time.

Categories
Coding

How to find great developers.

In response to those companies, hiring managers, interviewers who keep asking the same question:

Where can I find great developers?

A developer becomes a “great developer” when the company, team, resources, projects, recognition, etc., are compatible with that person. Under that logic, I believe any programmer can be great if they desire to do so and find the environment and motivation to thrive.

Most technical interviews fail to find the right people because interviewers and hiring managers usually go at it with an “idea” of what a “great developer” looks like to them. In most cases, everyone ends up hiring people who don’t work out and miss out on people who could have become the “great developers” there were looking for in the first place.

Categories
Coding

An introduction to Single Responsibility principle (SRP)

This is the fifth and last article describing SOLID principles. This article is about the Single Responsibility principle. Hopefully it will help you understand what the principle is all about and why it’s important to keep it in mind when designing and writing your code.

What is the Single Responsibility principle?

Here is the definition from Wikipedia: The term was introduced by Robert C. Martin in an article by the same name as part of his Principles of Object Oriented Design, made popular by his book Agile Software Development, Principles, Patterns, and Practices. Martin described it as being based on the principle of cohesion, as described by Tom DeMarco in his book Structured Analysis and Systems Specification.