Ask HN: How do you switch between programming languages?

47 points by BrandoElFollito ↗ HN
I am an amateur developer and bounce back and forth between Go and Typescript. Since I code in my free time, I usually do a week of Go, then maybe a few days of Python, and then back to Typescript.

I find it difficult to switch between the grammar of the languages (especially ones that are rather close such as Go and TS) and spend a day pondering over bugs which are an extra colon, or a dot in the wrong line.

Do you have any tricks/hacks to make the switch easier?

---

Note: by "close" I mean "visually close": "func" vs "function", stacked chained methods that either have their dot at the end of the previous line, or at the beginning on the next, single vs double quotes, types prefixed by a colon or not, ...

67 comments

[ 2.7 ms ] story [ 124 ms ] thread
There are plugins for vim/emacs/vscode to aggressively lint/highlight various errors as you type and/or save.

Since two of the languages you've mentioned are compiled, they should be giving you specific line/column/error information on syntax errors. If it's taking you a day to de-cipher explicit error reports from the compiler, it's probably time to better familiarize yourself with compiler lingo (most people recommend "the dragon book").

Considering he’s an amateur programmer I doubt he wants to delve into compilers, which is a hard topic even for CS students.

With that being said it is a great thing to know, but likely unnecessary unless you’re very serious about programming.

Didn't tell him to write one from scratch. Just suggested boning-up on the lingo: grammars, tokens, lexing, parsing, etc. An understanding of the first 2-3 chapters would be more than sufficient.

Getting a firm grasp on such things made every language I deal with "click". Knowing how the sausage is made--if only at the kindergarten field-trip level--brings all of the trade-offs language designers have to make into relief. With this knowledge, you'll know where the sharp corners are to begin with--the ones that were unavoidable due to the limitations of the medium. This is much better than beating one's head against the wall over a syntax error while being pissed at the language for not being a miracle.

I agree that this is a great thing for any serious programmer to do. It depends on the person though. Someone that codes for fun would not want to spend any amount of time in a compiler text.
I program professionally in C, Python and Javascript.

Instead of starting with a empty file, I copy an existing file in the language that I'm using. Simply by seeing examples of syntactically correct code, I assimilate. It's only a 90% solution. For example using single quotes on strings in C and putting a ';' at the end of lines tends to creep into my Python code.

Why not use double quotes for strings in all languages? It makes sense for a number of reasons. Single quotes are much more likely to be in the contents of a string than double quotes.

Of course, if you have a style guide, you should stick to that.

In some languages double quotes have different meaning to single quotes, for example in shell variables expand inside double quotes but not inside single. So you need to know which your current language uses.
Editor support can really help with transitioning. I write C++, Matlab, python, and bash all with regilarity, and eventually your brain separates out the sytanx for each one.
This sums up nicely exactly what I was going to say. A solid editor helps me transition if I've been thinking in a different language.
I code in java, golang and python, with occasional forays into terraform, SQL, JS, C and other bits and pieces.

For my main three, these days I use the jetbrains IDEs, and they are helpful enough that I tend to switch mode fairly quickly with their suggestions.

Back when I used to use gedit with a shell open underneath for basically everything, it was a bit harder. I was mostly coding in C and C++ then, and python felt really different and was harder to flip into.

That said, the more frequently you do these sorts of context switches, the better you're likely to get at them.

While I switch relatively effortlessly between programming languages, the problem does indeed exist. I try not to switch between languages in the same day... or at least, not too many times... OK, who am I kidding, at least not every half an hour.
I think this is something you naturally get better at. You start to resize that languages have similar ideas, but different representations.

Is this a problem that is having a significant impact, or just an annoyance?

If you aren’t using an IDE, then you definitely should start. Any good IDE for the languages you mentioned will provide lots of help with incorrect syntax. Be sure you have it properly configured so that it can give you as much help as possible.

I program in many different languages and I always have to search for things when switching to one I haven’t used recently. I view this not as a bad thing. Google exists for this very purpose, and there isn’t too much benefit to intentional memorization.

Lastly, the book The Programmers Brain touches on how to remember syntax among other things. It might be useful.

Two things I forgot to add. Most programming languages have ‘linters’. Linters analyze syntactically valid code for mistakes of different types. Mistakes can be something like you forgot to use a variable you created, to you’re performing some operation incorrectly.

Linters are useful in general, but in your case they can help you write better code when you aren’t as familiar with the language. Go has a project called golangci-lint, while TypeScript has eslint.

The second thing is code formatters. Every language has its own formatting style, so it can be nice to have a program that does the formatting for you. Go has gofmt, and TS has Prettier (among others).

These tools add some complexity, but if you set these up you’ll have some extra guard rails as long as you remember to run them, or even better run them as a GitHub Action.

For OP, I switch between Java, Ruby, python, bash, read scala, rust, Haskell, go…

And the advice here in this comment chain is spot on:

1. Know the underlying concepts, all programming languages are “same” after awhile (ex: object.prop, function calls, global vs non-globals, lambdas/anon funcs.

2. Use a compiler and or linter to catch syntax errors and don’t worry about memorizing it - knowing how to read the output/error msg and solving the issue is more important

Same here, different set of 8 or so languages I cycle through. I stopped caring. From time to time I'll make a silly mistake like using "def" in rust instead of "fn". The highlighter or the compiler will shout at me, I'll fix it and move on.
I've gotten used to duckduckgo for even basic things for the first few hours after picking up a language I didn't use for a while. Here are some examples of things I searched for: "rust join two iterables", "rust impl sum", "rust tokio select macro", "rust impl bitand for bool", "rust convert pathbuf to path", "rust join paths", "rust check if file exists", "rust tuple windows", "rust rand gen_range", "rust generate n random numbers", "rust system exit".

Then there are the small differences where trial-and-errors + immediate IDE feedback give me the answer. For example, is it 'len(foo)' or 'foo.len()?' Is it 'int foo' or 'foo: int'? Is it 'let foo = ...' or just 'foo = ...'?

The more you code, the better you get at 1) adapting faster, 2) accepting that you need to "land". "Landing" can take hours or days, if you measure by how simple things you have to search for.

Your two concrete examples are "dangerous syntax"; another one (although not language-specific) is forgetting to close a parenthesis: They're the kind of errors that give poor error messages in many languages, and so take unproportionally long to debug considering how banal the mistake is. I think that over time, you learn to be more careful in the places where you've wasted the most time.

Thanks to having too many jobs/responsibilities, I currently am switching multiple times daily between Rust, Typescript, Dart, Python, and sometimes, god forbid, C/C++. Usually at a moments notice, because a lot of times I'm writing FFI between those languages so it's multiple at the same time.

I just fuck up in small ways, a lot, and accept that I'm gonna do so thanks to context switching. func/function/fn, var/let/auto/wait I don't even have inference in this language, etc. It just happens, and there's some idiom bleed too.

Luckily IDEs are usually real fast to squiggly red line things now and boot me back into whatever the current context is going to be, and for the code I get reviews on, idiom bleed can be called out.

I refer to my own cheat sheets from time to time.

For example, iteration is so different in TypeScript and Python and I sometimes must remind me about brackets in TS/JS. So I finally resort to a cheat sheet.

Highly recommended!

It may seem silly, but I try to use different IDEs, editors or at least different colour schemes for different languages. Currently that generally means XCode for C++, VSCode for javascript (current main two) & Sublime Text for everything else. Helps me mentally context switch just that little bit quicker, though luckily the syntax isn’t all that “close” as you put it.
Use the most aggressive linter and warning settings, and an editor or IDE which shows warning/error squiggles while typing.
I always have to lookup the elvis or null save operators and end up cursing the java version optional.ofNullable().orElse()

?? ?: ?. .? &. || and more ascii salad

Also the semicolon or access of ivars with arrow, this, self or directly is a gradient transition every time.

> I am an amateur developer

Nowadays I mostly work with Java, Node.js (JavaScript/TypeScript) and Rust. I have been programming professionally for about 15 years, and I have been paid for work in C++, C#, Python, PHP in the past, not to mention a gazillion languages that I used on my side projects.

> I find it difficult to switch between the grammar of the languages

I have similar issues that you describe (wasting a few minutes, in my case, because of some syntax error) if I start off from an empty file or if a few days have passed from the last time that I used a certain tech. If I am continuing work on an on-going project I first do smaller tasks in existing codebase to get "into it", If I am starting a new project I sometimes copy an old one to get started and I edit it (delete most of the code, modify the config etc.)

Also, a lot is gained if you make yourself aware that you just switched to a different tech.

Obviously (is it?) I use an IDE and that helps _a lot_.

A very long time ago when editors were unhelpful I was able to give the background a subtle colour depending on the file type. It put me in the right frame of mind without thinking about it. Not sure what modern editors would support that.
Give it some time. I don't think there is any trick here beside experience.

Go and TS(js\java\c++ etc) may look similar now but they won't after some time.

a) keep doing it b) try to do work in different languages in blocks, and don't switch in between all the time c) keep doing it d) nobody wants to hear that, but .. keep doing it.

I program in more languages that I care to enumerate and some are just so in-grained in me that I don't make mistakes with 'close' naming. I'd be more efficient in a single language if I only programmed in that, true, but I think I'm more efficient overall because I bring a wide variety of solution sketches with me that can be applied depending on scope and primary, secondary, tertiary (and more) concerns. Once you have the first half-dozen to dozen languages under your belt, ideally with different paradigma (OO, FP, logic, old-school structural/procedural, bare-metal, VM/script) the next dozen dozen will be a piece of cake and your biggest problem is the warm-up of the language keyword and stdlib name cache. Hence b), above.

1. Install VS code

2. install language runtime support extension for first language

3. Install language runtime support extension for second language

4. RFTM #1

5. RTFM #2

:set syntax=language
I move between 4 different languages depending on what I'm working on.

I would only say I'm an advanced-to-expert user of one of them. The others I'm just an intermediate user.

When switching languages there's always a period of about 10 minutes where I'm adjusting to the syntax. Then there's a period of up to a few hours to get my head back into the correct space. For example, moving to a functional language like Erlang requires me a bit longer to get comfortable again.

I've pretty much always got the docs open when I move over to my less comfortable languages for the first few days.

For some reason this isn’t an issue with me. Past month I’ve been writing C#, Python, and Lua pretty much equally.

While I do sometimes almost write “nil” instead of “None” in Python, it’s rare enough not to be an issue.

Maybe it helps that I also use three human languages in my daily life, and mixing up words just isn’t something that happens (although forgetting does).

practice, practice, practice. there are some speed up hacks like 'don't start with an empty file' but mostly practice.
You'll get used to it the more you practice. Give it a few years, you'll hardly notice it. Also it helps that you don't fall into the trap of "one IDE for all programming languages", like the evangelists of VSCode plugins and/or vim preach out there.

Keep each programming language with its classical IDE, the more visually different one another is the easy the switch is.

For example I use Zend for PHP, Delphi for Object Pascal, Visual Studio for C# and for rare occasions I need same Visual Studio for C++ I have it differently configured both with colors and with menus arrangements to be strongly different than the C# one. Also I employ the strategy of each project in its own virtual machine. Nothing runs in the host OS, everything is in virtual machines.

Having this kind of development process is very easy to switch to that part of my brain dealing with that programming language, even when multiple virtual machines are running and I am switching between them.

Hope this helps and good luck.

> it helps that you don't fall into the trap of "one IDE for all programming languages"

I don't see this as a trap at all. I use IntelliJ for practically everything, and switching languages frequently (currently between Typescript and Scala several times a day) has not been difficult. I get way more benefit from developing increased familiarity with my environment.

I program Java and javascript for money and a mix of APL, BQN, J, emacs lisp, and clojure for fun.

The biggest challenge is switching from one array language to another, they are very similar but have subtle differences that I forget. Mostly because I am still not proficient at any of them.

Switching between a mainly imperative OOP langauge to a functional language is always very smooth, especially since my preference is lisp so the syntax and patterns are completely different, although modern Java has taken on a lot of functional goodness.

But I never really think about the switch, it has all become second nature to me.

I never thought about it but maybe it is related to being multi-lingual? E.g. I can switch between Danish and English no problem as well.

Use a linter, a formatter or any other tool that complains early and often. Have a starting setup for each programming language. Being surrounded by a supporting environment helps. You don't have to start with a blank document in a dumb editor.

Pick an IDE that you like. I really like PyCharm but vscode is also quite great.

To that extend you might even go further and have some specific visual clues per programming language. Whether that's different editors, editor profiles / themes depends on you.

I switch between Ruby and C++ frequently. My trick, which I admit is complete unplanned fluke, is different IDEs. Sublime for Ruby and XCode for C++. They look so different that my brain just disregards the other language. I briefly tried Ruby in XCode and nothing made any sense.

Assuming you don't want to install multiple editors, perhaps different colour schemes might produce something similar in your brain.