DEV Community 👩‍💻👨‍💻

Cover image for Carbon: A New Programming Language from Google (Will it Replace C++)
Quokka Labs for Quokka Labs

Posted on

Carbon: A New Programming Language from Google (Will it Replace C++)

Carbon is experimental, but we all know that new languages always come up with new features, faster programming, solving predecessor errors, and more. In this article, we will learn about the new Carbon Programming language and answer the biggest question "will it replace C++?" But it's not that easy to answer.
Calm down; you will get the answer. Before that, let's look deeper at Carbon and its capability improvement over C++, its features, and more.

Quick Overview of Carbon

Google is the developer behind the Carbon development. Carbon is also an open-source programming language like C++, released on July 19, 2022, during the CPP North C++ conference in Toronto. At the Conference, Google engineer Chandler Carruth revealed about Carbon.
Carbon is like Type script to Javascript and Kotlin to Java. We know it's not an immediate replacement for C++. Still, Carbon aims to make it easy for developers at enterprise level adoption and existing codebase migration.

Carbon programming language is developed to reach the below-listed goals:

  • Fast and scalable in development
  • Focus on code that is easy to write
  • Testing mechanisms
  • Practical safety
  • Evolution of language and software
  • A gentle learning curve for C++ developers

Now, let's have a quick look at the features of Carbon.

Top Features of Carbon

Compatibility with C++

If you are a C++ developer, the learning curve will be gentle. C++ single library is also migratable to Carbon by adding a new application or in an existing application. Let's see what the codes of both languages look like.
Example: C++ Code

//C++:
#include <math.h>
#include <iostream>
#include <span>
#include <vector>

struct Circle {
  float r;
};


void PrintTotalArea(std::span<Circle>circles){
 float area = 0;


 for(const Circle& c:circles){
    area += M_PI * c.r * c.r;
  }

  std::cout <<"Total area: " << area << endl;
 }

auto main(int argc, char** argv)->;int{
  std::vector<Circle> circles = {{1.0},{2.0}};


// Implicitly constructs 'span' from 'vector'.
  PrintTotalArea(circles);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

If translated to Carbon, then the code will look like this:

//Carbon:
package Geometry api;
import Math;

class Circle {
varr:f32;
} 


fn PrintTotalArea(circles:Slice(Circle)){
var area: f32 = 0;

for(c:Circle in circles){
  area += Math.Pi * c.r * c.r;
}


Print("Total area:{0}",area);
}

fn Main() ->; 132 {
  //A dynamically sized array,like`std::vector`.
  var circles:Array(Circle)=({.r 1.0),{.r=2.0});


// Implicitly constructs`Slice` from`Array`.
Print TotalArea(circles);
return 0;
}
Enter fullscreen mode Exit fullscreen mode

An Up-to-Date Generics System

Carbon provides easy C++ interoperability with opt-in templates and an up-to-date generic system. The generic system offers a few advantages:

  • Generic system type-checks to avoid the compile-time cost of re-checking
  • A robust checked interface reduces accidental dependencies during implementation details and clarifies the agreement.

Memory Safety

Carbon solves key issue regarding memory safety that is present in C++ by

  • The robust and default debug mode that's more modern than C++
  • Supporting dynamic bounds checks and strong builds by creating base APIs and idioms
  • Improving the tracking of uninitialized states, toughening against initialization bugs, and increasing initialization enforcement

We know about Carbon's top features that solve some problems of C++. Why can't C++ be improved? Let us clear it.

What's wrong with C++?

We know C++ is a dominant programming language for critical software, and developers would improve C++ to keep it dominant. But improving C++ to get the same experience as a modern programming language is complex. Why? It's because of Language and Governance.

  • More and more features are added but not removed/replaced. Due to backward compatibility prioritization, it is tough to fix issues.
  • The C++ improvement process is lengthy because of a bureaucratic committee approach. It is the committee where all the decisions for C++ improvements, like the design to standradization, are processed.

Carbon Is here. What does it promise?

Carbon programming language intends to start from scratch:

  • Simple syntax
  • Up-to-date generics system
  • Modular code organization

Carbon aims to be more inclusive by:

  • Formulating using open-source methods, procedures, and tools. It's simpler and more transparent to contribute
  • Having a transparent governance structure that can act quickly when necessary
  • Enhancing the ecosystem with tools (compiler, standard library, IDE tools) that offer a rich developer experience as well as tool-based upgrades
  • Utilizing a built-in package manager to solve a gap in the C++ ecosystem

What do developers say about Carbon Vs. C++?

After reading many developer comments on the Carbon vs. C++ topic, I have concluded the below lines. Still, it's not the final judgment yet!

  • Developers seem to be positive about the overall Carbon release
  • Most developers love the C++ interoperability
  • Some developers seem to be arguing about Carbon's widely adoption
  • Some JavaScript developers are confused about immutable declarations "let," but Swift developers are optimistic about Carbon.

Final Words

The answer to your biggest question, "Will it replace C++?" is very early to judge.
This article is not the final judgment on which one to use, but you can get a decent overview of Carbon and how it fixes errors that C++ has.
The C++ improvement process is lengthy and may take years; on the other hand, the successor gives few advantages. Time will only tell which programming language will be dominant.

If you wish to know more about these trending programming languages for mobile app development- Programming Languages For Mobile App Development

Top comments (0)

👋 Welcome new DEV members in our Welcome Thread

Say hello to the newest members of DEV.