Re: attack of silly coding standard?
On 08/12/2010 21:34, red floyd wrote:
On Dec 8, 12:42 pm, Leigh Johnston<le...@i42.co.uk> wrote:
On 08/12/2010 20:19, Keith H Duggar wrote:
On Dec 8, 12:05 pm, Leigh Johnston<le...@i42.co.uk> wrote:
On 08/12/2010 16:59, Yannick Tremblay wrote:
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:
[snip unbelievably naive and primitive mess]
Wow ... just wow ... that is a prime(evil) example of the horrific
code that poorly trained fanboys write when they lose all (or never
had any) rational perspective on code readability.
It's exactly the kind of stuff you see all over Micros$$t code so
I guess it's not so surprising in your specific case; but ... wow.
You are right about one thing though, no explanation should be
provided in this case. Instead the code should be instantaneously
rejected and the programmer sent to rehab.
KHD
There is nothing wrong with the constructor I posted. It contains no
branches (is therefore very simple) and all it does is construct objects
which is generally what constructors do. It is also perfectly readable.
and it would be much clearer if it was table driven. All that nasty
inline stuff could be replaced by a loop and a table.
Just to show that I am not immune to constructive criticsm (and to shut
the trolls up) I decided to rewrite the constructor so it uses a table
and luckily the use of templates made sure it wasn't too tedious:
namespace
{
const wchar_t* sPredefinedNamespaces[] =
{
L"system",
L"text",
L"math",
L"time",
L"stdio",
L"gui",
L"media",
L"irc"
};
struct predefined_function
{
const wchar_t* iNamespace;
const wchar_t* iName;
struct normal
{
normal(function* (*aCreator)()) : iCreator(aCreator) {}
function* (*iCreator)();
};
struct stdlib
{
template <typename Function>
stdlib(function* (*aCreator)(const std::wstring&, void*), const
wchar_t* aStdlibFunctionName, Function aStdlibFunction) :
iCreator(aCreator), iStdlibFunctionName(aStdlibFunctionName),
iStdlibFunction(aStdlibFunction) {}
function* (*iCreator)(const std::wstring&, void*);
const wchar_t* iStdlibFunctionName;
void* iStdlibFunction;
};
typedef lib::variant<normal, stdlib> type_t;
type_t iType;
};
template <typename T>
function* create_normal_function()
{
return new T();
}
template <typename T>
function* create_stdlib_function(const std::wstring& aName, void*
aFunction)
{
return new T(aName, reinterpret_cast<typename
T::function_type>(aFunction));
}
const predefined_function sPredefinedFunctions[] =
{
{ L"system", L"wait", create_normal_function<wait_1> },
{ L"system", L"wait", create_normal_function<wait_2> },
{ L"system", L"create_thread", create_normal_function<create_thread_1> },
{ L"system", L"create_thread", create_normal_function<create_thread_2> },
{ L"system", L"send", create_normal_function<send_object_1> },
{ L"system", L"send", create_normal_function<send_object_2> },
{ L"system", L"post", create_normal_function<post_object_1> },
{ L"system", L"post", create_normal_function<post_object_2> },
{ L"system", L"available", create_normal_function<available> },
{ L"system", L"which", create_normal_function<which> },
{ L"system", L"get", create_normal_function<get> },
{ L"system", L"open", create_normal_function<resource::file_open> },
{ L"system", L"read", create_normal_function<resource::file_read_1> },
{ L"system", L"read", create_normal_function<resource::file_read_2> },
{ L"system", L"read_line",
create_normal_function<resource::file_read_line> },
{ L"system", L"write", create_normal_function<resource::file_write_1> },
{ L"system", L"write", create_normal_function<resource::file_write_2> },
{ L"system", L"write_line",
create_normal_function<resource::file_write_line> },
{ L"system", L"seek_read",
create_normal_function<resource::file_seek_read_1> },
{ L"system", L"seek_read",
create_normal_function<resource::file_seek_read_2> },
{ L"system", L"tell_read",
create_normal_function<resource::file_tell_read> },
{ L"system", L"seek_write",
create_normal_function<resource::file_seek_write_1> },
{ L"system", L"seek_write",
create_normal_function<resource::file_seek_write_2> },
{ L"system", L"tell_write",
create_normal_function<resource::file_tell_write> },
{ L"system", L"is_good", create_normal_function<resource::file_is_good> },
{ L"system", L"close", create_normal_function<resource::file_close> },
{ L"text", L"tokens", create_normal_function<tokens> },
{ L"text", L"to_utf8", create_normal_function<to_utf8> },
{ L"text", L"from_utf8", create_normal_function<from_utf8> },
{ L"math", L"set_precision", create_normal_function<::set_precision> },
{ L"math", L"set_fixed", create_normal_function<::set_fixed> },
{ L"math", L"set_scientific", create_normal_function<set_scientific> },
{ L"math", L"hex", create_normal_function<hex> },
{ L"math", L"oct", create_normal_function<oct> },
{ L"math", L"from_hex", create_normal_function<from_hex> },
{ L"math", L"from_oct", create_normal_function<from_oct> },
{ L"stdio", L"print", create_normal_function<print> },
{ L"stdio", L"print_line", create_normal_function<print_line> },
{ L"stdio", L"input", create_normal_function<input> },
{ L"stdio", L"input_line", create_normal_function<input_line> },
{ L"stdio", L"ignore", create_normal_function<ignore> },
{ L"gui", L"process_messages", create_normal_function<process_messages> },
{ L"gui", L"next_message", create_normal_function<next_message_1> },
{ L"gui", L"next_message", create_normal_function<next_message_2> },
{ L"gui", L"__message_box", create_normal_function<message_box_1> },
{ L"gui", L"__message_box", create_normal_function<message_box_2> },
{ L"gui", L"create_window",
create_normal_function<resource::create_window> },
{ L"gui", L"destroy_window",
create_normal_function<resource::destroy_window> },
{ L"gui", L"register_message_procedure",
create_normal_function<resource::register_message_procedure> },
{ L"gui", L"resize_client_area",
create_normal_function<resource::resize_client_area> },
{ L"gui", L"create_canvas",
create_normal_function<resource::create_canvas> },
{ L"gui", L"plot", create_normal_function<resource::plot> },
{ L"gui", L"clear", create_normal_function<resource::clear> },
{ L"media", L"play_sound", create_normal_function<play_sound> },
{ L"math", L"random", create_normal_function<random_0> },
{ L"math", L"random", create_normal_function<random_1> },
{ L"math", L"random", create_normal_function<random_2> },
{ L"math", L"seed", create_normal_function<seed_> },
{ L"time", L"time", create_normal_function<time_> },
{ L"time", L"uptime", create_normal_function<uptime> },
{ L"math", L"abs",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<int, int>
>, L"abs", static_cast<int(*)(int)>(&abs)) },
{ L"math", L"acos",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"acos", static_cast<double(*)(double)>(&acos)) },
{ L"math", L"asin",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"asin", static_cast<double(*)(double)>(&asin)) },
{ L"math", L"atan",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"atan", static_cast<double(*)(double)>(&atan)) },
{ L"math", L"atan2",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
double, double> >, L"atan2", static_cast<double(*)(double,
double)>(&atan2)) },
{ L"math", L"cos",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"cos", static_cast<double(*)(double)>(&cos)) },
{ L"math", L"cosh",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"cosh", static_cast<double(*)(double)>(&cosh)) },
{ L"math", L"exp",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"exp", static_cast<double(*)(double)>(&exp)) },
{ L"math", L"fabs",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"fabs", static_cast<double(*)(double)>(&fabs)) },
{ L"math", L"fmod",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
double, double> >, L"fmod", static_cast<double(*)(double,
double)>(&fmod)) },
{ L"math", L"log",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"log", static_cast<double(*)(double)>(&log)) },
{ L"math", L"log10",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"log10", static_cast<double(*)(double)>(&log10)) },
{ L"math", L"pow",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
double, double> >, L"pow", static_cast<double(*)(double, double)>(&pow)) },
{ L"math", L"sin",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"sin", static_cast<double(*)(double)>(&sin)) },
{ L"math", L"sinh",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"sinh", static_cast<double(*)(double)>(&sinh)) },
{ L"math", L"tan",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"tan", static_cast<double(*)(double)>(&tan)) },
{ L"math", L"tanh",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"tanh", static_cast<double(*)(double)>(&tanh)) },
{ L"math", L"sqrt",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"sqrt", static_cast<double(*)(double)>(&sqrt)) },
{ L"math", L"ceil",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"ceil", static_cast<double(*)(double)>(&ceil)) },
{ L"math", L"floor",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"floor", static_cast<double(*)(double)>(&floor)) },
{ L"math", L"frexp", create_normal_function<frexp_> },
{ L"math", L"hypot",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
double, double> >, L"hypot", static_cast<double(*)(double,
double)>(&_hypot)) },
{ L"math", L"_j0",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"_j0", static_cast<double(*)(double)>(&_j0)) },
{ L"math", L"_j1",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"_j1", static_cast<double(*)(double)>(&_j1)) },
{ L"math", L"_jn",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
int, double> >, L"_jn", static_cast<double(*)(int, double)>(&_jn)) },
{ L"math", L"ldexp",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
double, int> >, L"ldexp", static_cast<double(*)(double, int)>(&ldexp)) },
{ L"math", L"floor",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"floor", static_cast<double(*)(double)>(&floor)) },
{ L"math", L"_y0",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"_y0", static_cast<double(*)(double)>(&_y0)) },
{ L"math", L"_y1",
predefined_function::stdlib(create_stdlib_function<stdlib_function_1<double,
double> >, L"_y1", static_cast<double(*)(double)>(&_y1)) },
{ L"math", L"_yn",
predefined_function::stdlib(create_stdlib_function<stdlib_function_2<double,
int, double> >, L"_yn", static_cast<double(*)(int, double)>(&_yn)) },
{ L"math", L"modf", create_normal_function<modf_> },
{ L"irc", L"message_create",
create_normal_function<resource::irc::message_create> },
{ L"irc", L"message_id",
create_normal_function<resource::irc::message_id> },
{ L"irc", L"message_time",
create_normal_function<resource::irc::message_time> },
{ L"irc", L"message_is_ctcp",
create_normal_function<resource::irc::message_is_ctcp> },
{ L"irc", L"message_direction",
create_normal_function<resource::irc::message_direction> },
{ L"irc", L"message_origin",
create_normal_function<resource::irc::message_origin> },
{ L"irc", L"message_command",
create_normal_function<resource::irc::message_command> },
{ L"irc", L"message_command_as_string",
create_normal_function<resource::irc::message_command_as_string> },
{ L"irc", L"message_target",
create_normal_function<resource::irc::message_target> },
{ L"irc", L"message_parameter_count",
create_normal_function<resource::irc::message_parameter_count> },
{ L"irc", L"message_parameter",
create_normal_function<resource::irc::message_parameter> },
{ L"irc", L"message_set_direction",
create_normal_function<resource::irc::message_set_direction> },
{ L"irc", L"message_set_origin",
create_normal_function<resource::irc::message_set_origin> },
{ L"irc", L"message_set_command",
create_normal_function<resource::irc::message_set_command_1> },
{ L"irc", L"message_set_command",
create_normal_function<resource::irc::message_set_command_2> },
{ L"irc", L"message_add_parameter",
create_normal_function<resource::irc::message_add_parameter> },
{ L"irc", L"buffer_type",
create_normal_function<resource::irc::buffer_type> },
{ L"irc", L"buffer_name",
create_normal_function<resource::irc::buffer_name> },
{ L"irc", L"buffer_send_message",
create_normal_function<resource::irc::buffer_send_message> },
{ L"irc", L"buffer_send_command",
create_normal_function<resource::irc::buffer_send_command> },
{ L"irc", L"connection_name",
create_normal_function<resource::irc::connection_name> },
{ L"irc", L"connection_server_buffer",
create_normal_function<resource::irc::connection_server_buffer> },
{ L"irc", L"connection_buffer_from_name",
create_normal_function<resource::irc::connection_buffer_from_name> },
{ L"irc", L"connection_send_message",
create_normal_function<resource::irc::connection_send_message> },
{ L"irc", L"connection_send_command",
create_normal_function<resource::irc::connection_send_command> },
{ L"irc", L"connection_receive_message",
create_normal_function<resource::irc::connection_receive_message> },
{ L"irc", L"create_event_handler",
create_normal_function<resource::irc::create_event_handler> },
};
}
script::script(program& aProgram) : instance(Instance, aProgram),
iCompiling(false), iPass(1), iPuretime(false), iState(ParsingNamespace),
iTypeOk(false)
{
add_namespace(scope::Global, L"");
for (std::size_t i = 0; i != sizeof(sPredefinedNamespaces) /
sizeof(sPredefinedNamespaces[0]); ++i)
add_namespace(scope::Namespace, sPredefinedNamespaces[i]);
for (std::size_t i = 0; i != sizeof(sPredefinedFunctions) /
sizeof(sPredefinedFunctions[0]); ++i)
{
if (sPredefinedFunctions[i].iType.is<predefined_function::normal>())
{
const predefined_function::normal& theType =
sPredefinedFunctions[i].iType;
specific_namespace(sPredefinedFunctions[i].iNamespace).symbol_table().insert(
sPredefinedFunctions[i].iName, value(FunctionDefinition,
function_ptr(theType.iCreator())));
}
else // stdlib function
{
const predefined_function::stdlib& theType =
sPredefinedFunctions[i].iType;
specific_namespace(sPredefinedFunctions[i].iNamespace).symbol_table().insert(
sPredefinedFunctions[i].iName, value(FunctionDefinition,
function_ptr(theType.iCreator(theType.iStdlibFunctionName,
theType.iStdlibFunction))));
}
}
}
I don't think I have gained much though. :)
/Leigh