Making code maintainable and readable

How can I make code more maintainable? I come from a Java background, where there is Javadoc, which allows for documentation to be made quickly.

Are there also any good code formatters?

Is there something similar for C++? Also where could I find good naming conventions and best practice for C++ code. As I have knowledge of this in Java but not in C++.

I personally follow this:
https://www.gnu.org/prep/standards/
Make sure that you follow the best pactices:
https://github.com/lefticus/cppbestpractices/blob/master/00-Table_of_Contents.md
https://isocpp.org/

Also read about "Clean Code"
some c++ shops use javadocs or similar to generate docs from the structured comments. I have forgotten the name, doxygen maybe, of the c++ go-to from years back. Either way, yes, there are comment to document tools for c++, several flavors.

everyone and their brother has a code formatter. The key is following the standards at your employer or for the project (opensource etc) or your own favorite (personal coding). Visual studio had one built in, as does notepad++ as a plugin, and there are thousands of them online (if you trust dumping code to an unknown site that could borrow it), and its honestly not too hard to write a basic one, a page or 2 of code and you can check, clean, and such the rules that matter to you; Ive written one for the specific rules used in-house before.

good naming conventions --- this can cause a 'holy war' ... you have the all caps constants guys, the hungarian guys, the camel case guys, and the underscore guys, and a couple of other flavors here and there as well. I am an all lower case underscore kind of guy, eg number_of_thingys.
There isnt any one standard because C and C++ predate the internet and coders did what their local group wanted done, which varied widely, and that background has persisted for decades esp at smaller places, schools, and so on. Some of it depends on your tools too; some tools will tell you what type a variable is by mouse over, so putting the type into the name of it is just clutter. Some tools do not have this, and those shops may opt to put the
type into the name like the old days.

Another holy war is whether you align {} pairs to the same tab stop or misalign them javascript/textbook style, eg
void foo(){ //textbook misaligned style
...
} //not aligned with above, see
vs
void foo()
{
..
} //visually aligned.

C++ also does not spoonfeed you. You do not have to have a class wrapper on everything, and you do not have to name the files the same as the class inside the file. None of that is enforced (and wrappers on things that do not need them is arguably usually bloat anyway).


The approach used by Java is fine for C++ for the most part. There may be one or two things that need to be cleaned up / thought about, like whether you need Us in unsigned types which Java forgot, things like that.

Last edited on
any good code formatters?

Visual Studio IDE has a MS customizable code formatter built in, Code::Blocks using the "Artistic Style" plugin.
http://astyle.sourceforge.net/

There are others available.
Topic archived. No new replies allowed.