Categories
Hard Skills

JavaScript VS C# ( Google distance API )

To use Google’s DistanceMatrix API and do some calculations with JavaScript is easy. Try the same with C#!

Nice, quick, and dirty. Start with JavaScript

I was (and I am still) playing with a code that can do some math based on a distance between 2 locations.

Because Google supports JavaScript it is really easy to do. Also to get a JSON file from my back-end is easy to do again so everything was perfect to write a solution in less than a day.

I was really proud to myself that I got a library that I can shape to my needs. Quick and dirty 170 lines of JS code.

Now, why is it dirty? Because the result of this calculation is part of a booking process so it will be part of a package’s price. To clarify myself, this project started only for internal/informational use, not part of a public booking process.

The problem is with the new requirement is if I get a quote on the front-end and send the price to the server calculated with JS, nothing stops a smarter user to alter this price. Not good at all. Do you want a free package or a huge discount? With JS hack it is not a problem at all.

I have to rewrite everything in C#

So what? It is easy, isn’t it? I got everything I need, all the math and things. It is only 170 lines in JS and one-day coding. Could not be that hard with C# either. Wrong!

At first, I could not find support for C# from Google. It is OK, it can handle https requests with a nice formatted url and I can construct them easily. Great!

But I had to realize it would take a lot of time to write all the models to mimic Google’s response, services to construct the proper request URL and handle the communication. No problem, let’s see what can I find on GitHub. It is great to work with open source. I like it. Nothing is better than getting help by helping.

So I searched for “google distance API c#”.  Perfect! I picked 3 of them and started to look at the code. One of them is outdated. The other had no tests but at least it is active. The third one was better constructed, had tests, and still active in a way.

Let’s try them out.

I started the one (and finished) with the tests.

Here is the URL if you are interested: https://github.com/ericnewton76/gmaps-api-net or check it in my fork https://github.com/bbotond90/gmaps-api-net/tree/distanceMatrix.

Fork, Download, Build! The unit test passed, works. Great again. But not that quickly. The model is not finished, missing some properties. Let’s fix it, test it. Few hours passed. Ok, but how can I attach an APP key for authentication? Found it but an hour passed again. I think you got the idea, I spent more time just on GitHub and with this project to be able to communicate with Google than with JS with a full solution.

Ok, so I can make requests, get data. Let’s shape it in a library that is reusable in my project. To cut it short, I had to write tons of lines of codes. The response was in the raw text in the model, but I needed integers, doubles, enums, etc and I had to transform those data into my project’s needs. I spent another day on it and in total with tests I wrote nearly 1.5k lines of codes and not nearly finished, just got the models and the communication with Google right, built into a nice library to reuse.

I still have to write data validation, have to communicate with front-end, write JS to get a result from this C# library, etc.

To finish

JS 170 finished (works).

C# 1.5k halfway to finish (to make it work and nice).

Wow, that is a huge difference. What I have learned is how much is easier to solve problems with JavaScript than C# if you got something like this. Ok, I have to admit the C# version is well-formatted and tested. But still. Use the right tools for the job! But what if you do not have all the tools you need? 🙂

Unfortunately, I cannot share code from these projects, but probably I will write some posts about the bits that you need to create something similar.

Let me know what you think!

Oh, to not forget to mention, if you are interested in participating to support Google API’s in C#, I found this library the best to start with.

Here is the URL again:

My branch:  https://github.com/bbotond90/gmaps-api-net/tree/distanceMatrix

Original master: https://github.com/ericnewton76/gmaps-api-net

Other resources:

By Botond Bertalan

I love programming and architecting code that solves real business problems and gives value for the end-user.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.