Home All posts #dev

Contract oriented development

As everyone knows, when you are working on business real software, I mean, using software engineering where the software needs to be as predictable as possible faithfully following the business rules and quality standards, one of the problems that usually appears is the bureaucracy because we are obligated to follow rules that sometimes are completely useless wasting our time.

Regularly we waste our effort and resources making unnecessary features and development methodologies so Im gonna tell you what I do in this cases, always thinking in the simplest solution to solve business problems.

The typing is the best standard technique to make our applications scalable and predictable if you are an experimented programmer probably you already know this way to do software. Well starting in the database designing, almost the entire application can be derived from that, in the most cases of business applications/platforms, these are composed of cruds and transactions with not much complex logic, so all you need is contracts.

Contracts

A contract is a formal agreement and explicit that exposes a rules that must be followed by preconditions (Max length, type, etc), postconditions (That returns the correct expected type) and invariants (conditions that must be true before and after running the code). A contract could be a struct type or a interface.

In typescript we have libraries like zod that permit us to make contracts even from our orm.

// Drizzle entity

export const residentialAddressTable = pgTable("residential_address", {

id: bigint({ mode: "number" }).primaryKey().generatedAlwaysAsIdentity(),

city: varchar({ length: 96 }).notNull(),

department: varchar({ length: 64 }).notNull(),

homeAddress: varchar({ length: 255 }),

extra: varchar({ length: 255 }),

});

// Zod contract

import { residentialAddressTable } from "../../db/schemas/index.js";

import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";

extendZodWithOpenApi(z);

// making our contract

export const residentialAddressSelectSchema = createSelectSchema(

residentialAddressTable,

)

Why is this useful?

Well, if we use this way to orient our whole app we can later expose these contract to client-side. I mean, if we're using an api rest to share data with the frontend. It will be as easy as implement a repository that performs the data operations and return it in the api using OpenAPI standard. And these OpenAPI standard permits expose our all contracts in an one file, then we will give it to our agent and code the frontend implementation full based in the backend agents. This without wasting tokens on analyzing the apis and saving more tokens dividing the development.

Thanks for read, this post is still an eraser and will be edited in the future