Re: attack of silly coding standard?
On 08/12/2010 16:59, Yannick Tremblay wrote:
In article<45d42867-bfec-462d-9e3f-b77b275bf780@d24g2000prj.googlegroups.com>,
hanukas<jukka@liimatta.org> wrote:
SESE may help making the compiled machine code smaller. Example;
const char* getError(GLenum error)
{
switch (error)
{
case GL_BLA: return "bla";
case GL_FOO: return "foo";
// ... etc ...
}
// ... etc ...
}
YMMV, but often compilers replicate the return statement as many times
as you have case statements there. This will bloat the binary a lot.
There is a good mechanism to overcome that:
Arggh!
If the above code is causing your binary to bloat a lot you have either:
- Incredibly well optimised code
- A very different meaning of "a lot2"than I am used to.
const char* v;
switch (error)
{
case GL_BLA: v = "bla"; break;
// ... etc.
default: v = "unknown error"; break;
}
return v;
Also use ## in a nice macro to save some typing when converting error
into string. ;-)
This was just one example that came to mind (probably because this is
a common case). Multiple returns are more often less clear than single
return, but doesn't mean it isn't true from time to time.
Rules like that are a good thing when you have junior staff members.
They will eventually learn what time it is and make good design calls
based on experience; when you have a good reason to have many exits,
by all means do it.
I think this is a very fundamental thing. I have authored coding
guidelines that contains some items that I do not believe should be
rules for experienced programmers. However, coding guidelines are at
their most useful as a set of reminders to more junior staff members
steering them towards best practices.
So for example, although I do not beleive that there exists a number
of lines per functions that is the threshold between a bad and a good
function, in some evironment, it can be worthwhile to place a rule
that say that all "all functions must be less than XX lines unless a
good explanation is provided".
Why provide a good explanation? Functions that contain switch
statements are often long and require no explanation. The following
function of mine is over 100 lines long and requires no explanation:
script::script(program& aProgram) : instance(Instance, aProgram),
iCompiling(false), iPass(1), iPuretime(false), iState(ParsingNamespace),
iTypeOk(false)
{
add_namespace(scope::Global, L"");
add_namespace(scope::Namespace, L"system");
add_namespace(scope::Namespace, L"text");
add_namespace(scope::Namespace, L"math");
add_namespace(scope::Namespace, L"time");
add_namespace(scope::Namespace, L"stdio");
add_namespace(scope::Namespace, L"gui");
add_namespace(scope::Namespace, L"media");
add_namespace(scope::Namespace, L"irc");
specific_namespace(L"system").symbol_table().insert(L"wait",
value(FunctionDefinition, function_ptr(new wait_1())));
specific_namespace(L"system").symbol_table().insert(L"wait",
value(FunctionDefinition, function_ptr(new wait_2())));
specific_namespace(L"system").symbol_table().insert(L"create_thread",
value(FunctionDefinition, function_ptr(new create_thread_1())));
specific_namespace(L"system").symbol_table().insert(L"create_thread",
value(FunctionDefinition, function_ptr(new create_thread_2())));
specific_namespace(L"system").symbol_table().insert(L"send",
value(FunctionDefinition, function_ptr(new send_object_1())));
specific_namespace(L"system").symbol_table().insert(L"send",
value(FunctionDefinition, function_ptr(new send_object_2())));
specific_namespace(L"system").symbol_table().insert(L"post",
value(FunctionDefinition, function_ptr(new post_object_1())));
specific_namespace(L"system").symbol_table().insert(L"post",
value(FunctionDefinition, function_ptr(new post_object_2())));
specific_namespace(L"system").symbol_table().insert(L"available",
value(FunctionDefinition, function_ptr(new available())));
specific_namespace(L"system").symbol_table().insert(L"which",
value(FunctionDefinition, function_ptr(new which())));
specific_namespace(L"system").symbol_table().insert(L"get",
value(FunctionDefinition, function_ptr(new get())));
specific_namespace(L"system").symbol_table().insert(L"open",
value(FunctionDefinition, function_ptr(new resource::file_open())));
specific_namespace(L"system").symbol_table().insert(L"read",
value(FunctionDefinition, function_ptr(new resource::file_read_1())));
specific_namespace(L"system").symbol_table().insert(L"read",
value(FunctionDefinition, function_ptr(new resource::file_read_2())));
specific_namespace(L"system").symbol_table().insert(L"read_line",
value(FunctionDefinition, function_ptr(new resource::file_read_line())));
specific_namespace(L"system").symbol_table().insert(L"write",
value(FunctionDefinition, function_ptr(new resource::file_write_1())));
specific_namespace(L"system").symbol_table().insert(L"write",
value(FunctionDefinition, function_ptr(new resource::file_write_2())));
specific_namespace(L"system").symbol_table().insert(L"write_line",
value(FunctionDefinition, function_ptr(new resource::file_write_line())));
specific_namespace(L"system").symbol_table().insert(L"seek_read",
value(FunctionDefinition, function_ptr(new resource::file_seek_read_1())));
specific_namespace(L"system").symbol_table().insert(L"seek_read",
value(FunctionDefinition, function_ptr(new resource::file_seek_read_2())));
specific_namespace(L"system").symbol_table().insert(L"tell_read",
value(FunctionDefinition, function_ptr(new resource::file_tell_read())));
specific_namespace(L"system").symbol_table().insert(L"seek_write",
value(FunctionDefinition, function_ptr(new resource::file_seek_write_1())));
specific_namespace(L"system").symbol_table().insert(L"seek_write",
value(FunctionDefinition, function_ptr(new resource::file_seek_write_2())));
specific_namespace(L"system").symbol_table().insert(L"tell_write",
value(FunctionDefinition, function_ptr(new resource::file_tell_write())));
specific_namespace(L"system").symbol_table().insert(L"is_good",
value(FunctionDefinition, function_ptr(new resource::file_is_good())));
specific_namespace(L"system").symbol_table().insert(L"close",
value(FunctionDefinition, function_ptr(new resource::file_close())));
specific_namespace(L"text").symbol_table().insert(L"tokens",
value(FunctionDefinition, function_ptr(new tokens())));
specific_namespace(L"text").symbol_table().insert(L"to_utf8",
value(FunctionDefinition, function_ptr(new to_utf8())));
specific_namespace(L"text").symbol_table().insert(L"from_utf8",
value(FunctionDefinition, function_ptr(new from_utf8())));
specific_namespace(L"math").symbol_table().insert(L"set_precision",
value(FunctionDefinition, function_ptr(new ::set_precision())));
specific_namespace(L"math").symbol_table().insert(L"set_fixed",
value(FunctionDefinition, function_ptr(new ::set_fixed())));
specific_namespace(L"math").symbol_table().insert(L"set_scientific",
value(FunctionDefinition, function_ptr(new set_scientific())));
specific_namespace(L"math").symbol_table().insert(L"hex",
value(FunctionDefinition, function_ptr(new hex())));
specific_namespace(L"math").symbol_table().insert(L"oct",
value(FunctionDefinition, function_ptr(new oct())));
specific_namespace(L"math").symbol_table().insert(L"from_hex",
value(FunctionDefinition, function_ptr(new from_hex())));
specific_namespace(L"math").symbol_table().insert(L"from_oct",
value(FunctionDefinition, function_ptr(new from_oct())));
specific_namespace(L"stdio").symbol_table().insert(L"print",
value(FunctionDefinition, function_ptr(new print())));
specific_namespace(L"stdio").symbol_table().insert(L"print_line",
value(FunctionDefinition, function_ptr(new print_line())));
specific_namespace(L"stdio").symbol_table().insert(L"input",
value(FunctionDefinition, function_ptr(new input())));
specific_namespace(L"stdio").symbol_table().insert(L"input_line",
value(FunctionDefinition, function_ptr(new input_line())));
specific_namespace(L"stdio").symbol_table().insert(L"ignore",
value(FunctionDefinition, function_ptr(new ignore())));
specific_namespace(L"gui").symbol_table().insert(L"process_messages",
value(FunctionDefinition, function_ptr(new process_messages())));
specific_namespace(L"gui").symbol_table().insert(L"next_message",
value(FunctionDefinition, function_ptr(new next_message_1())));
specific_namespace(L"gui").symbol_table().insert(L"next_message",
value(FunctionDefinition, function_ptr(new next_message_2())));
specific_namespace(L"gui").symbol_table().insert(L"__message_box",
value(FunctionDefinition, function_ptr(new message_box_1())));
specific_namespace(L"gui").symbol_table().insert(L"__message_box",
value(FunctionDefinition, function_ptr(new message_box_2())));
specific_namespace(L"gui").symbol_table().insert(L"create_window",
value(FunctionDefinition, function_ptr(new resource::create_window())));
specific_namespace(L"gui").symbol_table().insert(L"destroy_window",
value(FunctionDefinition, function_ptr(new resource::destroy_window())));
specific_namespace(L"gui").symbol_table().insert(L"register_message_procedure", value(FunctionDefinition, function_ptr(new resource::register_message_procedure())));
specific_namespace(L"gui").symbol_table().insert(L"resize_client_area",
value(FunctionDefinition, function_ptr(new
resource::resize_client_area())));
specific_namespace(L"gui").symbol_table().insert(L"create_canvas",
value(FunctionDefinition, function_ptr(new resource::create_canvas())));
specific_namespace(L"gui").symbol_table().insert(L"plot",
value(FunctionDefinition, function_ptr(new resource::plot())));
specific_namespace(L"gui").symbol_table().insert(L"clear",
value(FunctionDefinition, function_ptr(new resource::clear())));
specific_namespace(L"media").symbol_table().insert(L"play_sound",
value(FunctionDefinition, function_ptr(new play_sound())));
specific_namespace(L"math").symbol_table().insert(L"random",
value(FunctionDefinition, function_ptr(new random_0())));
specific_namespace(L"math").symbol_table().insert(L"random",
value(FunctionDefinition, function_ptr(new random_1())));
specific_namespace(L"math").symbol_table().insert(L"random",
value(FunctionDefinition, function_ptr(new random_2())));
specific_namespace(L"math").symbol_table().insert(L"seed",
value(FunctionDefinition, function_ptr(new seed_())));
specific_namespace(L"time").symbol_table().insert(L"time",
value(FunctionDefinition, function_ptr(new time_())));
specific_namespace(L"time").symbol_table().insert(L"uptime",
value(FunctionDefinition, function_ptr(new uptime())));
specific_namespace(L"math").symbol_table().insert(L"abs",
value(FunctionDefinition, function_ptr(new external_function_1<int,
int>(L"abs", &abs))));
specific_namespace(L"math").symbol_table().insert(L"acos",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"acos", &acos))));
specific_namespace(L"math").symbol_table().insert(L"asin",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"asin", &asin))));
specific_namespace(L"math").symbol_table().insert(L"atan",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"atan", &atan))));
specific_namespace(L"math").symbol_table().insert(L"atan2",
value(FunctionDefinition, function_ptr(new external_function_2<double,
double, double>(L"atan2", &atan2))));
specific_namespace(L"math").symbol_table().insert(L"cos",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"cos", &cos))));
specific_namespace(L"math").symbol_table().insert(L"cosh",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"cosh", &cosh))));
specific_namespace(L"math").symbol_table().insert(L"exp",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"exp", &exp))));
specific_namespace(L"math").symbol_table().insert(L"fabs",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"fabs", &fabs))));
specific_namespace(L"math").symbol_table().insert(L"fmod",
value(FunctionDefinition, function_ptr(new external_function_2<double,
double, double>(L"fmod", &fmod))));
specific_namespace(L"math").symbol_table().insert(L"log",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"log", &log))));
specific_namespace(L"math").symbol_table().insert(L"log10",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"log10", &log10))));
specific_namespace(L"math").symbol_table().insert(L"pow",
value(FunctionDefinition, function_ptr(new external_function_2<double,
double, double>(L"pow", &pow))));
specific_namespace(L"math").symbol_table().insert(L"sin",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"sin", &sin))));
specific_namespace(L"math").symbol_table().insert(L"sinh",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"sinh", &sinh))));
specific_namespace(L"math").symbol_table().insert(L"tan",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"tan", &tan))));
specific_namespace(L"math").symbol_table().insert(L"tanh",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"tanh", &tanh))));
specific_namespace(L"math").symbol_table().insert(L"sqrt",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"sqrt", &sqrt))));
specific_namespace(L"math").symbol_table().insert(L"ceil",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"ceil", &ceil))));
specific_namespace(L"math").symbol_table().insert(L"floor",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"floor", &floor))));
specific_namespace(L"math").symbol_table().insert(L"frexp",
value(FunctionDefinition, function_ptr(new frexp_())));
specific_namespace(L"math").symbol_table().insert(L"hypot",
value(FunctionDefinition, function_ptr(new external_function_2<double,
double, double>(L"hypot", &_hypot))));
specific_namespace(L"math").symbol_table().insert(L"_j0",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"_j0", &_j0))));
specific_namespace(L"math").symbol_table().insert(L"_j1",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"_j1", &_j1))));
specific_namespace(L"math").symbol_table().insert(L"_jn",
value(FunctionDefinition, function_ptr(new external_function_2<double,
int, double>(L"_jn", &_jn))));
specific_namespace(L"math").symbol_table().insert(L"ldexp",
value(FunctionDefinition, function_ptr(new external_function_2<double,
double, int>(L"ldexp", &ldexp))));
specific_namespace(L"math").symbol_table().insert(L"floor",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"floor", &floor))));
specific_namespace(L"math").symbol_table().insert(L"_y0",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"_y0", &_y0))));
specific_namespace(L"math").symbol_table().insert(L"_y1",
value(FunctionDefinition, function_ptr(new external_function_1<double,
double>(L"_y1", &_y1))));
specific_namespace(L"math").symbol_table().insert(L"_yn",
value(FunctionDefinition, function_ptr(new external_function_2<double,
int, double>(L"_yn", &_yn))));
specific_namespace(L"math").symbol_table().insert(L"modf",
value(FunctionDefinition, function_ptr(new modf_())));
specific_namespace(L"irc").symbol_table().insert(L"message_create",
value(FunctionDefinition, function_ptr(new
resource::irc::message_create())));
specific_namespace(L"irc").symbol_table().insert(L"message_id",
value(FunctionDefinition, function_ptr(new resource::irc::message_id())));
specific_namespace(L"irc").symbol_table().insert(L"message_time",
value(FunctionDefinition, function_ptr(new resource::irc::message_time())));
specific_namespace(L"irc").symbol_table().insert(L"message_is_ctcp",
value(FunctionDefinition, function_ptr(new
resource::irc::message_is_ctcp())));
specific_namespace(L"irc").symbol_table().insert(L"message_direction",
value(FunctionDefinition, function_ptr(new
resource::irc::message_direction())));
specific_namespace(L"irc").symbol_table().insert(L"message_origin",
value(FunctionDefinition, function_ptr(new
resource::irc::message_origin())));
specific_namespace(L"irc").symbol_table().insert(L"message_command",
value(FunctionDefinition, function_ptr(new
resource::irc::message_command())));
specific_namespace(L"irc").symbol_table().insert(L"message_command_as_string", value(FunctionDefinition, function_ptr(new resource::irc::message_command_as_string())));
specific_namespace(L"irc").symbol_table().insert(L"message_target",
value(FunctionDefinition, function_ptr(new
resource::irc::message_target())));
specific_namespace(L"irc").symbol_table().insert(L"message_parameter_count", value(FunctionDefinition, function_ptr(new resource::irc::message_parameter_count())));
specific_namespace(L"irc").symbol_table().insert(L"message_parameter",
value(FunctionDefinition, function_ptr(new
resource::irc::message_parameter())));
specific_namespace(L"irc").symbol_table().insert(L"message_set_direction", value(FunctionDefinition, function_ptr(new resource::irc::message_set_direction())));
specific_namespace(L"irc").symbol_table().insert(L"message_set_origin",
value(FunctionDefinition, function_ptr(new
resource::irc::message_set_origin())));
specific_namespace(L"irc").symbol_table().insert(L"message_set_command", value(FunctionDefinition, function_ptr(new resource::irc::message_set_command_1())));
specific_namespace(L"irc").symbol_table().insert(L"message_set_command", value(FunctionDefinition, function_ptr(new resource::irc::message_set_command_2())));
specific_namespace(L"irc").symbol_table().insert(L"message_add_parameter", value(FunctionDefinition, function_ptr(new resource::irc::message_add_parameter())));
specific_namespace(L"irc").symbol_table().insert(L"buffer_type",
value(FunctionDefinition, function_ptr(new resource::irc::buffer_type())));
specific_namespace(L"irc").symbol_table().insert(L"buffer_name",
value(FunctionDefinition, function_ptr(new resource::irc::buffer_name())));
specific_namespace(L"irc").symbol_table().insert(L"buffer_send_message", value(FunctionDefinition, function_ptr(new resource::irc::buffer_send_message())));
specific_namespace(L"irc").symbol_table().insert(L"buffer_send_command", value(FunctionDefinition, function_ptr(new resource::irc::buffer_send_command())));
specific_namespace(L"irc").symbol_table().insert(L"connection_name",
value(FunctionDefinition, function_ptr(new
resource::irc::connection_name())));
specific_namespace(L"irc").symbol_table().insert(L"connection_server_buffer", value(FunctionDefinition, function_ptr(new resource::irc::connection_server_buffer())));
specific_namespace(L"irc").symbol_table().insert(L"connection_buffer_from_name", value(FunctionDefinition, function_ptr(new resource::irc::connection_buffer_from_name())));
specific_namespace(L"irc").symbol_table().insert(L"connection_send_message", value(FunctionDefinition, function_ptr(new resource::irc::connection_send_message())));
specific_namespace(L"irc").symbol_table().insert(L"connection_send_command", value(FunctionDefinition, function_ptr(new resource::irc::connection_send_command())));
specific_namespace(L"irc").symbol_table().insert(L"connection_receive_message", value(FunctionDefinition, function_ptr(new resource::irc::connection_receive_message())));
specific_namespace(L"irc").symbol_table().insert(L"create_event_handler", value(FunctionDefinition, function_ptr(new resource::irc::create_event_handler())));
}
/Leigh