Carbon is a new programming language created by Google engineers as a successor to C++. Here’s what software developers need to know about it.
Earlier this year, a new programming language called Carbon was unveiled. It is a systems programming language first introduced on 19 July by Chandler Carruth, a software engineer at Google, at the CppNorth conference in Canada.
While it is still at an experimental stage, Carbon could act as a successor to C++. But what do developers need to know about it? To find out more, we spoke to Norway-based technology and software development writer Erik Engheim.
He said at the moment, Carbon is at a very early stage. “What we have is an early incomplete specification together with an interpreter called Carbon Explorer, which allows you to test out parts of the language,” he said.
“Thus when evaluating what Carbon could be for developers, we have to look at the current language specification and the long-term goals of the language.”
What makes Carbon different?
New programming languages often emerge. For example, Engheim said Scala and Clojure are languages that were made with the idea of being able to reuse existing Java code in new and modern languages.
However, adopting these languages for existing Java developers “never worked out that well” because it was difficult to run a project mixed with both Java and Scala.
But then a new language, Kotlin, arrived. “It picked up many of the good ideas of Scala, but packaged in a way that Java developers can easily comprehend. Kotlin is semantically similar enough to Java that it can be dropped into a Java project and you can easily call Kotlin code from Java and Java code from Kotlin.”
Engheim pointed out that Swift succeeded for similar reasons by being a new language designed specifically to blend well with Objective-C.
“No need to make a full commitment to a new language. A project could easily exist as a mix of Swift and Objective-C. Object-C developers could easily call Swift code and Swift developers could easily call Objective-C code. Mentally going between the languages were not too big of a burden.”
So where does Carbon fit into all of this? Engheim said it’s the same concept as Swift and Kotlin, in that it is a new language that aims to be “a drop-in replacement” for C++, allowing developers to have both in the same project.
“That is very different from something like Rust, Go, D and Nim, which are all systems programming languages that, in principle, can replace C++. The problem is that they are not designed to have a mixed project. These languages can, through extra work, reuse existing C++ code but not effortlessly.”
Why use Carbon instead of C++?
While being able to mix both languages in a project can be seen as a good thing, why would developers want to know Carbon at all when they already have C++?
Engheim said Carbon could be thought of as a clean implementation of C++ and potentially allow less senior developers to enter the fray.
“C++ has become a specialist language used primarily by experienced and very capable developers. Sure, it is not impossible for inexperienced developers to write C++ code, but they run a serious risk of making many bad programming mistakes. C++ is a language which offers a rich buffet of ways to shoot yourself in the foot. Writing quality C++ code thus requires a lot of experience and skill,” he said.
“Carbon will open the doors to allow less experienced developers to make valuable contributions to an existing C++ project. From a company perspective, Carbon means less time has to be spent on training to get developers to produce quality code. The key appeal to companies is that they can reuse large C++ codebases which they have spent small fortunes developing over many years.”
There’s something in it for experienced developers too, according to Engheim, who said Carbon could offer better productivity and a bit of a safety net.
What else sets Carbon apart?
Based on the language specification, Engheim has noticed some other features of Carbon:
- No more null pointers – this has been called the billion-dollar mistake by British computer scientist Tony Hoare, and Carbon isn’t making it
- All values must be initialised to a known value – C++ allows uninitialised values, which can cause strange behaviour when running your code
- Control-flow statements expects Boolean expressions – you can accidentally create infinite loops in C++ by not providing a Boolean expression, and a for-loop will not complain if given an integer as its stopping condition in C++
- There are no constructors so you cannot accidentally blow up your code by calling a virtual function from a constructor
- You cannot accidentally override a virtual method, like in C++
- Template programming becomes much easier to do in Carbon than in C++ as you have interfaces, which defines clearly what a template type should support
Advice for developers
While there are a lot of potential reasons to get excited about the new programming language, Carbon is still in its very early stages, which means there’s a lot that is unknown.
For example, Engheim noted that the details of memory management have not been hashed out yet. “Manual memory management is a given, but there is of course various ways in which we would help facilitate such allocation and deallocation,” he said.
“While Carbon does not have constructors, it does in fact have destructors, so that is an important concept C++ developers will be familiar with and which they tend to love as it gives a nice way of handling resources. A destructor for an object representing a file could for instance make sure that a file handle is closed.”
There are no compilers for Carbon and the interpreter is very limited in functionality, Engheim said, so he advised waiting a few years before diving into the language, especially if you’re a developer who likes to be very hands-on.
However, for those who are still curious to see the language and what’s coming down the road, there are a few options.
“You could read the language specification yourself, or you could cheat and read articles written by others, including me, which goes into specific aspects of the language,” he said.
“Of course, the creators have various code examples you could look at. There is of course the original presentation as well. It depends on what you are looking for. Reading a language specification will not appeal to everyone, hence reading popularised versions of that material may be more sensible.”
Source: Jenny Darmody. 2022. “Programming skills: What you need to know about Carbon”. https://www.siliconrepublic.com/advice/carbon-programming-language-google