How does codex usually generate shared functions/modules?

Assume that functions A, B, C are drafted for a V1 of a project, and then the session is told to further amend to support additional functionalities as V2, V3, etc. In most cases, the signatures of function A, B, C might be modified, and additional functions like D, E might be written. However, as human programmer would derive shared functions/modules for different functionalities, i am curious instead of piling up functions and/or amending the signatures, what is the mechanism underneath codex that can ensure the capability of effective derivation of shared functions/modules, so that the project can be neat, easier to maintain and read?

In my experience it will do exactly that, create common functions that are used by others. If it doesn’t you can tell it to adhere to principles of code reuse, abstraction and inheritance

Aside from significant training, prompting is important here.

Some relevant prompts are here:

But you could add an AGENTS.md file to your project to emphasise this more if you need to, with e.g.:

### Architectural evolution and reuse

When implementing new functionality:

- Inspect existing implementations for opportunities to reuse or extend established abstractions.
- Consider whether new requirements reveal common functionality that should be extracted into shared functions, modules, or services.
- Prefer coherent refactoring over accumulating duplicated or narrowly specialised implementations.
- Avoid speculative abstractions, unnecessary indirection, and premature generalisation.
- Preserve existing behaviour and validate refactoring through appropriate tests.
- Treat maintainability and architectural coherence as ongoing requirements, not separate cleanup tasks.

But I agree with @platypus to an extent, I’ve not found vanilla codex particularly egregious in this space …

However, it is worth auditing this from time to time.

In fact this discussion has prompted me to work on a de-duplication PR for one of my projects :sweat_smile: …

As a data point in this discussion, here’s my “de-duplication” PR.

There’s a nice table in it which suggests Codex hadn’t been doing a bad job.

The wins were pretty small …

UPDATE:

So I took this quite a bit further :sweat_smile:

Codexometer now has a duplication detector in CI that ratchets a baseline and blocks a PR if there is degradation.

This uses a respected deterministic duplication detector tool.

So yeah, you can control for this.