# In Search of Code Purity

![rw-book-cover](https://i.ytimg.com/vi/voRBS0r4EyI/maxresdefault.jpg?v=65aa862f)

## Metadata
- Author: [[No Boilerplate]]
- Full Title: In Search of Code Purity
- Category: #articles
- Document Tags: [[dev]] [[dev/rust]] 
- URL: https://www.youtube.com/watch?v=voRBS0r4EyI

> [!note]
> Goal: The intention is to leverage the principles of functional programming purity in a project, utilizing Rust or other programming languages, to enhance code readability, maintainability, and reliability. This to-do list will help achieve that by outlining actionable steps for implementation.
> - [ ] 🦀 **Familiarize with Rust's const functions** to understand how they execute at both compile time and runtime, ensuring the utilization of pure functions.
> - [ ] ⚙️ **Implement pure functions** in the codebase, ensuring they do not produce side effects and maintain referential transparency for predictable behavior.
> - [ ] 🔍 **Establish a system for tagging functions** as pure in the codebase, allowing both developers and the compiler to reason about function behavior effectively.
> - [ ] 📜 **Utilize Rust macros wisely** to enable compile-time evaluations effectively, while understanding their limitations compared to const functions.
> - [ ] 📝 **Incorporate error handling** strategies focused on return types and assertions for better debugging of const functions.
> - [ ] 🧪 **Experiment with caching techniques** that leverage the predictability of pure functions to optimize performance in the application.
> [!tldr]
> Alternate title: Rust's Hidden Purity System
I was taught formal methods at university but these ultra-safe development techniques are expensive, require using unusual external verification languages, and most damning for web and application developers, they slow down iteration.
After graduating and getting a webdev job, I despaired that the safety and guarantees of the formal systems that I had been introduced to weren't available to me as a web developer.
I was going to have to act if I wanted to live in a different world.

❤️ If you would like to support what I do, I have set up a Patreon here: https://www.patreon.com/noboilerplate - Thank you!

📄 All my videos are built in compile-checked markdown, transcript source code available here https://github.com/0atman/noboilerplate this is also where you'll find links to everything mentioned. 

🖊️ Corrections are in the pinned ERRATA comment.

🦀 Start your Rust journey here: https://www.youtube.com/watch?v=2hXNd6x9sZs

👕 Bad shirts available here https://www.teepub...

## Highlights
first function here factorial is a pure function a function that doesn't cause or rely upon side effects we know this without reading the function body because it doesn't have IO in the signature the second function main prints to the screen and so must have I/O this is a fantastic way to keep side effects managed and covers half of the nightmare errors I've seen throughout my career [View Highlight](https://read.readwise.io/read/01j6ez3db15q2wagxxh4fxpagw))

if your language has a way of separating or tagging functions that are pure and then can hold you to that contract both you and the compiler can reason about your code in useful new ways [View Highlight](https://read.readwise.io/read/01j6ezg1548mj8f5ntkwrhxh86))

If a pure function is called twice with the same inputs the result is guaranteed to be the same every time this is called referential transparency or idance or determinism this enables perfect predictable caching of return values which your compiler might automatically and easier debugging [View Highlight](https://read.readwise.io/read/01j6ezh53e7hcszmxxd91aze3r))

const functions are functions that can be executed at compile time as well as runtime they differ from rust macros which can only run at compile time and can do anything by being much more limited [View Highlight](https://read.readwise.io/read/01j6ezpb7jgqybcmhz38r5sdxk))

when debugging the only way to get information out of a const function is by its return type or Hal in compilation
with a panic or a failed assertion [View Highlight](https://read.readwise.io/read/01j6f15drv4ypknhpcqz8jh9dc))

macros execute arbitrary code at compile time and then can insert the
results of that processing as potentially const values [View Highlight](https://read.readwise.io/read/01j6f1arc85y5twtf781mgy5tz))

rust's const functions are only pure once you get to runtime [View Highlight](https://read.readwise.io/read/01j6f1a5g0yddv78empzwmh1xp))

rust
is as pure as possible but no purer [View Highlight](https://read.readwise.io/read/01j6f1env98rtskyvf5sp1eygp)) [[dev/rust]] [[dev]] 

