Chapter 09
Build and ship projects
Turning skills into finished, shareable work: scoping, JSON and APIs, dependencies, configuration, a good README and publishing on GitHub.
Scoping a small project
Also called MVP, minimum viable product, milestones
- What
- Cutting an idea down to the smallest version that solves one real problem end to end, then splitting it into a handful of milestones.
- Why
- Finished small projects teach more and show more than ambitious unfinished ones.
- Example
- Not "a personal finance app", but "a command-line tool that reads my bank CSV and prints spending per category per month". Milestones: parse the CSV, categorise, total, print, test, README.
Watch out
Starting with login, a database and a UI before the core logic works at all.
Reading and writing JSON
Also called JSON, serialisation, parsing JSON, JSON.parse
- What
- JSON is a text format for objects, arrays, strings, numbers, booleans and null. Serialising turns your data into JSON; parsing turns JSON back into data.
- Why
- It is how most APIs and many config and save files exchange data. Python, JavaScript and C# ship a JSON library; Java and C++ usually add Jackson or nlohmann/json.
#include <nlohmann/json.hpp>
using json = nlohmann::json;
std::ifstream in{"ledger.json"};
json data = json::parse(in);
in.close();
data["entries"].push_back(
json{{"amount", 12.5}, {"note", "lunch"}});
std::ofstream out{"ledger.json"};
out << data.dump(2);Watch out
Trusting parsed JSON blindly: fields can be missing, null or the wrong type, so validate before use.
Calling a public API
Also called HTTP, REST API, fetch, HttpClient, requests
- What
- Sending an HTTP request (usually
GETorPOST) to a web service and reading its response, typically a status code plus a JSON body. - Why
- Public APIs give a small project real data (weather, books, transport) without building a backend.
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
auto res = cpr::Get(
cpr::Url{"https://jsonplaceholder.typicode.com/todos/1"},
cpr::Timeout{10000});
if (res.status_code != 200) {
throw std::runtime_error("HTTP error");
}
auto todo = nlohmann::json::parse(res.text);
std::cout << todo["title"] << '\n';Watch out
Assuming the call succeeds: set a timeout, check the status code and handle the network being down.
CMake
Also called CMakeLists.txt, build system, vcpkg, Conan
- What
- CMake describes how to build a C++ project in
CMakeLists.txtand generates the real build files for your platform. Package managers such as vcpkg or Conan supply libraries. - Why
- It is the de facto standard for C++ projects, so others can build yours on Linux, macOS or Windows with the same two commands.
cmake_minimum_required(VERSION 3.25)
project(ledger CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(ledger src/main.cpp src/pricing.cpp)
target_compile_options(ledger PRIVATE -Wall -Wextra)
# cmake -S . -B build && cmake --build buildWatch out
Committing the generated build folder, or hard-coding compiler paths that only exist on your machine.
Configuration and secrets
Also called environment variables, .env, API keys
- What
- Settings that change between machines (API keys, URLs, file paths) are read from environment variables or a local config file instead of being written into the code.
- Why
- It keeps secrets out of Git and lets the same code run on your laptop, a friend's machine and a server.
const char* key = std::getenv("WEATHER_API_KEY");
if (key == nullptr) {
throw std::runtime_error("WEATHER_API_KEY not set");
}
std::string api_key{key};Watch out
Committing an API key: bots scan GitHub within minutes, so revoke the key at once, since deleting the commit is not enough.
Writing a README
Also called README.md, project documentation
- What
- The front page of a repository: what the project does and for whom, a demo, how to install, run and test it, how it works and its known limits.
- Why
- It is the first thing a reviewer, teammate or employer reads, and often the only thing. A project nobody can run might as well not exist.
# Ledger
One sentence: what it does and who it is for.
## Demo
A GIF or two screenshots.
## Getting started
Requirements, then install and run commands.
## Running tests
## How it works
## Limitations and next stepsWatch out
Leaving the default template, or skipping the exact commands to run the project from a fresh clone.
Publishing on GitHub
Also called gh repo create, licence, portfolio
- What
- Pushing a project to a public GitHub repository with a README, a licence, a
.gitignoreand a clean history of meaningful commits. - Why
- It backs your work up, makes it easy to share, and shows how you work, not just what you built.
git remote add origin https://github.com/you/ledger.git
git branch -M main
git push -u origin main
gh repo create ledger --public --source=. --pushWatch out
Pushing one "initial commit" containing the entire finished project, which hides how it was built.