Code Formatting - Need Help C++

Hey all I need some help. I am new and still learning. I have a curious question on code formatting:

Should I put braces like this when there is a lot of code statements even if it is not part of the readprocessmemory function? It is just the next line of code. Cause when no brackets it is bunched up. Code below will show example.

1
2
3
4
5
6
7
Readprocessmemory(hProcess, LPVOID bla bla bla);
{
cout << “Display text here” << endl;
cout << “Another text here” << endl;
Do more stuff here...
cin.get();
}




Thanks hope y’all can help...
Last edited on
styles vary quite a bit. I personally say that you should only add 'extra' brackets for programmatic reasons, for example if you declare a variable inside the brackets, it will destroy outside them, and sometimes that is useful with classes to invoke a destructor or to dispose of large amounts of temporary memory (hopefully you don't do this much, though). That said, extra brackets do not generally hurt so long as you are aware of their implications to code (mostly, the scope thing). The trouble with extra brackets (apart from the scope) is they mess up automatic indent tools that attempt to style the code for you, and they can confuse the reader if used randomly (like anything, if you DO go this route, be very consistent).

I would code this with some white space instead, like this (you can decide for yourself where line breaks make the most sense, the example isnt useful for decision making here).

1
2
3
4
5
6
7
8
Readprocessmemory(hProcess, LPVOID bla bla bla);

cout << “Display text here” << endl;
cout << “Another text here” << endl;

Do more stuff here();

cin.get();


also as a rule of thumb you want to group things in functions when the code becomes 'messy'.
so if you had 20 cout statements, just wrap them in a function to clean it up, grouping them all together in one place. I know its just and example but if you had redundant text like "variable text here" ... a function reduces this to 1 line instead of 2 hard coded lines, and for large amounts of spew this really helps clean it up; there are numerous ways to deal with bulk text in code if you have it.
Last edited on
I assume this is not on the global scope (obviously, but the lack of spacing, and the weird LPVOID makes it look like it in first glance)

Generally people use a scope to create variables that people want to recreate to avoid redefinition errors, I see these used a lot in automatic test suits.

It is also useful for controlling the scope of a mutex wrapper (as in, std::lock_guard<std::mutex> lk(mt);), or other objects lifetimes.
Topic archived. No new replies allowed.