Refactoring
Renaming a field is the cheapest refactor and the best test of a toolchain: it touches code that no test exercises, and the only thing that can find every use is a compiler that reads every use. Both projects below have a configuration struct, one call site and one function that nothing calls. The rename goes wrong in a different way in each language.
Rename a field in C++
A C++ project: the header declares the struct, one file uses the field, and a template in a third header uses it too but is never instantiated.
#pragma once
#include <string>
struct Config {
std::string address;
int port;
};
#pragma once
#include "config.h"
#include <iostream>
template <typename T>
void log_host(const T& config)
{
std::cout << config.host << "\n";
}
#include "config.h"
#include "log.h"
#include <iostream>
int main()
{
Config config{"db.local", 5432};
std::cout << config.address << "\n";
}
Rename the field in the header with sed, the tool a C++ codebase usually reaches for, and compile:
sed -i 's/std::string host;/std::string address;/' config.h
g++ -std=c++23 -O2 -Wall -Wextra main.cpp -o main
main.cpp: In function 'int main()':
main.cpp:9:25: error: 'struct Config' has no member named 'host'
9 | std::cout << config.host << "\n";
| ^~~~
The compiler found the call site because it compiled main.cpp. Fix the line and the program builds and prints the address; the rename is done as far as this translation unit is concerned.
The use the compiler never sees
The template in log.h still says config.host. Nothing instantiates it, so nothing checks it, and a file that includes the header compiles clean:
#include "log.h"
void nothing()
{
}
g++ -std=c++23 -O2 -Wall -Wextra -c log_only.cpp -o log_only.o; echo "status: $?"
grep -n host log.h
status: 0
10: std::cout << config.host << "\n";
A clean build of the project and a green test suite can both be true while this line is broken. The bug appears the day somebody calls log_host, in a file that was not part of the rename, possibly months later. This is not an argument that C++ is broken; it is the cost of a language whose unit of checking is the translation unit, and the reason a large C++ rename ends with a grep for the old name and a full rebuild of every target.
Rename a field in Go
The same project as a Go module with two packages. The struct and a function that nothing calls are in config, the call site in app.
package config
// Config is the database settings.
type Config struct {
Address string
Port int
}
// HostOf returns the host of c. Nothing calls it yet.
func HostOf(c Config) string {
return c.Address
}
package main
import (
"fmt"
"example.com/refactor/config"
)
func main() {
cfg := config.Config{Address: "db.local", Port: 5432}
fmt.Println(cfg.Address)
}
The rename changes the field declaration and nothing else, and the build reports the stale use inside the function that no one calls:
sed -i 's/^\tHost string$/\tAddress string/' config/config.go
go build ./...
# example.com/refactor/config
config/config.go:11:11: c.Host undefined (type Config has no field or method Host)
Fix that line and the build moves to the caller, which is the second error a rename should produce:
go build ./...
# example.com/refactor/app
app/main.go:10:23: unknown field Host in struct literal of type config.Config
app/main.go:11:18: cfg.Host undefined (type config.Config has no field or method Host)
gofmt -r
gofmt has a rewrite mode: a pattern of the form old -> new is matched against the syntax tree and every match is replaced. The pattern is Go syntax, not a regular expression, so it does not touch a comment or a string that happens to contain the word.
gofmt -r 'Host -> Address' -w app/main.go
gofmt -w .
The second command is not decoration: a field name that gets longer changes the alignment of the struct, and gofmt rewrites the spaces so the columns line up again. After both commands the module builds, vets and runs:
gofmt -l .
go build ./...
go vet ./...
go build -o refapp ./app && ./refapp
db.local
The same rewrite works on any identifier, not only a field: a function name, a local variable, a method call. It is not a type-aware rename, so a pattern that matches too much will rewrite too much, which is why a real rename in an editor uses the language server; but it is in the toolchain, it needs no project index and it works on a checkout with no build cache.
What the compiler still cannot see
Go has a hole of its own, and it is a string. A struct tag names the JSON key, and neither the compiler nor gofmt reads it:
package main
import (
"encoding/json"
"fmt"
)
type Config struct {
Address string `json:"host"`
}
func main() {
data, _ := json.Marshal(Config{Address: "db.local"})
fmt.Println(string(data))
}
go run ./tag
{"host":"db.local"}
The Go field is now called Address and the wire format still says host. That is often exactly what an API wants, and it is a silent change when it is not. A C++ program has the same class of problem with a serialized field name written by hand, and no language solves it without a schema.
What this says about maintenance
A rename is a proxy for every change a maintainer makes. In Go, go build ./... reads every function of every package, called or not, and stops at the first file that still uses the old name. In C++, the change is checked per translation unit and per instantiation, so the answer to "did we get all of them" is a full build plus a search. Neither answer is complete: reflection, struct tags, generated code, #ifdef branches and dynamic loading all hide uses from both.
The practical difference is the size of the safety net, not its existence. A team that refactors often feels it every day; a team that ships a library and freezes its API feels it once a year. The previous page of this group is performance; the next one is tooling.
The C++ projects were built with g++ 15.2.0 (Alpine 15.2.0) at -O2 and the Go programs with go 1.26.10, all on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev