The day I learned about global variables — The hard way.

It was the summer of 2014, and I had just launched my first e-commerce site. It was a Ruby on Rails application, and it took me about 12 months to write the code as a side project. All lights were green and I was already serving customers. I was in charge and in control.

A couple of months before launch, I had arranged a deal with a few schools in Norway, stating that the students could buy stationary online with a discount from my newly launched master piece. As part of this deal, I provided each school with a unique coupon code for the students to use. When the students provided the code, I could tie each completed order to their respective school and bulk ship the items with free shipping. This was a good deal and an ideal situation for my Rails app to shine.

When you first unleash real customers on a platform, you try to follow their every move throughout the checkout process. I remember watching every click hoping to see that famous 'Thank you'-page showing up in the log. It's a great feeling to see the system work, especially when you know every conditional statement (if and else) the customer is firing along the funnel.

Seeing all the pieces come together and serving a purpose is both rewarding and scary. You are vulnerable and anything can happen.

As more customers were passing through the checkout process, the system gained trust, and I could take a step back from watching the logs. Unknown to me, there was a cloud on the horizon, a dark one. It would later seem to be related to a term defined as 'scoping' in the world of programming.

My phone rang, and I answered the unidentified caller ID.

“Hello, is this the school supply store?”

Knowing that this customer must had found my phone number in the phone book as the company was not listed yet, I immediately knew the call had a sense of urgency. The customer explained that she had just completed a purchase from my store, and after checking the email confirmation she noticed that both the price and coupon-code were different from what she had written during the final step of the checkout-process. The coupon code belonged to someone from a school located at the other side of the country. How on earth could this happen. More customers were showing up and my head was on fire. It took me probably half an hour to figure out what had happened.

The coupon-state of my application was a global variable. It was global across the application from the time a customer clicked 'Go to checkout' until he or she completed the purchase. A workflow that normally took 120 seconds. This was a huge design flaw, and in practice meant that only one customer could check out at the same time. As with many technical situation, this is best described by an illustration.

There are many ways to solve the issue that appeared that day, and for most programmers it's a no brainer. Every client should have their own little exclusive virtual checkout-booth, preferably living inside their computer (more specifically, the web browser). This is often solved by a what's called a cookie. Most importantly, the customer should be given days to decide if to buy the products, and should not have to care about other customers waiting in line. Or waiting in line to change the global coupon variable, as it used to be called in my application.

This issue was handled swiftly after a quick search at Stack Overflow, like most issues have during my time as a Ruby on Rails developer. By now, the store has served thousands of customers. We are still providing high quality accessories for students all over Norway — And we'd like to continue doing just that.

If you liked this story — feel free to leave a comment , or visit the store at