---
type: "article"
title: "Coding with AI"
newsletter: "Jordi Villar"
newsletter_handle: "jordivillarcom"
newsletter_url: "https://usecommune.com/n/jordivillarcom"
author: "Jordi Villar (@jordi)"
published: "2024-08-23T13:09:33.053Z"
canonical_url: "https://usecommune.com/n/jordivillarcom/a/coding-with-ai"
markdown_url: "https://usecommune.com/n/jordivillarcom/a/coding-with-ai.md"
chat_url: "https://usecommune.com/n/jordivillarcom/a/coding-with-ai/chat"
source_url: "https://jordivillar.com/notes/coding-ai/"
body_source: "imported"
likes: 0
replies: 0
body_words: 255
---

# Coding with AI

Read the full article in your browser [here](https://jordivillar.com/notes/coding-ai/)

# Coding with AI

![Zed screenshot](https://jordivillar.com//coding-ai-1.png)

I've never been enthusiastic about the AI revolution in software development.

However, this week, I started using [Zed.dev](https://zed.dev/) together with their AI assistant integration. I'm surprised by the experience and efficiency gains. I still have reservations, but it can be valuable allies in tackling complex programming challenges and streamlining routine tasks.

For example, a few days ago, I was working on a tool that needed to parse ClickHouse explain output\[^1\], an easy task requiring recursively transverse all the properties looking for some specific properties.

With a simple prompt, the model wrote a working version.

> Write code to extract all primary key granules from explain_result. An example of explain_result here:
>
> {...}

```
def extract_primary_key_granules(explain_result):
    def traverse(node):
        granules = []
        if isinstance(node, dict):
            if 'Indexes' in node:
                for index in node['Indexes']:
                    if index.get('Type') == 'PrimaryKey':
                        granules.append({
                            'Keys': index.get('Keys', []),
                            'Initial Granules': index.get('Initial Granules', 0),
                            'Selected Granules': index.get('Selected Granules', 0)
                        })
            for value in node.values():
                granules.extend(traverse(value))
        elif isinstance(node, list):
            for item in node:
                granules.extend(traverse(item))
        return granules

    return traverse(explain_result)

# Example usage:
result = extract_primary_key_granules(explain_result)
for granule in result:
    print(f"Keys: {granule['Keys']}")
    print(f"Initial Granules: {granule['Initial Granules']}")
    print(f"Selected Granules: {granule['Selected Granules']}")
    print()
```

The task was relatively easy, but I had to make changes and improve the initial code. Despite that, this saved me at least a few back-and-forth hours writing the function and running some tests to see if it worked as expected!

\[^1\]: This is how a [ClickHouse explain output](https://gist.githubusercontent.com/jrdi/5934c25ce11e003877e1f319096d7d87/raw/f447390e7234db5ad2015ae7301f474a86df7b56/clickhouse_explain.json) looks like

***

## Discussion

No replies yet.
