Code-Switching: Switching Between Multiple Programming Languages

Image for post
Image for post
Can you spot what’s wrong with this Ruby code?

In the world of software developers, writing code in multiple programming languages is quite common. Most software developers are not only expected to be polyglot but be able to do so within the same day!

Learning a new language can be daunting but after some time learning the loops, the classes, the variable types, etc you get a hang for it. You settle into the new language and start to become comfortable with it. But now, what if you have to switch back and forth between programming languages? That sounds trickier… But don’t worry! Developers do this every day and it becomes easier with time!

Recently I’ve found myself writing code in two different languages at the same time. While working on my capstone project for the Flatiron School, I was using Ruby for my back-end and Javascript for my front-end. I find myself making silly mistakes. I use brackets in Ruby (like the photo above) when I shouldn’t and I use Ruby enumerables in Javascript where they do not exist. I’m starting to learn some Python too, and that's adding another language! I find myself code-switching a lot these days….

Code-Switching

What is code-switching? Code-switching is a term used by linguistics which means:

“the practice of alternating between two or more languages or varieties of language in conversation.”

It's used to describe what happens when people who speak multiple languages such as French, Italian, and Mandarin start to mix their words and phrases up. There are different types of code-switching, such as replacing a single word in another language to replacing an entire sentence.

Now it's not a term normally used for programming languages since code won’t run if you start using syntax from different languages together. This is what happened in the photo above where instead of using “do” and “end” in Ruby, I accidentally started using “{“ and “}” reminiscent from earlier in the day coding in JavaScript. However, in spoken languages, you can get away with saying “Wow this dress is tres chic”. But maybe there is something to learn from bilinguals who switch between human languages with ease. How do they do it?? Maybe there is something to learn to apply to switching programming languages

I speak a few different languages with varying degrees of fluency. I speak Bengali and English with native fluency, my French would be intermediate, though fading with each day of no practice, and some conversational German leftover from my summer abroad. I naturally can move between these languages without much thought. So what are the mechanisms that help me keep track of my spoken languages and can I rely on them to help me fluidly transition between programming languages as well?

Context

Context is the most important player here. It is very important to your ability to switch between languages, spoken or programming.

I speak Bengali with my family, I speak English with my husband and I speak French if back home in Montreal when I can’t seem to avoid it. The reason I don’t mix up what to say (normally) is all due to context. My brain knows what to say depending on the person in front of me without even thinking about it. Context helps it not get confused.

Context can also be used in code. In fact, context is the very reason there are multiple programming languages, to begin with! For example, I was using Ruby for my backend but Javascript for my front-end in the application I was building. Ruby, along with the Rails framework, is widely preferred by startup companies for web development. Python is the leading language for Data-Science and Analysis thanks to its massive open-source libraries (Pandas, NumPy, SciPy, MatPlotLib, etc.). JavaScript is used in almost every web page these days. Every language has its own strengths and weaknesses.

Happens Naturally

One thing linguists have noticed is that when it comes to speaking multiple languages it's not an on-off switch between them. You are in fact capable of using multiple languages at the same time. That's the whole basis of code-switching! So your brain is actually already wired to be able to handle multiple languages as it is constantly active and these languages are trying to interact with each other. You cannot stop is any more than you can turn your brain off.

So there really is nothing to worry about! You can switch between speaking formally and informally in the same language instantly and you know to not use swear words when a child enters the room. The same mechanisms can help you switch between functional programming or object-oriented programming languages.

Practice

I'm really not adding any value to the internet with this section. But ill say it anyway, you can only get better with PRACTICE. The more you actually practice switching between languages the better you will get. This is true for not only all types of languages but any skill really. I know, shocking! A good way to start is to just experiment in the languages. See if you can write the same program or function in the two languages. Then compare them and see how they differed as well. Just keep coding, just keep coding, just keep coding coding coding…

Costs of Switching Frequently

Now as beneficial it may be to move through languages in a day, there are a few things to note:

  • You will be slower- it's ok, you won’t necessarily be slow, but you will be slower than if you focused on one programming language at a time. This occurs in spoken languages too! People who speak multiple languages, are slower to go from concept to words than monolinguals. They simply have more information to sift through.
  • You may find yourself thinking in the context of the other language- As anyone who has used google translate to quickly finish an essay for Spanish class will let you know, literal translations are not always best. Remember to not just switch languages but switch your way of thinking too. As mentioned above, different languages have different strengths, and while switching back and forth be sure to keep that in mind.
  • As with spoken languages, it is not a good idea to try to switch before you are completely comfortable in the new language- switching while still learning will slow down the learning process. Make sure to gain some comfort in both languages independently before allowing yourself to work in both concurrently. Remember, the best way to learn a language is by being fully immersive in it!
  • If coming back to a language after a long gap, you may not be up to date on the language- programming languages change rapidly so always take some time to brush up if you haven’t coded in the language in a while. This would be the equivalent of using slang from the 90s, so not hot!

There you see, switching between languages isn’t that bad! In the end, you will become a more desirable software engineer with your ability to be so flexible. With the mastery of coding in multiple languages concurrently, you have now become a true polyglot programmer. Bonjour World!

References:

https://www.fluentlanguage.co.uk/blog/switching-between-languages

https://simpleprogrammer.com/switching-programming-languages/#:~:text=Languages%20that%20come%20from%20the,be%20no%20trouble%20for%20you.

https://medium.com/analytics-vidhya/code-switching-the-magic-of-modularization-and-communication-in-programming-2e8a927c38c4

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store