Tag Archive : .NET

/ .NET

Around 10 months ago we hired a contractor to help us with some projects we needed to start. After 9 months his contracts expired but he couldn’t complete the project he was working on, so as the Lead developer in my team, I had to take his code and complete the project. I’ve spent 20 percent of my time completing the project and 80% optimising it. I’ll write several articles on this experience but for now lets’ concentrate on how to improve a C# code using Generics.

At the end of my optimisation with Generics, the code was 20% shorter and around 30% faster.

What are the Generics?

Generics allow you to use a placeholder instead of a type and you can use them with classes , fields, methods, parameters…

For a class a generic could be:

class Node<T>
    {
        private T value;

        public Node(T currentValue)
        {
            value = currentValue;
        }
    }

We have basically created a node that can contain every type we need. This node can contain an int, a string or a class. It doesn’t matter. It works.

We can also create a Generic method like this:

void DoSomeWork(T myParam) { … }

Also in this case we have a method that can accept every type of parameter (an int, a string, a class…).

How Generics can help us?

Going back to my story, what this contractor needed to do is to get the data from an excel file and evaluate that the value contained in each column is valid. To ba valid, a value must be contained in a list of values.

There are around 20 columns we have to evaluate and to do that he wrote this code (I copied only 3 methods but there were 20 of them):

private static bool ValidOverloadWarning(string value, List<OverloadWarning> listOfOverloadWarnings)
        {
            return listOfOverloadWarnings.Select(x => x.Name.ToLower()).Contains(value.ToLower());
        }

        private static bool ValidOverGeoidUnits(string value, List<GeoidUnits> listOfGeoidUnits)
        {
            return listOfGeoidUnits.Select(x => x.Name.ToLower()).Contains(value.ToLower());
        }

        private static bool ValidOverAltitudeUnits(string value, List<GeoidUnits> listOfAltitudeUnits)
        {
            return listOfAltitudeUnits.Select(x => x.Name.ToLower()).Contains(value.ToLower());
        }

The first think you can notice is that the code looks entirely the same, the only difference is the class contained in every list. Moreover I’ve noticed that all these classes have at least 2 fields: Id, Name.

So I thought: “this code can be optimised and this is the perfect situation to use Generics“.

So I started to create a base class with 2 fields: Id and Name.

public class BaseClass
{
    string Id { get; set; }
    string Name { get; set; }
}

After that I changed all the classes like OverloadWarning, GeoidUnits, GeoidUnits,… so that all could inherit from Base Class. For example the class OverloadWarning became:

public partial class OverloadWarning : GenericItem
{
    //...
    // Other properties
    //...
}

After that I was able to replace 20 almost identical methods with a single one, this:

private static T GetItemByName<T>(string value, List<T> list) where T : GenericItem
{
    return list.FirstOrDefault(x => x.Name.ToLower() == value.ToLower());
}

Just doing this I was able to replace 80 lines (4*20) with just 4. A shorter code means that your code is easier to read and maintain and it’s definitely easier to spot any problems or errors and find improvements.

In facts, I noticed that this contractor was doing 2 operations:

  1. check if the value was in the list
  2. if it was present, then he was retrieving this code from the list

It means that you have to search for that element twice and it takes time, so inside the method I wrote, I just replaced the 2 operations with a single one using the command FirstOrDefault that returns the object if it finds it otherwise it returns null. That’s exactly what I wanted to achieve. This is why my method GetItemByName returns a generic type T instead of a boolean value. Replacing two operations with one, helped me to save another 80 lines of code and it allowed me to halve the time to complete these operations

Conclusion

From this short story we have discovered how the Generics can help us. With the use of Generics I was able to replace 160 lines of code with just 4 lines and to replace 2 quite expensive operations with a single one.

Now my code is shorter, cleaner and more important for the client, it takes around 4 seconds instead of 7 to analyse a file.

What do you think about Generics? Do you have more questions or you need help with them? Write here a comment

Usually a desktop platform has a lot of available resources so many developers tend to avoid to improve the quality of their code.On a mobile platform the resources are quite limited and improving the code could make a huge difference. It can transform a slow and annoying app in a great fast and great one.Today, using the code I’ve written for one of my app Tv Series Time Manager, we are going to see how improving the code can make a huge difference in the loading time of your apps.

LINQ
LINQ

Improve your code – LINQ is your friend

Tv Series time manager allows you to keep track of your preferred tv series.It uses a public API to get information about all the TV Series created. As you can imagine there are thousands and thousands of Tv Series so we need to handle lists of thousands of items.Every time we launch the app, it searches online updated about the Tv Series you are following (new episodes, release dates, and so on). This can be a quite intensive task as the API we need to call returns a JSON file with more than 34000 items.The first time I’ve written the updated method I had this code:

var localTvShows = LocalData.GetAllTvShows(); 
var remoteUpdatedTime = await WsData.GetUpdatedShows(); 
foreach (var info in remoteUpdatedTime) 
{ 
    var show = localTvShows.FirstOrDefault(x => x.id == info.Key); 
    if (show != null && info.Value > show.updated) 
    { 
        updateRequired = true; 
        break; 
    } 
}

            if (UpdateRequired)
            {
                foreach (var info in remoteUpdatedTime)
                {
                    var show = localTvShows.FirstOrDefault(x => x.id == info.Key);
                    if (show != null && info.Value > show.updated)
                    {
                        var remoteserie = await WsData.GetShow(show.id);
                        SqlHelper.SyncSerie(show, remoteserie);
                        var episodeList = await WsData.GetEpisodes(show.id);
                        SqlHelper.SyncEpisodes(show.idLocal, episodeList);
                    }
                }
            }

I load the local Tv Show followed by the user and the remote Tv Series with the corresponding update time.Inside the first foreach I check if I need to update the local Tv Series.In case I have to update a Tv Series, I use a second foreach to update every local TV Series inside the local Sql database. (To add SQLite database to your app, you can read this tutorial)The code worked pretty well but I wasn’t satisfied by the loading time so I started to look for a way to improve the performances and here I discovered once more that LINQ is our friend.

The new code with LINQ

I decided to use LINQ to evaluate if I have to update a tv series, so I’ve written the code replacing the old one with this:

var seriesToUpdate = from remoteTime in remoteUpdatedTime
                join localShow in localTvShows 
                    on remoteTime.Key equals localShow.id
                where remoteTime.Key == localShow.id && remoteTime.Value > localShow.updated
                select localShow;

            if (seriesToUpdate.Any())
            {
                foreach (var show in seriesToUpdate)
                {
                    var remoteserie = await WsData.GetShow(show.id);
                    SqlHelper.SyncSerie(show, remoteserie);
                    var episodeList = await WsData.GetEpisodes(show.id);
                    SqlHelper.SyncEpisodes(show.idLocal, episodeList);
                }
            }

Speed Test

Now it’s time to see if the new code is really better than the first (Obviously it is 🙂 ). To do so, I’ve divided the code in two parts.

  • Evaluate if an update is required (Part A)
  • Update the Tv Series (Part B).

To give you the idea of how much time we can save, I’ve enclosed both the code in a for statement repeating the it for 100 times. These are the result:

Part A

  • OLD CODE:  Elapsed Time: 1077 ms
  • NEW CODE: Elapsed Time:   225 ms

Part B

  • OLD CODE:  Elapsed Time:  590 ms
  • NEW CODE: Elapsed Time:   122 ms

Total

  • OLD CODE:  Elapsed Time:  1667 ms
  • NEW CODE: Elapsed Time:    347 ms

Looking at the time spent by the old and new code we  went from 1667 ms to 347 ms, saving 1320 ms. The difference is HUGE.From the point of view of a user, saving almost 1.5 seconds makes a massive difference, maybe the difference between keeping your app or uninstalling it.

Conclusion

A mobile device has a limited amount of resources and users don’t like to spend time waiting in front of a loading screen. Even the most useful app, if not optimized will be uninstalled by the users. This is why it’s really important to optimize your code.Today we have seen that with a good use LINQ  we can massively reduce the time used by our code.If you like the article, comment and share it!

%d bloggers like this: