Tricks and Tips: Make your code better (Intro)!
This is my first post about tips and tricks. For this initial post, I’ve selected some simple and humorous tricks, which happen to be my ‘favorite’ types of code commonly seen from beginner or exhausted developers. I often find myself commenting on these during the code review process. One common situation is when you need to add a method to check if some data is valid. Here’s how it typically looks:
private static bool IsValid(string name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}
else
{
return true;
}
}
OR
private static bool IsValid(string name)
{
return !string.IsNullOrEmpty(name) ? true : false;
}
Please DON’T do that. Just make your code as simple as possible and always think about the readability of your code. So, how it should be:
private static bool IsValid(string name)
{
return !string.IsNullOrEmpty(name);
}
In this implementation, this method will be clean and understandable for everyone.