Re: attack of silly coding standard?
On 09/12/2010 14:04, Michael Doubez wrote:
On 9 d?c, 14:13, Leigh Johnston<le...@i42.co.uk> wrote:
On 09/12/2010 12:18, Michael Doubez wrote:
On 9 d c, 02:32, Leigh Johnston<le...@i42.co.uk> wrote:
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.
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:
[snip]
Actually, I'd rather have a builder pattern:
[snip]
ByteCodeRepository repository = ByteCodeRepository::Builder()
.addNamespace(L"")
.addNamespace(L"system"),
.addNormalFunction<wait_1>( L"wait" )
.addNormalFunction<wait_2>( L"wait" )
[snip]
.addNamespace(L"text"),
.addNormalFunction<tokens> ( L"tokens" )
.addNormalFunction<to_utf8> ( L"to_utf8" )
.addNormalFunction<from_utf8> ( L"from_utf8" )
.addNamespace(L"math"),
// ...
;
};
Hereafter it is not too hard to make a loop for registration.
But I agree that if it doesn't make sense to keep it afterward, you
could put that in a function and it can exceed the XX lines limit.
I don't see how this is any better than the other two solutions.
I see the following advantages:
- the namespace string is not repeated, typing errors are reduced.
- the namespace.function tree becomes apparent which make reading
easier (you could also reorganize it into subinitialisation functions/
modules). In fact, syntactic sugar could be designed to make easier to
read.
- the init can be put into its own file, which separates the code
and the initialisation data.
- the structure can be reused/preprocessed/enhanced before
insertion: gives a base for future changes (in particular using more
than one insertion sequence).
--
As I value code readability quite highly I have decided to go with your
solution Michael and it turned out that using the pattern made things a
lot cleaner.
Here it is (hopefully we can now change the subject):
namespace
{
class predefined_normal_function
{
// types
public:
typedef function* (*creator_t)();
// construction
public:
predefined_normal_function(creator_t aCreator) : iCreator(aCreator) {}
// operations
public:
creator_t creator() const { return iCreator; }
// attributes
private:
creator_t iCreator;
};
class predefined_stdlib_function
{
// types
public:
typedef function* (*creator_t)(const std::wstring&, void*);
// construction
public:
template <typename Function>
predefined_stdlib_function(creator_t aCreator, const wchar_t* aName,
Function aStdlibFunction) :
iCreator(aCreator), iName(aName), iStdlibFunction(aStdlibFunction) {}
// operations
public:
creator_t creator() const { return iCreator; }
const wchar_t* name() const { return iName; }
void* stdlib_function() const { return iStdlibFunction; }
// attributes
private:
creator_t iCreator;
const wchar_t* iName;
void* iStdlibFunction;
};
class predefined_functions
{
// types
public:
typedef lib::variant<predefined_normal_function,
predefined_stdlib_function> predefined_function;
typedef std::vector<predefined_function> function_list_t;
typedef std::map<std::wstring, function_list_t> namespaces_t;
class builder
{
// construction
public:
builder() : iCurrentNamespace(iNamespaces.end()) {}
// operations
public:
namespaces_t& namespaces() const { return iNamespaces; }
builder& add_namespace(const std::wstring& aNamespace)
{
iCurrentNamespace = iNamespaces.insert(std::make_pair(aNamespace,
function_list_t())).first;
return *this;
}
template <typename T>
builder& add_normal_function()
{
assert(iCurrentNamespace != iNamespaces.end());
iCurrentNamespace->second.push_back(predefined_normal_function(create_normal_function<T>));
return *this;
}
template <typename T>
builder& add_stdlib_function(const wchar_t* aName, typename
T::function_type aStdlibFunction)
{
assert(iCurrentNamespace != iNamespaces.end());
iCurrentNamespace->second.push_back(predefined_stdlib_function(create_stdlib_function<T>,
aName, aStdlibFunction));
return *this;
}
// implementation
private:
template <typename T>
static function* create_normal_function()
{
return new T();
}
template <typename T>
static function* create_stdlib_function(const std::wstring& aName,
void* aFunction)
{
return new T(aName, reinterpret_cast<typename
T::function_type>(aFunction));
}
// attributes
private:
mutable namespaces_t iNamespaces;
namespaces_t::iterator iCurrentNamespace;
};
// construction
public:
predefined_functions(const builder& aBuilder)
{
iNamespaces.swap(aBuilder.namespaces());
}
// operations
public:
const namespaces_t& namespaces() const { return iNamespaces; }
// attributes
private:
namespaces_t iNamespaces;
};
const predefined_functions sPredefinedFunctions =
predefined_functions::builder()
.add_namespace(L"system")
.add_normal_function<wait_1>()
.add_normal_function<wait_2>()
.add_normal_function<create_thread_1>()
.add_normal_function<create_thread_2>()
.add_normal_function<send_object_1>()
.add_normal_function<send_object_2>()
.add_normal_function<post_object_1>()
.add_normal_function<post_object_2>()
.add_normal_function<available>()
.add_normal_function<which>()
.add_normal_function<get>()
.add_normal_function<resource::file_open>()
.add_normal_function<resource::file_read_1>()
.add_normal_function<resource::file_read_2>()
.add_normal_function<resource::file_read_line>()
.add_normal_function<resource::file_write_1>()
.add_normal_function<resource::file_write_2>()
.add_normal_function<resource::file_write_line>()
.add_normal_function<resource::file_seek_read_1>()
.add_normal_function<resource::file_seek_read_2>()
.add_normal_function<resource::file_tell_read>()
.add_normal_function<resource::file_seek_write_1>()
.add_normal_function<resource::file_seek_write_2>()
.add_normal_function<resource::file_tell_write>()
.add_normal_function<resource::file_is_good>()
.add_normal_function<resource::file_close>()
.add_namespace(L"text")
.add_normal_function<tokens>()
.add_normal_function<to_utf8>()
.add_normal_function<from_utf8>()
.add_namespace(L"math")
.add_normal_function<::set_precision>()
.add_normal_function<::set_fixed>()
.add_normal_function<set_scientific>()
.add_normal_function<hex>()
.add_normal_function<oct>()
.add_normal_function<from_hex>()
.add_normal_function<from_oct>()
.add_normal_function<random_0>()
.add_normal_function<random_1>()
.add_normal_function<random_2>()
.add_normal_function<seed_>()
.add_stdlib_function<stdlib_function_1<int, int> >(L"abs",
static_cast<int(*)(int)>(&abs))
.add_stdlib_function<stdlib_function_1<double, double> >(L"acos",
static_cast<double(*)(double)>(&acos))
.add_stdlib_function<stdlib_function_1<double, double> >(L"asin",
static_cast<double(*)(double)>(&asin))
.add_stdlib_function<stdlib_function_1<double, double> >(L"atan",
static_cast<double(*)(double)>(&atan))
.add_stdlib_function<stdlib_function_2<double, double, double>
>(L"atan2", static_cast<double(*)(double, double)>(&atan2))
.add_stdlib_function<stdlib_function_1<double, double> >(L"cos",
static_cast<double(*)(double)>(&cos))
.add_stdlib_function<stdlib_function_1<double, double> >(L"cosh",
static_cast<double(*)(double)>(&cosh))
.add_stdlib_function<stdlib_function_1<double, double> >(L"exp",
static_cast<double(*)(double)>(&exp))
.add_stdlib_function<stdlib_function_1<double, double> >(L"fabs",
static_cast<double(*)(double)>(&fabs))
.add_stdlib_function<stdlib_function_2<double, double, double>
>(L"fmod", static_cast<double(*)(double, double)>(&fmod))
.add_stdlib_function<stdlib_function_1<double, double> >(L"log",
static_cast<double(*)(double)>(&log))
.add_stdlib_function<stdlib_function_1<double, double> >(L"log10",
static_cast<double(*)(double)>(&log10))
.add_stdlib_function<stdlib_function_2<double, double, double>
>(L"pow", static_cast<double(*)(double, double)>(&pow))
.add_stdlib_function<stdlib_function_1<double, double> >(L"sin",
static_cast<double(*)(double)>(&sin))
.add_stdlib_function<stdlib_function_1<double, double> >(L"sinh",
static_cast<double(*)(double)>(&sinh))
.add_stdlib_function<stdlib_function_1<double, double> >(L"tan",
static_cast<double(*)(double)>(&tan))
.add_stdlib_function<stdlib_function_1<double, double> >(L"tanh",
static_cast<double(*)(double)>(&tanh))
.add_stdlib_function<stdlib_function_1<double, double> >(L"sqrt",
static_cast<double(*)(double)>(&sqrt))
.add_stdlib_function<stdlib_function_1<double, double> >(L"ceil",
static_cast<double(*)(double)>(&ceil))
.add_stdlib_function<stdlib_function_1<double, double> >(L"floor",
static_cast<double(*)(double)>(&floor))
.add_normal_function<frexp_>()
.add_stdlib_function<stdlib_function_2<double, double, double>
>(L"hypot", static_cast<double(*)(double, double)>(&_hypot))
.add_stdlib_function<stdlib_function_1<double, double> >(L"_j0",
static_cast<double(*)(double)>(&_j0))
.add_stdlib_function<stdlib_function_1<double, double> >(L"_j1",
static_cast<double(*)(double)>(&_j1))
.add_stdlib_function<stdlib_function_2<double, int, double> >(L"_jn",
static_cast<double(*)(int, double)>(&_jn))
.add_stdlib_function<stdlib_function_2<double, double, int>
>(L"ldexp", static_cast<double(*)(double, int)>(&ldexp))
.add_stdlib_function<stdlib_function_1<double, double> >(L"floor",
static_cast<double(*)(double)>(&floor))
.add_stdlib_function<stdlib_function_1<double, double> >(L"_y0",
static_cast<double(*)(double)>(&_y0))
.add_stdlib_function<stdlib_function_1<double, double> >(L"_y1",
static_cast<double(*)(double)>(&_y1))
.add_stdlib_function<stdlib_function_2<double, int, double> >(L"_yn",
static_cast<double(*)(int, double)>(&_yn))
.add_normal_function<modf_>()
.add_namespace(L"stdio")
.add_normal_function<print>()
.add_normal_function<print_line>()
.add_normal_function<input>()
.add_normal_function<input_line>()
.add_normal_function<ignore>()
.add_namespace(L"gui")
.add_normal_function<process_messages>()
.add_normal_function<next_message_1>()
.add_normal_function<next_message_2>()
.add_normal_function<message_box_1>()
.add_normal_function<message_box_2>()
.add_normal_function<resource::create_window>()
.add_normal_function<resource::destroy_window>()
.add_normal_function<resource::register_message_procedure>()
.add_normal_function<resource::resize_client_area>()
.add_normal_function<resource::create_canvas>()
.add_normal_function<resource::plot>()
.add_normal_function<resource::clear>()
.add_namespace(L"media")
.add_normal_function<play_sound>()
.add_namespace(L"time")
.add_normal_function<time_>()
.add_normal_function<uptime>()
.add_namespace(L"irc")
.add_normal_function<resource::irc::message_create>()
.add_normal_function<resource::irc::message_id>()
.add_normal_function<resource::irc::message_time>()
.add_normal_function<resource::irc::message_is_ctcp>()
.add_normal_function<resource::irc::message_direction>()
.add_normal_function<resource::irc::message_origin>()
.add_normal_function<resource::irc::message_command>()
.add_normal_function<resource::irc::message_command_as_string>()
.add_normal_function<resource::irc::message_target>()
.add_normal_function<resource::irc::message_parameter_count>()
.add_normal_function<resource::irc::message_parameter>()
.add_normal_function<resource::irc::message_set_direction>()
.add_normal_function<resource::irc::message_set_origin>()
.add_normal_function<resource::irc::message_set_command_1>()
.add_normal_function<resource::irc::message_set_command_2>()
.add_normal_function<resource::irc::message_add_parameter>()
.add_normal_function<resource::irc::buffer_type>()
.add_normal_function<resource::irc::buffer_name>()
.add_normal_function<resource::irc::buffer_send_message>()
.add_normal_function<resource::irc::buffer_send_command>()
.add_normal_function<resource::irc::connection_name>()
.add_normal_function<resource::irc::connection_server_buffer>()
.add_normal_function<resource::irc::connection_buffer_from_name>()
.add_normal_function<resource::irc::connection_send_message>()
.add_normal_function<resource::irc::connection_send_command>()
.add_normal_function<resource::irc::connection_receive_message>()
.add_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 (predefined_functions::namespaces_t::const_iterator i =
sPredefinedFunctions.namespaces().begin(); i !=
sPredefinedFunctions.namespaces().end(); ++i)
{
for (predefined_functions::function_list_t::const_iterator j =
i->second.begin(); j != i->second.end(); ++j)
{
function_ptr functionInstance;
if (j->is<predefined_normal_function>())
{
const predefined_normal_function& predefinedFunction = *j;
functionInstance = function_ptr(predefinedFunction.creator()());
}
else // stdlib function
{
const predefined_stdlib_function& predefinedFunction = *j;
functionInstance =
function_ptr(predefinedFunction.creator()(predefinedFunction.name(),
predefinedFunction.stdlib_function()));
}
specific_namespace(i->first).symbol_table().insert(functionInstance->name(),
value(FunctionDefinition, functionInstance));
}
}
}