coding style - c++ curly braces and comments -


c++ begginer here. i'm struggling bit what's best practice curly braces + //comments.

i see functions comments above definition provide visual studio inspection utility hovering mouse on them anywhere. when comes if statements , sorts, can't figure out best more helpful in upcoming projects

so, between

if (condition) {  // comment     something(); } 

or

// comment if (condition) {       something(); } 

or

if (condition) // comment {       something(); } 

or following 1 (to use useless newline)

if (condition) {  // comment     something(); } 

there no clear "ah 1 better because of xyz", me yet.

thank foresight!

cheers

this example may handled differently depending on whether comment applied condition or bock. first case typically indicates condition may rather complex , make sense refactor separate variable or separate method proper naming comment (if still necessary) applied variable or method. second case typically indicates doing complex in block , make sense refactor block separate method proper naming comment (if still necessary again) applied method. notice introduction of separate entities proper names removes need comment.

as curly braces there no common approach, can encounter kind of crazy braces placement. people defend such pluralism. prefer place matching braces aligned - either horizontally (that on same line) or vertically (that same indentation) when content not fit 1 line. , rule applied braces, not curly.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -