The Case for Single Assignment: Write Safer, Clearer Code

Stop Reassigning Variables! When writing code, especially in dynamic languages like Python, it’s tempting to reuse and reassign a variable multiple times. We should be advocating for a powerful practice that significantly improves code quality: Single Assignment. The rule is simple, echoing the advice of software veterans like John Carmack: Strive to assign a value to a variable only once outside of true iterative calculations (like accumulation inside a loop). ...

October 31, 2025

Expose your local port on public internet

You can use Cloudflare infrastructure to expose your local app to the public i.e. for demo purposes or for testing on a device outside your local network. Prerequisite: install cloudflared. brew install cloudflared Example If you want to expose your app running on port $PORT in local network, run the following: cloudflared tunnel --url http://localhost:$PORT Your app will be accessible over a random subdomain matching *.trycloudflared.com Check the command output to get the publicly accessible URL You don’t need to be logged in to Cloudflare to use it

July 20, 2025

Python: Function Documentation with `Annotated`

The core idea is to leverage Python’s typing.Annotated to embed params documentation directly within function parameter and return type hints. Example: from typing import Annotated, Any def foo( x: Annotated[int, "Represents the horizontal coordinate."], ) -> Annotated[float, "Calculated y-coordinate."]: return float(x * 2) Drawback: the comment within Annotated should not be too long, otherwise it will decrease function signature readability.

July 20, 2025

Google Sheets Snippets

The LET Function Let’s say you want to extract the domain from an email address in your Google Sheet. You could easily do this with the following formula: =RIGHT(A1, LEN(A1) - FIND("@", A1)) However, if you need to apply this to a different cell, you’d have to update the cell address (A1) in three places within the formula. This is where the LET function becomes incredibly useful. With LET, you can streamline the formula like this: ...

June 25, 2025

CrunchyData Postgres Operator snippets

Specification Available here Example usage Available here

April 9, 2025

Helm snippets

Debugging Helm Templates To debug Helm templates, navigate to the Helm chart directory and run: helm template test . --dry-run --debug \ --values values.yaml \ --values values/dev.yaml > output.yaml This command renders the templates with specified values and outputs the result to output.yaml. The --dry-run and --debug flags provide valuable debugging information.

March 2, 2025

Kubernetes snippets

Spec Explorer kube-spec.dev: A handy tool for exploring Kubernetes resource specifications. Link: https://kubespec.dev Running a One-Time Job from a CronJob To execute a job based on an existing CronJob, use the following command: kubectl create job --from=cronjob/<cronjob-name> <job-name> -n <namespace-name> Source: https://stackoverflow.com/a/50041304 Spawning a Curl Container for Debugging To quickly spin up a curl container with specific security and resource constraints, use: kubectl run debug-curl \ --image=curlimages/curl \ --overrides='{ "apiVersion": "v1", "spec": { "securityContext": { "runAsNonRoot": true, "runAsUser": 1000, "runAsGroup": 1000 }, "containers": [ { "name": "debug-curl", "image": "curlimages/curl", "resources": { "requests": { "cpu": "100m", "memory": "128Mi" }, "limits": { "cpu": "200m", "memory": "256Mi" } }, "command": ["/bin/sh", "-c", "sleep infinity"] } ] } }' This command creates a debug-curl pod with a non-root user context and resource requests/limits. Connect to the shell using: kubectl exec -it pod/debug-curl sh Delete container using kubectl delete pod/debug-curl Getting Kubernetes Events To view Kubernetes events sorted by creation timestamp, use: ...

March 1, 2025

Postgres Troubleshooting Guide

1. Monitor CPU Usage Use Kubernetes-native tools like kubectl top to check CPU usage for the PostgreSQL pod: kubectl top pod <postgres-pod-name> -n <namespace> Check if the CPU usage is consistently high or spiking. 2. Check PostgreSQL Metrics Use PostgreSQL’s built-in statistics and monitoring tools: pg_stat_activity: Identify active queries and their states. SELECT * FROM pg_stat_activity; pg_stat_statements: Identify the most resource-intensive queries. SELECT query, calls, total_exec_time, rows, 100.0 * total_exec_time / sum(total_exec_time) over () as percentage FROM pg_stat_statements ORDER BY total_exec_time DESC; Reset pg_stat_statements: ...

February 28, 2025

TypeScript lookup type

Context You want to declare an array of options. It should not change during the runtime of your program. To enforce non-mutability you declare it using const assertion. const options = ["Foo", "Bar", "Baz"] as const; const assertion means that you ask compiler to use the narrowest or most specific type. So options is not string[] but a readonly, 3-elements tuple. Problem Now you want to create a type that reflects literal strings listed in this options tuple. ...

July 24, 2022

TypeScript freshness

A friend of mine has recently intrigued me with a question about TypeScript. He asked: Why TypeScript is not consistent in the following situation? Imagine you have a type Foo and type Bar which extends Foo. Why, when passing instance of Bar into a function accepting Foo, TypeScript does not complain, but when we try to declare a variable of type Foo but with extra keys specified, it does complain. ...

July 12, 2022