As a kid I grew up in a very multicultural household, with parents that remarried when I was around thirteen years old. Up until then I had been an only child to my parents and suddenly I was surrounded by three siblings from a wildly different ethnic background than my own. They were one of the best things to ever happen to me, but while the experience is something I’d love to share, I won’t be going deep into that in this blog. What I want to talk about is language.
For anybody that has grown up in/experienced an environment where the people around you are multilingual you have probably seen an occurrence of a linguistics term known as code-switching. Code switching is defined as:
“the practice of alternating between two or more languages or varieties of language in conversation.”
And while there are several reasons that people engage in code-switch in conversation, there are two that I personally draw from when writing code(yes, this article is about code I promise).
- People may use code-switching to hide fluency or memory problems in the second language
- Code-switching is used to mark switching from informal situations to formal situations
Code switching can be used to accommodate for the skill and flexibility of the speaker, and it can also be used as a tool to deliver the most appropriate setting for your needs.
Now imagine instead of speaking, we’re talking about coding.
Coding languages at the end of the day are really just tools; tools built by other people to make our lives as developers easier. Anybody that’s coded in Java, LISP (or God-forbid Malbolge) then switched over to Ruby/Python can attest that when iterating over an array, you probably wouldn’t pick any of the former. And each tool is going to have its ups, downs and specialties.
- Python is the leading language nowadays in terms of Data-Science and Analysis thanks to its massive open source libraries (Pandas, NumPy, SciPy, MatPlotLib etc.)
- Ruby is one of the most widely preferred startup company web development languages due to its magical Rails web app framework
- SQL is a widely used database query language heavily incorporated in many functions of higher level languages(including Ruby and Pandas)
- Java is nice, because its everywhere
A lot of these languages do similar things, some just do them better or maybe even easier. But we can utilize the same thought process behind code switching for our, well, code!
If you have a fluency or memory problem in a particular language that you haven’t used in years but know a one-line solution in another, wouldn’t it be nice to just get it easily and then tell your rusty code where to find it? Or say you’re working with legacy C++ code that looks like gibberish to you and your boss wants you to do a one-time transfer to it’s connected SQL database. Well nobody said you had to get the transfer done in C++, just do it in Ruby.
Lastly, maybe you’re trying to make a high-level database analysis webpage and it needs to be done fast. Well, wouldn’t it be nice if we could code our complicated data set systems for the backend in Python, interface it all to a SQL database, link it up to a Rails web application and still have some time to sleep?
The true power of coding languages are brought out when you are able to use them for what they’re best at(or for what you’re best at), and offload weaknesses to languages better suited for the job. Modularize files, and plan out exactly what a block needs put in, and exactly what you expect it to put out. When you learn how to make your languages talk to each other you open up a whole world(or playground) of possibilities for development. You also save yourself the trouble of learning how to full stack develop in a new language every time you switch jobs. You have become a polyglot programmer.
I’d like to end with a description of a small implementation I put together this week that inspired this article. At the time of writing this I’m attending Flatiron School’s Web Development Immersive Bootcamp and am currently four weeks in on the game.
A little over a week ago, I suffered a coder’s worst nightmare, a hand injury in my dominant hand. Oh the horror. I went from being able to write line after line of code as fast as I could think to having to type with one hand, and during project week to boot. I decided that learning how to avoid excess typing would be a good investment now and got into bash aliasing. Aliases are just terminal inputs that you can pre-define in either your .bash_profile or your .bashrc file in your user folder on Mac.
.bash_profile sets up a shell environment on login, .bashrc sets up an environment without the login requirement. The way OS X Terminal runs by default however makes this difference mean little for us.
But they don’t do everything. Aliases can only be used for one-line terminal commands. More complicated behaviors wouldn’t work. Additionally, you can’t pass arguments to an alias.
I wanted to write a one-line command that I could pass a github url that would clone the repo in a new folder at a specified directory, then automatically open up the directory in VSCode. This way I could type “clone paste-url-here” and it would make a new folder, clone the repo to the folder, and open it up for me all in one step. But:
- Aliases can’t take arguments
- I know next to nothing about bash scripting
- Functions accept arguments in bash, but I didn’t know how to pass a bash argument anywhere other than more bash script
That being said, I did know how to do all of these steps in Ruby. I could generate the url’s and directory addresses I needed line by line in a Ruby executable, and run everything step by step. So the last part of my function would just execute that file. But I wanted to pass in a new url in the command line every time that would change how the ruby code operated, and I didn’t know how to pass a bash function argument into Ruby.
So instead, I used a file type I knew both bash and ruby can communicate with easily. And, more importantly, one that I already knew how to work with. A simple .txt.
The Solution: Improvise!
I knew that in my bash function, I could accept the url and use echo to write it to a .txt file that both my function and my ruby code knew how to access.
$1 means the first argument a bash function accepts.
echo “” > filename will overwrite any text in that file and replace it with the supplied string
echo “” >> filename will append a supplied string to the end of a file, but we didn’t need that here
After saving my url, I could handle all of my logic in an easy-to-code ruby file and just execute the file.
In the ruby file, I pre-define my particular username and the directory I want my labs cloned into. Then I read the url freshly echo’d to my .txt and use these three variables to parse out and join the strings I need to run my system commands. The first one to clone the repo, the second to open the new file.
‘system’ commands in Ruby execute shell commands as though you typed them in the terminal. For more ways to execute shell commands in Ruby: https://redpanthers.co/different-ways-to-run-shell-commands-in-ruby/
I was so excited when I finally ran this and it worked for the first time. It was one of the first times I used two separate tools talking to each other without some built in library handling all the specifics for me. In fact, I was so excited that I shared this cool little thing I made with the rest of my cohort.
Then one of our lovely instructors(and I do mean that sincerely) informed us of a tool built into our learn suite that did something very similar to what I had made. And when I say similar, I mean it did what mine did and then did more and did it better. As quickly as my shortcut had come about it was swept away into the world of wheels that have been re-invented except my wheel was square because I only had a steel square to work with.
Regardless, this was a fun exercise and one that I personally take a lot of pride in despite its relative simplicity. Knowing how to cross-interface between the different tools at your disposal helps in productivity, creativity, and drives you towards pursuit of mastery.
It is a concept that shows practicality in linguistics, and in code, and in all approaches to problem solving. So I will continue to exercise it in practice, and I encourage anyone with a passion for building things to do so as well. Thank you, and good luck!
Polyglot programming -- development in multiple languages
Computerworld covers a range of technology topics, with a focus on these core areas of IT: Windows, Mobile…
What Is Code Switching?
Code switching (also code-switching, CS) is the practice of moving back and forth between two languages or between two…