Using the C-preprocessor to replace curly braces
In my article I argued that curly braces neither enchanced clarity nor reduced keystrokes to point out that whatever you care about curcly braces is not an advantage.
As to why I like a number of languages with curly braces. I don't like them because of curly braces, I like them despite curly braces.
I quite like Go, Swift, C and Zig. However the curly braces is not something I think adds to their appeal. Quite the opposite.
I think the C pre-processor should be used as little as possble. That is one of the advantages of Zig, that it ditches the pre-processor. It just does simple text substitution with no understanding of the language, which is a receipt for problems.
But perhaps more importantly the C-language has been specifically designed for use with curly braces. A simple text substitution cannot easily fix that. Consider transforming this:
if (x > 10) {
printf("Larger than x\n");
}
else if (x == 10) {
printf("Equal!");
}
else {
printf("Smaller");
}
Now I am going to replace all the braces with begin
and end
:
if (x > 10) begin
printf("Larger than x\n");
end
else if (x == 10) {
printf("Equal!");
end
else begin
printf("Smaller");
end
How does that look? It looks terrible. C was not meant to be used that way. And I would say that applies to most curly-brace languages. You cannot to a simple 1–1 substitution. You have to actually design the language around not using curly braces, the way Julia, Lua and other languages have been made. I seem to recall Ruby also making curly braces optional but they made the alternative forms actually nice to use.