Onboarding: reading code you did not write
Onboarding is the time between "here is the repository" and "here is a change that passes review". Most of it is spent reading code, so the fair test is a small program a newcomer has to understand and then change. The one below counts words and prints the three most common, written in both languages, and then the same single typo is introduced into each.
The program in C++
A C++ program with a std::map counts, a vector of pairs is sorted by the count, and the first three are printed. The input is a string stream so the program runs without a file.
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
int main()
{
std::istringstream input("go go go cpp cpp rust");
std::map<std::string, int> counts;
std::string word;
while (input >> word) {
++counts[word];
}
std::vector<std::pair<std::string, int>> sorted(counts.begin(), counts.end());
std::sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) {
return a.second > b.second;
});
for (std::size_t i = 0; i < sorted.size() && i < 3; ++i) {
std::cout << sorted[i].first << " " << sorted[i].second << "\n";
}
}
g++ -std=c++23 -O2 -Wall -Wextra words.cpp -o words
./words
go 3
cpp 2
rust 1
The same program in Go
The Go program: a map counts, a slice of structs is sorted by the count, and the first three are printed. There is one place where the Go version is longer on purpose: opening a scanner over a reader can fail, and the error is checked instead of being ignored.
package main
import (
"bufio"
"fmt"
"sort"
"strings"
)
type entry struct {
word string
count int
}
func main() {
input := strings.NewReader("go go go cpp cpp rust")
counts := make(map[string]int)
scanner := bufio.NewScanner(input)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
counts[scanner.Text()]++
}
entries := make([]entry, 0, len(counts))
for word, count := range counts {
entries = append(entries, entry{word, count})
}
sort.Slice(entries, func(i, j int) bool { return entries[i].count > entries[j].count })
for i := 0; i < len(entries) && i < 3; i++ {
fmt.Println(entries[i].word, entries[i].count)
}
}
go run .
go 3
cpp 2
rust 1
What a newcomer has to know
In C++ the first minute goes to the includes: which header declares std::map, which one std::sort, and why <utility> is needed for std::pair. Then the types: the map owns its strings, the vector copies them, and the lambda takes them by const reference so the sort does not copy. The input is an istringstream, which never fails here; a reader has to know that the same code over an ifstream would report a missing file only through a stream state nobody checks.
In Go the imports are four packages and the compiler rejects an unused one, so the list is exactly what the file uses. The map is a built-in type, the slice grows with append, and the entry struct is declared next to its use. Every call that can fail returns an error that is either checked or visible as ignored. A reader who knows one Go file can read the next, because the language has no headers, no macros and no overload set to resolve.
The first mistake
Change string to strng in the map type, in each program. The C++ compiler prints this:
typo.cpp: In function 'int main()':
typo.cpp:13:19: error: 'strng' is not a member of 'std'; did you mean 'string'?
13 | std::map<std::strng, int> counts;
| ^~~~~
| string
typo.cpp:13:29: error: template argument 1 is invalid
13 | std::map<std::strng, int> counts;
| ^
typo.cpp:13:29: error: template argument 3 is invalid
typo.cpp:13:29: error: template argument 4 is invalid
typo.cpp:16:17: error: no match for 'operator[]' (operand types are 'int' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
16 | ++counts[word];
| ^
typo.cpp:19:60: error: request for member 'begin' in 'counts', which is of non-class type 'int'
19 | std::vector<std::pair<std::string, int>> sorted(counts.begin(), counts.end());
| ^~~~~
Sixteen lines for one typo, and five of the six errors are consequences of the first: the map became an int, so its subscript and its iterators are wrong too. The first message is right and even suggests the fix; a newcomer who stops reading after line two is fine, and one who reads to the end is frightened. The Go compiler prints:
# example.com/words
./main.go:18:21: undefined: strng
One line, one error, and no cascade, because Go stops at the first unresolved name in the package instead of substituting an error type and continuing. That is the whole difference in reading effort for a beginner, and it is why the first hour of a Go project is usually quieter than the first hour of a C++ one.
Counting the cost
| Measure | C++ | Go |
|---|---|---|
| Lines of the program | 27 | 34 |
| Files a reader must open | 1, plus the headers it includes | 1 |
| Declarations to read | 7 includes, 1 lambda, 1 comparator type | 4 imports, 1 struct |
| Error paths in the source | None: the stream is trusted | Two, both visible |
| Lines of the first typo diagnostic | 16 | 2 |
| Commands to build and run | 2 | 1 |
Go is the longer program here, by seven lines, and the shorter diagnostic, by fourteen. A newcomer reads the program once and the diagnostics many times, which is why the second number matters more than the first, and why a language that optimises for the reader rather than the writer is easier to join.
Where each one is easier
This program is close to C++ at its best: the standard algorithms do the work, the types are short, and nothing is allocated by hand. A newcomer who knows the standard library reads it in a minute. C++ becomes hard to join when the code uses template metaprogramming, multiple inheritance, macros or a build system with a hundred targets, and none of those is visible in a twenty-line example.
Go is easy to join at the level of one file and stays at that level: the language has little to learn and the tools are the same everywhere. It becomes tedious when the program needs what Go deliberately left out, and a newcomer then has to learn the project's conventions for generics, errors and context instead of the language. The previous page of this group is tooling; the next one is maintainability over years.
The C++ program was built with g++ 15.2.0 (Alpine 15.2.0) at -O2 and the Go program with go 1.26.10, both run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev