This part of the C# idioms series is only about dictionaries. Let’s get rid of exceptions when working with dictionaries.
Instead of checking if a dictionary contains an item before adding it, use TryAdd
TryAdd() will return if an item was added or not to the dictionary. Unlike Add(), if the given key is already in the dictionary, TryAdd() won’t throw any exception. It will simply do nothing. The item is already there.
Before, if we added an item that already exists on the dictionary, we got an ArgumentException.
varmyDictionary=newDictionary<string,string>();myDictionary.Add("foo","bar");myDictionary.Add("foo","baz");// ^^^// System.ArgumentException:// An item with the same key has already been added. Key: foo
After, we checked first if the dictionary contains the item.
Do you imagine a big book when you hear 'dictionary'? Photo by Edho Pratama on Unsplash
Avoid KeyNotFoundException with TryGetValue or GetValueOrDefault
At least now, the KeyNotFoundException message contains the name of the not-found key. The old days chasing the not-found key are over.
On one hand, TryGetValue() uses an output parameter with the found value. It outputs a default value when the dictionary doesn’t contain the item. TryGetValue() dates back to the days without tuples.
On another hand, GetValueOrDefault() returns a default value or one you provide if the key wasn’t found.
Before, if we tried to retrieve a key that didn’t exist on a dictionary, we got a KeyNotFoundException.
vardict=newDictionary<string,string>();dict["foo"];// ^^^// System.Collections.Generic.KeyNotFoundException:// The given key 'foo' was not present in the dictionary.
Voilà! That’s how to get rid of exception when working with dictionaries. Use TryAdd() and GetValueOrDefault(). Or, if you prefer output parameters, TryGetValue().
In this part of the C# idioms series, we have one idiom to organize versions of commands, events or view models. And another idiom, on coditionals inside switch statements.
Separate versions of commands and events using namespaces and static classes
Sometimes you need to support versions of your objects to add new properties or remove old ones. Think of, commands and queries when working with Command Query Responsibility Segregation (CQRS), or request and response view models in your API projects.
One alternative to organize classes by version is to encode the version number in the class name itself. For example, DoSomethingCommandV2.
For better organization, separate your commands and queries inside a namespace named with the version number.
But, someone could use one version instead of the other by mistake. Imagine someone writing the class name and using Ctrl + . in Visual Studio to resolve the using statement blindly.
Another option to group classes by vesion is to wrap your commands and queries inside an static, partial class named after the version number.
When using static classes to separate classes by version, you will use the version number up front. Something like, V2.DoSomethingCommand. This time, it’s obvious which version is used.
But, if you use a partial classes and you keep your commands and events in different projects, you will end up with a name conflict. There will be two V2 classes in different projects. Then you would need to use an extern alias to differentiate between the two.
Finally, you can take the best of both worlds, namespaces and wrapper static classes.
These days, while watching YouTube, I found a Mexican YouTuber explaining what life was like in his city during the 2020 COVID-19 pandemic. I thought it was a good idea to write something similar but for coding. This is an opinionated view of what coding is like in 2020. Software developers from the future, this is programming in 2020.
GitHub has archived all public repositories until July 2020. It was part of his archive initiative. We earned a batch on our GitHub profiles if any of our repositories got into the Arctic vault. I got mine too. It will show future generations how code was in 2020. Should we be ashamed or proud? I don’t know. But this archive doesn’t show some of the practices around it.
Dear developers from the future, this is coding and interviewing in 2020.
1. On coding
Visual Studio 2019 is the latest version of Visual Studio. C# 8 is the latest C# version. And we don’t have SQL Server Management Studio for Mac or Linux yet.
Visual Studio Code is the most popular IDE. This is a different one.
Windows is still the most used operating system among developers.
JavaScript is the most popular programming language. The market is divided between React, Angular, and Vue. Although, every once in a while, a new front-end framework appears. Or a new version appears, changing almost everything from all previous versions. We even have a term for that: JavaScript fatigue.
Single-page applications are the norm now. Especially when you build them with one of the trending frameworks. Or with a library built on top of one of them.
Everyone is doing microservices these days. Monoliths are the evildoers.
Machine Learning and Artificial Intelligence are the next big things.
Everybody, when stocked, uses StackOverflow. It’s a place to post questions and receive answers. A forum.
Null is still a problem in mainstream languages.
When things break, we say “It works on my machine”. Then, we came up with containers. So we can ship developer’s machines to the clients or the cloud.
Most developers upload and share their code on GitHub. Oh yes! Git is the most popular version control system. There are also GitLab and BitBucket.
Every day, we have a (hopefully) short meeting. A “daily meeting.” It’s part of a ceremony called SCRUM methodology. Everyone calls himself “Agile” to hide the fact companies don’t know what they’re doing.
I’m writing this from a laptop with a 1.8GHz 4-Core processor, 16GB of memory, 500GB of hard drive, and a 6-hour battery life.
2. On interviewing
Interviewing is broken. Everybody with a blog complains about the interview process. Even on Twitter. Oh, Twitter. The place to complain in less than 280 characters.
We solve or attempt to solve algorithms and data structures exercises on whiteboards. Although, a study reveals whiteboarding only tests the candidate’s ability to deal with stress. Most of the time, we don’t use those subjects after the interview process.
Interviewing is based on rejection. Only a small percentage of applicants are hired. A story tells some managers at a big company rejected all applications they were asked to review. Later, the secret was revealed. They reviewed their own applications.
Ninjas, superheroes, wizards, 10x engineers…Ping-pong tables, open spaces, cool offices, being “agile” and [put the latest next big thing here] are often listed on job descriptions as perks and benefits.
During the 2020 global pandemic, some companies turned remote. We started to use Zoom, a conference room tool. Most people started working from home without any previous experience working remotely. _“Please, turn off your microphone.” “You’re muted.” We all heard these phrases in meetings every once in a while.
I hope the 2020 pandemic is still on Wikipedia or whatever you have these days to look things up…or are brains already connected to the Internet, like in the Matrix movie? Do you watch Matrix in class? Wait! Do you still have schools?
Once upon a time, a junior engineer at his first job
Everything started with my first professional job.
I was a junior software engineer, the least experienced on the team. I had just finished reading The Clean Code. And I wanted to rewrite all the code I had worked with.
I had a lot to share with my colleagues about coding. But I was the new guy. So I came up with “Daily Tips.” It was a weekly email with a single tip about writing better code.
Those tips came from what I had seen in the book and in the code I worked with. For example, use boolean variables instead of integers for flags. I started to accumulate some of these tips on my personal computer.
A few years later, those tips ended up in my presentations for newcomers at my next job.
The first post
The real starting point was a few years later in my second job.
After getting tired of writing log statements to chase down bugs, I went to the Internet to see what was out there. There must be a better way!–I thought.
I found Fody, a solution using Aspect-Oriented Programming. I put up a proof of concept and showed it to my team lead. Unfortunately, we ended up doing something else. But I had my findings. And I didn’t want to lose that time and that how-to behind an unread email. My blog and its first post were born.
Next posts
After that, from time to time, I started to share my learning and my experiences.
I started to write about the bugs that literally gave me headaches, the resources I used to learn languages and frameworks, and the notes from the books I read. And here you are, 30 posts after that first post.
Your turn
You don’t need to wait to be a well-known figure in the tech field to have a blog.
A blog is a means to share your learning. To learn in public. To share your insights. To show your work. That will make your blog unique.
I don’t have anything to write about?–you said. Have you learned something new? Share that. Share the resources you used to learn it.
Probably, next time you’re Googling something, you will find your own blog posts.
Scott Young describes in Ultralearning the strategy behind his own learning challenges, like “MIT Challenge in 1 year” and “A year without English.” Let’s learn what Ultralearning is all about. These are my takeaways.
Ultralearning is a self-directed and intense strategy to learn any subject. Ultralearning projects help to advance careers or excel at a particular subject. Ultralearning is a perfect alternative to traditional learning methods.
1. Before starting
Before starting an ultralearning project, answer why, what, and how you are going to ultralearn.
Why?
First, identify why you’re learning a subject and the effect you want to achieve. Are you learning the subject to get a specific result? Are you driven only by curiosity?
For example, are you learning to code to get a promotion? Or do you want to learn a new language to go on a trip? Those are two different motivations.
What?
Next, determine the concepts, facts, and procedures you need to learn.
A concept is something you need to understand instead of memorizing
A fact is something you need to memorize. Facts are useful only if you can recall them later
A procedure is something you need to practice
For example, learning vocabulary and expressions are facts when learning a foreign language. But pronunciation is procedural.
How?
After answering Why and What, select your resources. Spend about 10% of your learning time doing research. Use this research time to find how people are learning that subject.
Look for syllabi of courses, textbooks, boot camps, and experts in that field. Filter what won’t help you to achieve your goal.
Learning should be in the context where those skills will be applied. It’s doing the thing you want to get good at where most of the learning happens. For example, solve problem sets instead of watching lectures and learn a language through conversations instead of vocabulary lists.
Try project-based learning and immerse learning. For example, learn how to create a website in a month or go on a trip to learn a new language.
Prefer short study sessions
Spread shorter study sessions over a long period. Find a balance between long study sessions on a single topic and shorter sessions on different subjects. It’s better to have shorter sessions, between 15 minutes and an hour.
If you find yourself procrastinating, follow the 5-minute rule: start and sustain for 5 minutes. Also, you can try the Pomodoro technique: spread 25-minute practice sessions between 5-minute breaks.
Identify the bottleneck components in your learning. Separate your skill into sub-skills. And practice each sub-skill. Imagine a musician who practices tricky parts of a piece in isolation and then practices everything.
Recall instead of concept mapping
Recalling is better than concept maps and passive note reviewing. Recall concepts and facts. Your memory is a leaky bucket. Try space-repetition software or flashcards. After watching a lecture, write all you can remember. When practicing, avoid using your resources.
Voilà! Those are my takeaways from the Ultralearning book. It changed how I approach learning. Instead of overloading my brain with information, I start by creating a plan and list of learning resources.