DEV Community

Cover image for Literate Programming style for AI-Supported Coding
Remo Dentato
Remo Dentato

Posted on

Literate Programming style for AI-Supported Coding

Introduction

I recently rewrote (for the n-th time) a small literate programming tool.
The concept always fascinated me: writing software structuring the code as an explanation of why it does what it does (as opposed to just setting up a bunch of classes, functions, variables, in the order the programming language forces me to do) always seemed a "good thing" to me.

Just have a look at the source code for my tng tool (of course, it is written using itself) and tell me how much easier it is to find your way in how the code works. A blessing for maintainers!

Reality, however, is much harsher. Very few programmers are also good writers, and I, for one, am no exception. Creating a document that is easy to read, well structured, and engaging is not a simple task. Literate Programming, for how interesting an idea it may be, never became mainstream and was left relegated to some small circle writing scientific software.

But then came AI. Whoever tried to use one of the many "copilots" around knows that to get any decent code out of them you have to go a great length in describing what you want, to get the proper logic, and create the right context to avoid the AI will hallucinate or re-invent functions that already existed.

This article explores the idea that Literate Programming, when combined with AI, could revolutionize the way we write and understand code. By leveraging AI to fill in the gaps, developers can focus more on the conceptual and design aspects of programming, potentially enhancing productivity and code quality.

What is Literate Programming?

Literate Programming, introduced by Donald Knuth in the 1980s, is a programming paradigm that intertwines code and documentation. Instead of writing code first and then documenting it later, developers write their programs as an exposition of logic in a natural language, interspersed with source code snippets. This approach aims to make the code more understandable and maintainable, as the documentation explains the thought process behind the code's structure and functionality.

In a traditional Literate Programming setup, a developer might describe the problem and solution in plain English, gradually introducing code snippets to illustrate specific points. This results in a comprehensive document that serves both as the program itself and its documentation, providing a clear narrative that can be followed by other developers or even the original author at a later date.

Literate Programming Meets AI

With the advent of sophisticated AI models, there's an exciting opportunity to enhance Literate Programming. By using a literate style, developers can describe the high-level structure and functionality of a program in natural language, leaving the AI to generate detailed code snippets or, vice-versa, write a snippet of code and ask for a comprehensive description of the way it works.

The key point is that code and documentation go hand in hand. They evolve together while the software is developed, the text part captures the reasoning behind the code and the code provides an implementation of what the text described. They are kept together to maximize the chance that code and text stay in sync.

This approach can significantly reduce the time spent on boilerplate code and help developers focus on the overarching design and logic of their programs.

Describing Programs to AI

Imagine you are tasked with creating a web application. Instead of jumping straight into coding, you start by outlining the application's features and functionalities in a literate programming style. For instance:

  1. Introduction and Setup:

    • We need a web application that allows users to register, log in, and manage their profiles.
    • The application will use a standard MVC framework.
  2. User Registration:

    • The registration form should capture user details like name, email, and password.
    • On submission, the data will be validated and stored in the database.
  3. User Login:

    • The login form should authenticate users using their email and password.
    • Upon successful login, users will be redirected to their profile page.

By refining these descriptions, the developer provides a clear narrative for what the program should do. The AI can then take these descriptions and generate the necessary code, filling in the blanks with specific implementations. For example, given the description of the registration process, the AI might generate the HTML form, the validation logic, and the database interaction code.

Benefits of AI-Assisted Literate Programming

  • Enhanced Productivity: By offloading routine coding tasks to the AI, developers can focus on more complex and creative aspects of software design.
  • Improved Documentation: Literate Programming inherently produces better-documented code, which is further enhanced by the AI's ability to generate comprehensive and consistent descriptions of the code.
  • Learning and Collaboration: This approach can serve as an educational tool, helping new developers understand the rationale behind code structures and decisions. It also facilitates better collaboration among team members by providing a clear and detailed narrative of the code.

Conclusion

Literate Programming, when combined with AI, offers a promising new way to approach software development. By focusing on describing the program's functionality and structure in natural language, developers can leverage AI to handle the detailed coding tasks.

Also, AI can improve the quality of documentation by rewriting (in better English) sketchy or roughly outlined descriptions of the code. As pointed out earlier, great programmers are not necessarily great writers.

This synergy between human creativity and machine precision can lead to more efficient, error-free, and well-documented code.

To fully grasp the benefits of AI in this context, we must shift our focus.
I believe we should try this new approach; using one of the many LitProg tools available (of course, I'm using my tng tool but I'm biased :) try to establish a new workflow that mixes writing, prompting, coding, cut, and paste. I'm quite sure there's something to gain.

In conclusion, the integration of Literate Programming with AI support holds the potential to significantly enhance the software development process. By shifting some of the workload to AI, developers can focus on innovation and design, ultimately leading to better software and a more enjoyable programming experience. Embracing this shift in mindset can prove itself crucial to unlocking the full potential of AI-assisted programming.

Top comments (0)