Agree with (1) because short functions are easy to understand and less likely to have bugs.
Use a reasonable tab stop. Most people start to complain if the tab depth isn't at least 3, but
tab stops that are too wide mean a lot of line wrapping.
If I understand (3) it doesn't really matter. Perhaps for the sake of self-documenting code
you should limit the try block to only that which can throw, but I wouldn't go out of my way
to ensure that. ie, try { some_function_that_throws(); ++x; another_function_that_throws(); }
is fine even if ++x can't throw.
(4) Agree with Athar. C++ is designed for early exit, but it can require RAII to do correctly
(you don't want to have to write:
1 2 3 4
|
if( !some_function() ) { // failure case
do_a_ton_of_cleanup_here();
return;
}
|
Ideally just
1 2
|
if( !some_function() )
return;
|
and let destructors do the cleanup.
5) More generally, minimize logic, unless it is less understandable that way.
6) Agree, though if that means more code, I might break the rule. (What OP is saying is
that the coding technique used for the function should be like:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void foo() {
if( some_bad_condition_1 ) {
// error code here
}
// do work in success case
if( some_bad_condition_2 ) {
// error code here
}
// do more work in success case
}
|
That is, the "success" case of the function should lead you to the end of the function.
Or, said another way, all early exits are error cases.