K&R vs Allman: Code Formatting Styles

Code formatting helps make programs more readable and easier to maintain. Two common styles are K&R (Kernighan & Ritchie) and Allman.

In K&R style, the opening curly brace { appears on the same line as the statement (e.g., if or else). Thi…


This content originally appeared on DEV Community and was authored by Ben Santora

Code formatting helps make programs more readable and easier to maintain. Two common styles are K&R (Kernighan & Ritchie) and Allman.

In K&R style, the opening curly brace { appears on the same line as the statement (e.g., if or else). This keeps the code compact, using fewer lines.

K&R

Image description

Allman style places the opening brace on a new line. Though it takes more vertical space, it makes it easier to see where each code block starts and ends, improving readability for some.

Allman

Image description

The choice between K&R and Allman often depends on personal or team preferences. I learned using the K&R method and it seems much more common. But I found when trying to learn nested code blocks like the if-else-if-else statements in the C program shown above, that the Allman style helped me better see what was happening - ie - when I'm LEARNING a program, trying to understand the algorithm, etc. - I'll often write out the code in the Allman format - I think it really does help, especially for beginners. You can always default back to K&R once you've grasped what's going on.

Ben Santora - October 2024


This content originally appeared on DEV Community and was authored by Ben Santora


Print Share Comment Cite Upload Translate Updates
APA

Ben Santora | Sciencx (2024-10-24T19:26:48+00:00) K&R vs Allman: Code Formatting Styles. Retrieved from https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/

MLA
" » K&R vs Allman: Code Formatting Styles." Ben Santora | Sciencx - Thursday October 24, 2024, https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/
HARVARD
Ben Santora | Sciencx Thursday October 24, 2024 » K&R vs Allman: Code Formatting Styles., viewed ,<https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/>
VANCOUVER
Ben Santora | Sciencx - » K&R vs Allman: Code Formatting Styles. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/
CHICAGO
" » K&R vs Allman: Code Formatting Styles." Ben Santora | Sciencx - Accessed . https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/
IEEE
" » K&R vs Allman: Code Formatting Styles." Ben Santora | Sciencx [Online]. Available: https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/. [Accessed: ]
rf:citation
» K&R vs Allman: Code Formatting Styles | Ben Santora | Sciencx | https://www.scien.cx/2024/10/24/kr-vs-allman-code-formatting-styles/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.