r/cpp • • Jul 05 '26

Formatting vs Architecture: How formatters are erasing visual cues and hurting codebases

I’m preparing a presentation on how automatic formatters can actually ruin code management over time. This relates heavily to the ongoing discussions about C++ memory safety and why some people see it as a critical issue while others don't get the fuss since basic memory allocation isn't that hard.

Memory issues are usually just a symptom of architectural failure, not a lack of developer skill in handling allocations. A messy architecture turns minor oversights into catastrophes. That's when hidden memory leaks or memory corruption become critical safety flaws and crashes.

I often think about the (nowadays) hated Hungarian Notation. Love it or hate it, it made developers extremely efficient because it provided immediate visual cues. Developers who actively care about the visual shape of text think completely differently about code structure. If you look at Charles Simonyi's other work, it’s clear how much focus he had on architecture.

Formatters can be an absolute disaster for architecture. It has become a religion in development teams that everything must be run through Prettier or Clang-Format to the letter. But the price we pay is that formatters erase all opportunities for visual cues regarding architecture and layers.

Architecture is WAY more important than formatting.

Code is text. By deliberately allowing flexibility in how to format, we can highlight major patterns and make code much easier to reason about. When a formatter forces all code through the same rigid, mechanical template, all code looks identical. The architecture becomes invisible. Ironically, this causes codebases to rot because developers in the team can no longer see where the architectural boundaries actually are.

Formatting in itself isn't bad, but it must be a formatting style that elevates the architecture and allows for human semantic grouping. The opposite is dangerous.

As far as I know, there is no formatter out there today that is anywhere near capable of handling the flexibility required to actually make architecture visible in code.

What are your thoughts on this? Have formatters made us blind to the actual structure and layer belonging of our code?

EDIT

To those of you who only see problems with this approach: what is your actual alternative for managing cognitive load at scale?

The common stance seems to be "just write code however you want, as long as the automatic formatter makes the layout look consistent."

But formatting only fixes cosmetic consistency, it doesn't fix structure, architectural boundaries, or intent. If you believe that naming conventions, prefixes, and semantic text layouts are useless, you are essentially advocating for visual anarchy disguised as "clean text". How does a mechanical linter help a new developer navigate architectural layers in a large codebase if the code itself provides zero visual cues about where the boundaries go?

0 Upvotes

130 comments sorted by

View all comments

1

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 09 '26

I'm the one that pushed for auto formatters in our code base. As such, those few developers that can't handle change came to me with every example where formatting made the code worse to read.

Out of that I learned:

  • no one mentioned a link between architecture and formatting, they only cared about the number of lines and alignment of variables
  • whenever formatting shows you something horrible, it's because the code was horrible
  • people can't format consistently
  • code structure is much better readable after formatting

Id really would like to see snippets where you think formatting breaks architecture, as it really helps to talk about specific code rather than abstract concepts.

Hungarian notation is being disliked by many for a reason. It's forcing implementation details in the name of your variables. I really don't care if a "name" is a char ptr, string view, string or any other type. To understand the code, as a reader, I want to know what the purpose is. Any place where that difference between types is relevant should not influence architecture of the code.

1

u/gosh Jul 09 '26

Some samples

make this work with formaters are impossible https://github.com/perghosh/Data-oriented-design/blob/092b6ff28693f45ca8f378841e1c472c1d4d4442/external/gd/gd_arguments_shared.cpp#L1462
Facade object: https://github.com/perghosh/Data-oriented-design/blob/092b6ff28693f45ca8f378841e1c472c1d4d4442/target/server/http/Application.h#L42

Also if code is divided in levels or

Hungarian notation is being disliked by many for a reason. It's forcing implementation details in the name of your variables.

This is wrong, I think you have read wikipedia or some similar and the description there is not correct.

Hungarian is much more that some selected abbreviations that each team select. There is no standard. So many have not understood that that "apps-" and system-hungarian was just what Microsoft selected for their code base. It is not something other team should select.

Hungarian looks differently for each team that use it.

3

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 09 '26

I think we have the same understanding of Hungarian notation. You encode information about the type in your variable name with some prefix. Whatever the actual prefix is doesn't matter for me. I'm also used to having m_ for members, which isn't part of Hungarian (although regularly used together, causing confusion), such that semantics are exposed. Though let's ignore the naming. If you feel better having a p in front of every pointer, just make sure clang tidy is configured correctly, or any other tool that can do this.

I read through your code. In the first example, you have several statements followed by an assertion on the same line, just falling off my small screen. You might think this is good design, though it's surprising to anyone not used to the pattern. The pattern also only works for post-conditions for single statements. I'd say it's good that a formatter would be preventing this. Though more importantly, I don't consider this part of architecture.

In the same example, there are several remarks to be made about architecture, like having string and size being 2 separate parameters. I see you already constructing string_view in the function. At least, use a struct for putting the 2 together, such that you have less technical dependencies between parameters. Similarly, you have a value and a type. Also this should be abstracted in a separate type.

The use of align32_g (which also works with 64 integers?) looks also a bit strange to me and distracts from what you try to code. At least one of the functions has a tag to request the aligned value. However, even better would be having a type that knows about the alignment. I'd also wonder if the code is correct, as the new size is not aligned.

Later in the function, you are packing multiple pieces of information in a single integral value. I would expect a class (template) that abstracts this packing. Then it can also assert that all bits expected to be 0, are actually 0. It would prevent magical values like 24 for the shifting.

Then, you are constructing some kind of pstring. I would also expect that in its own type.

Given the remarks above, I believe formatting to be the least of your concern when it comes to architecture/design. If you would submit this for code review with me, I'd send you back to rework.

1

u/gosh Jul 09 '26

Can you find a better search tool compared to this?
https://github.com/perghosh/Data-oriented-design/releases/tag/cleaner.1.1.3

Or what webserver is better than this? https://github.com/perghosh/Data-oriented-design/tree/main/target/server/http

Even if you don't think the code is good it is very very effective for producing functionality

How do you write or format code when the domain is so advanced that the developers writing the code cant learn it?

1

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 10 '26

You are fully ignoring all the feedback I gave you. Given that I haven't heard of any of these products, I suspect better exist. For webservers, I've heard about https://github.com/crowcpp/crow which I believe is using clang-format.

As someone working in such a complex domain, I can tell that you want to remove as many distractions as possible. You want your code to focus on the domain concepts and remove technical complexity, like the suggestions I made previously. Also having formatting and linting in place helps, such that devs can focus on the functionality and not the technicalities. Beside that: tests, tests and tests, as you don't want to break functionality you didn't knew existed.

1

u/gosh Jul 10 '26 edited Jul 10 '26

I did not, the point with my links was to show that with your thinking you are limited in what you can do. Advanced code need better solutions.

For me readability is not prioritised. Beeing able to debug the code is a lot more important and also beeing able to search for code.

Method names are also standardised, you probably know how stl use naming, if you know vector you know the rest of the containers because of same names.

Hungarian is all about reduce cognitive load.

If I write code that is only there for debug then I want to have that in a different style, it should be clear that this code is only for debugging.

1

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 11 '26

I find it strange that you don't find readability a prerequisite for debugability. If you can't read the code in a easy way, how are you going to debug efficiently? Readability is all about the cognitive load. The harder it is to read, the more load you need for it.

I don't see how searchability becomes worse due to formatting, unless you only use text/regex search. I'm using clangd in vscode, which perfectly allows me to search whatever I need. The results of that are much more reliable than regex.

I'm open for your argument of cognitive load. It's exactly why I would like to run the clang modernizer checks on our code base. However, you are missing a piece of the puzzle here. Something I also only learned/accepted after years in always-auto. If your types are well-designed, the details don't matter.

Let me come back to a previous example. You are packing 2 (or more) numbers in a single integer field. You seem under the impression that having these bit operations in your function is important. This however is exactly the thing stealing your attention. If you have a decent type for it, it doesn't matter how it's stored. It can be a packed integer or a pair of integers. It will have assertions/contracts to check all preconditions. If you have such a common type, you are also much more inclined to write assertions. A debug visualizer on that type can help debugging much more than seeing the operations. And your code becomes: "store this value". Much easier to understand, making the cognitive load less and the code more reliable and testable. Instead of dealing with details, you can now focus on the flow of your program. This makes debugging much easier.

This holds for about everything in your Hungarian prefix. Make sure you have a good/clear type and it doesn't matter. Wether something is a pointer of not, will be clear from using it with ., * and ->. Though it doesn't matter in the flow of your function. It only matters when changing your code, where your LSP or compiler will tell you if you did it wrong.

Let me end by circling back to what I said before: you have bigger problems with architecture/design than formatting. I'm quite sure I won't be able to convince you right now of that. Though I do hope you understand my reasoning, even if you disagree.

1

u/gosh Jul 11 '26

I find it strange that you don't find readability a prerequisite for debugability. If you can't read the code in a easy way, how are you going to debug efficiently? Readability is all about the cognitive load. The harder it is to read, the more load you need for it.

What exactly do you mean when you say "readability"? Do you read source code the same way you read a novel like in a book, or how do you actually make code readable?

If we use the book analogy: a book relies on chapters, headings, capital letters, periods, and paragraph breaks. Why do you think these exist? They don't change the actual meaning of the text itself, but they exist solely to help the reader organize and navigate the information. They are structural anchors designed to reduce the cognitive load.

Without chapters, how are you supposed to scan or search the text effectively? Without an index at the back, how do you find specific information in a massive volume?

Even something as simple as a book requires multiple layers of formatting and structural meta-data just to make the text manageable. These structural elements have a completely different formatting than the body text, because their job isn't just to be "read"—their job is to provide architecture to the information.

1

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 12 '26

Taking your book example. Would you be able to get info from the book when all titles would be using the same font and size? It's due to the formatting of H1, H2 ... that you are able to grasp what the purpose of the headers is. Though even if that is consistent, how distracting is it for the text below it to have paragraphs left, right, middle and justify-aligned? How distracting is it if image descriptions would be using text above and below, sometimes in bold otherwise in italic. How about using different sizes of whitespace between the different lines, or chapter titles at the end of a page.

If anything, books are an extremely well example of why formatting is important. It's that way such that it doesn't distract you while reading. And yes, titles are important as well. If either the structure or the formatting is broken/missing, the book becomes much harder to read. They don't conflict, they augment each other.

Just like books, C++ has constructs for the structure: namespaces, classes, functions. It has a separation between headers and sources such that details can be hidden.

Mapping this back to the example of the multiple values. Your function is like a chapter in the book. The code you have is all the text in paragraphs right below it. By adding the abstractions, you reduce your text into 1 or 2 paragraphs. Very consicely giving the message that you want to bring with references to the details that follow later (or have been discussed in previous chapters). Than you have a subchapter per abstraction, which describes in detail how specific elements are implemented.

With this, you can read your index, find the chapters of relevance. Then you read the top paragraph(s) to see if everything connects as expected. This allows you to quickly eliminate irrelevant chapters. When you do observe something of relevance, you can deep dive in the subchapters mentioned.

At the same time, such a subchapter is much more focused as well. Where parts of the value packing was described in bits and pieces throughout the text and one has to search at many places to understand how the packing works, a subchapter can group the info, add extra context and clearly describe the contract of the piece of code.

1

u/gosh Jul 13 '26

If anything, books are an extremely well example of why formatting is important.

Yes, it is important but please tell me one single formatter that formats in a good way

→ More replies