Does it happen to you too? How do you ensure you're able to switch quickly and effectively between programming languages while coding?
Any tips or tricks are appreciated.
For me it's definitely the CLI, package manager, and other devops around using the language that makes the context-switching tougher. npm vs gem vs pip and the fact that they all kinda work differently: npm will install libs into a "node_modules" folder in every project (so you could have 45 copies of React) where other package managers might install 1 copy system wide and your project just references it, so it affects how you deal with version mismatches. nvm vs rbenv vs venv/pyenv - slight nuances there too.
Case in point try using Angular as a React dev. You're still in JavaScript, but the CLI part is different and more involved, the @ decorator syntax and other paradigms look/feel nothing like the React ecosystem.
Getting into compiled languages there's usually a heavy IDE component you need to use and be good at (thinking of iOS/Xcode, or whatever C/C++ devs are using).
But once I'm to the point of writing in a code file and I can easily run it and see my changes, I'm good at that point.
Tips: LLMs are great translators. An LLM sidekick to accompany work outside your normal language or framework is extremely useful - can't recommend it enough. Not only will you get bespoke code examples but it will explain the "why" behind certain things that are foreign to you.
And of course, just spend a lot of time coding in the language you want to make less mistakes in.
My concrete example would be Ruby and Elixir. There are fundamental differences in how you design your solution because the languages are that different. It really messes with your brain for a while until you can context switch.
A tip if its causing issues larger than typo's or syntax is to time-box your work in the different languages, only focusing on one at a time. Like mornings doing X and afternoons doing Y. Give your brain a break in between.
Yes. I switch between Java and JavaScript/TypeScript frequently. Probably the biggest syntax typo I make is switching the arrow functions: -> vs =>. Also, switching between the various `.includes`, `.contains`, `.size`, and `.length`.
Since my IDE catches it immediately, it doesn't really have any long-term affects.
If you want to get better at switching, practice more in both languages, separately.
Most of all, don't worry about making simple mistakes. If there's one thing that compilers, linters, test suites, and judiciously placed printf()/console.log() statements have taught me, is that I'm terrible at getting code exactly right the very first time. But that's OK, because if I've got the "shape" of the code mostly right (which I'm somewhat better at) then a couple of iterations on syntax, the order of expressions, and the fine details of those expressions, take very little time. Whether those fixups are needed due to language switching, or just general human error-propensity.
Is your checked-in code quality, and your end-of-the-day productivity, on a par with that of the work colleagues you respect? If so, how you get there is working for you.
If you want to get better at your craft, that's good. And spotting specific areas you'd like to improve at is helpful for that. But that doesn't mean you should beat yourself up for not being there yet.
I used to have similar issues with it, but now it's a relatively seamless compartment/context change. I sometimes forget super basic function names or something and need to Google/ChatGPT it. Stupid stuff like len() for Python after working in JS all morning.
I've got various cheatsheets I like to refer to.
Like others have said, keep at it and you’ll improve memory/recall over time. But if you don’t, that’s fine too. The ability to write code really fast with perfect recall of syntax is overrated IMO.
Also, I appreciate the ability to reuse code between the front end and back end. It's not a silver bullet and shouldn't be overused (not a fan of Server Side Rendering) but it's great for utility functions. It's a huge advantage to know that certain functions behave exactly the same in all environments.
Writing beautiful idiomatic code in multiple languages is a big challenge, but writing syntactically correct code should just come naturally after a while.
I am a huge protobuf fanboy, and accept it has flaws, but it has saved me so much time and pain over the years it is amazing.
[1] https://luduxia.com/showdown/ Seriously. Obviously the front end is JS, built (custom esbuild) and served with golang, with a Java backend that drives foundationdb.
So, it's just a "lack of practice" which doesn't mean you're a freshman at coding.
Our brain works in "rooms" or in "departments". Have you ever make the experience of wanting to do something and therefore go to another room. Arriving there, you've forgotten what you actually wanted? That's experienced by, I would assume, all of us. You just won't remember, what you wanted to do from one second to other .. BUT, if you go back to the room you had the initial thought in and in which you walked out from, then, suddenly it strike's you and you suddenly remember what you wanted to do. Just because our brain is working "in rooms". In each room we have our concepts (compare TV vs kitchen).
So obviously you have "learned two languages in the same brain room". What you now need is to separate this both rooms again. It just can be done by explicitly provoking that situation. For example, take a python code and translate it purely to JS while live coding. Or code different languages in different workspaces or even locations. You need to introduce a brain room separator somehow. Then it's just a thing of brain & thinking in rooms.
The same problem is faced by language translators, people speaking more than one language. All of them exercise and train to use different 'rooms' while learning to translate. Another example is the live translation of spoken to sign language. You have to explicitly train how to move your hands by hearing certain words. And even more, you have to explicitly train how to hear, think and make movements of hands, while/with the information coming in linearly/steadily with future context one might need to understand coming, well, in the future.
So it's just practice. You need to provoke such situations so your brain get used to fast context switching.
(My experience with that, although not coding:
I speak Russian as mother language. German is my second mother language. English is okay, I think. Learned 3 years French language. Spoke a little French on daily basis. Then started to learn Chinese. One French guy asked me if i can understand what he's talking about, I said yes of course and gave him instructions how to go to the place he wanted - in Chinese. I didn't even notice it. That guy didn't understand a word, while I'm being sure to communicate with that guy the same language.
Later, i went to China. Spoke fluently Chinese. But I needed like 10 minutes to switch from Chinese to English, when I wanted to talk to foreigners in English. Words just didn't come out. After some switching time of like 10 minutes, English has been coming out fluently, more or less. I decided not to speak it anymore that time.
After that, my girlfriend wanted to exercise English. So we had to speak Chinese mixed with English. Mix up the grammer. Mix up sentence by sentence. Which, actually needed fast switching capabilities that she nor I did have. After 1-2 months of daily talking on the phone - my and her brain got used to it. I noticed, I just could switch and use both languages mixed simultaneously. Even the grammar of one with the vocabulary of the other was possible without a problem. I tried to use other language combinations and it was possible too.. now I can switch my 4+1 natural languages JIT)
So there's not much you can do, except provoking such situations, doing translation and ... Actually ... It's training.