Ask HN: How do you switch between programming languages?
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 ] threadSince 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").
With that being said it is a great thing to know, but likely unnecessary unless you’re very serious about programming.
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.
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.
Of course, if you have a style guide, you should stick to that.
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.
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.
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.
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
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.
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.
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!
?? ?: ?. .? &. || and more ascii salad
Also the semicolon or access of ivars with arrow, this, self or directly is a gradient transition every time.
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_.
Go and TS(js\java\c++ etc) may look similar now but they won't after some time.
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.
2. install language runtime support extension for first language
3. Install language runtime support extension for second language
4. RFTM #1
5. RTFM #2
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.
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).
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.
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.
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.
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.
Assuming you don't want to install multiple editors, perhaps different colour schemes might produce something similar in your brain.