Go, C# and Python: traps a reviewer must see
Each language hides a few sharp edges in ordinary-looking code. These are the ones that cause incidents.
Remember: Go: check every error and protect shared state. C#: check nulls, use a cryptographic random source. Python: never use assert for security, and know what -O removes.
Languages differ, but the questions are the same: what does this return when it fails, who else can see this value, and what does the runtime remove or allow. Learn the traps of the languages you read, and the code you review will look different.
Go
- A returned error that is not checked: the call compiles and the failure disappears. Check every error where it is returned.
- A shared map written from many goroutines: the runtime aborts the process with 'concurrent map writes'.
- Nil maps and nil pointers: writing to a nil map panics, and a nil interface is not the same as a nil pointer inside it.
- defer inside a loop, which runs at the end of the function, not each iteration.
store.Debit(from, amount)
store.Credit(to, amount)
C#
- Null references: FindAsync and dictionary lookups return null. Check before you read a member, and enable nullable reference types.
- System.Random for tokens or codes: it is predictable. Use RandomNumberGenerator.
- String interpolation inside SQL: $"SELECT ... {id}" is a SQL injection, the same as an f-string in Python.
- Decimal division by zero throws. An empty list is a value to handle, not a crash to ignore.
- BinaryFormatter and other binary deserialisers on untrusted data: deserialisation can run code.
var rng = new Random();
RandomNumberGenerator.GetBytes(32);
Python
- assert for a security check: python -O removes every assert, so the check disappears in optimised builds.
- Negative indexes and slices: they count from the end, and never raise.
- random for tokens: use secrets.token_urlsafe or os.urandom.
- eval, exec, pickle and yaml.load on untrusted input: each one can run code.
assert current_user.is_admin, "admins only"
if not current_user.is_admin:
raise PermissionError("admins only")
When you read unfamiliar code, look for three things in every language: the error path, the shared state, and the things that the runtime or the build can switch off.
Questions to ask yourself
- What does this return or throw when it fails, and does the caller see the failure?
- Is this value shared between requests or goroutines, and who can write to it?
- Does this check still run in a release build, with the interpreter flags or compiler settings used in production?
Practise this
- Transfer that ignores errors · Business logic
- Shared cache without a lock · Business logic
- Six-digit reset codes from System.Random · Authentication & cryptography
- Admin check written as an assert · Authorization