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.
using System.Text.Json;
var web = JsonSerializerOptions.Web;
var text = await File.ReadAllTextAsync("ledger.json");
var ledger = JsonSerializer.Deserialize<Ledger>(text, web)!;
ledger.Entries.Add(new Entry(12.5m, "lunch"));
var output = JsonSerializer.Serialize(ledger, web);
await File.WriteAllTextAsync("ledger.json", output);
record Entry(decimal Amount, string Note);
record Ledger(List<Entry> Entries);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.
using System.Net.Http.Json;
using var http = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10),
};
var url = "https://jsonplaceholder.typicode.com/todos/1";
var todo = await http.GetFromJsonAsync<Todo>(url);
Console.WriteLine(todo?.Title);
record Todo(int Id, string Title, bool Completed);Watch out
Assuming the call succeeds: set a timeout, check the status code and handle the network being down.
NuGet packages
Also called NuGet, dotnet add package, PackageReference, solution
- What
- NuGet is the .NET package manager.
dotnet add packageadds aPackageReferenceto the.csproj, anddotnet restoredownloads what is listed. - Why
- Dependencies are recorded in the project file, so anyone can clone the repository and build it with
dotnet build.
dotnet add package Spectre.Console
dotnet list package
dotnet restore
dotnet buildWatch out
Adding a large package for something the base library already does, such as JSON with System.Text.Json.
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 string Name = "WEATHER_API_KEY";
string apiKey = Environment.GetEnvironmentVariable(Name)
?? throw new InvalidOperationException($"{Name} not set");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.