Created
January 23, 2026 05:22
-
-
Save jneen/e12bf7f2c9ce53ba9cf9d169b09aff7a to your computer and use it in GitHub Desktop.
jneen's curly language mini style recommendation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * jneen's curly-brace language style guide! | |
| * applies to: c, c++, c#, java, javascript, etc | |
| * | |
| * does *not* apply to golang because of some fun choices | |
| * | |
| * can be modified to use c#-style bracing, but please, not for guards. | |
| * | |
| * if (cond) | |
| * { | |
| * return; | |
| * } | |
| * | |
| * is like 3 whole lines longer than it needs to be. | |
| */ | |
| // Point 1. Blocks should have curly braces if and only if they go on to the next line. | |
| // no | |
| if (cond) | |
| thing; | |
| // no, unless required by language | |
| if (cond) { thing; } | |
| // yes | |
| if (cond) thing; | |
| // yes | |
| if (cond) { | |
| thing1; | |
| thing2; | |
| } | |
| // Switch to multi-line curlies if either you need more than one statement, *or* the condition is very long. | |
| if (reallyVeryLongCondition.withLotsOfVerbiageAndArguments(longArgument1, longArgument2)) { | |
| return; | |
| } | |
| // Point 2. Curly openings at end of line, closings at start of line. again, can be modified | |
| // for c# style if necessary, but honestly i do it this way even in c#. | |
| foo(bar) { | |
| baz; | |
| } | |
| // i might be the only one who does this instead of `} else {`, but i think | |
| // else being its own clause makes it easier to scan. | |
| if (a) { | |
| b; | |
| c; | |
| } | |
| else { | |
| d; | |
| e; | |
| } | |
| // the next 2 are marginal, but can be useful in certain situations | |
| if (a) b; | |
| else c; | |
| if (a) b; | |
| else { | |
| c; | |
| } | |
| // Point 3. Prefer guards to nested if/else. Encourages factoring out helper methods, which is good to do anyways. | |
| // no | |
| if (a) { | |
| // body of function | |
| } | |
| // yes: guard style | |
| if (!a) return; | |
| // no | |
| if (a) { | |
| // body of function | |
| } | |
| else { | |
| handleError(...) | |
| } | |
| // yes: guard with special handling | |
| if (!a) { | |
| handleError(...); | |
| return; | |
| } | |
| // this is cool, easy to read, nicely laid out | |
| if (a) return 1; | |
| if (b) return 2; | |
| if (c) return 3; | |
| return 4; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment