I
Ian Collins
Interesting example. First, one notices there is a quite generic
and common "try to find and throw if not found" block screaming
"refactor me".
It will get extracted next time I need it!
Interesting example. First, one notices there is a quite generic
and common "try to find and throw if not found" block screaming
"refactor me".
On Dec 7, 4:59 pm, Bart van Ingen Schenau <[email protected]>
wrote:
Perhaps misunderstanding. I claimed that RAII uses OOP to manage
resources not that OOP must use RAII nor that RAII is OOP.
RAII is idiom that fits with OOP.
(e-mail address removed):
My 2 cents (will be converted to 0.1278 cents soon):
Exception RecoverExceptionInCatch() throw() {
try {
throw;
} catch(const Exception& e) {
return e;
} catch(const std::exception& e) {
return Exception("EX12", e.what());
} catch(const char* what) {
return Exception("EX14", what);
} catch( ... ) {
return Exception("EX16", "Unknown error");
}
}
-- and --
bool IpAddress::IsIpv4MappedIpv6() const {
const unsigned char ipv4_prefix[12] =
{0,0,0,0,0,0,0,0,0,0,255,255};
return af_==AF_INET6 && memcmp(ip_, ipv4_prefix, 12)==0;
}
bool IpAddress:perator==(const IpAddress& b) const {
if (af_==b.af_) {
return memcmp(ip_, b.ip_, 16)==0;
} else if (af_==AF_INET) {
DEBUG_ASSERT(b.af_==AF_INET6);
return b.IsIpv4MappedIpv6() && memcmp(ip_, b.ip_+12, 4)==0;
} else {
DEBUG_ASSERT(b.af_==AF_INET);
DEBUG_ASSERT(af_==AF_INET6);
return IsIpv4MappedIpv6() && memcmp(b.ip_, ip_+12, 4)==0;
}
}
And my claim is that RAII and OOP are independent concepts.
The concept of RAII can not possibly exist without constructors and
destructors.
And although, in particular, constructors make OOP a lot easier, they
are not a fundamental requirement for OOP.
I have written OO programs
in C, which does not have constructors (at best, you can emulate them
by using conventions).
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..
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..
On 08/12/2010 20:19, Keith H Duggar 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:
I don't think I have gained much though.
On 09/12/2010 02:24, Ian Collins wrote:
The kind of worse which happens to the codebase of companies which the
likes of Keith Duggar works for. The kind of worse that results from
religiously (blindly) following the doctrine of some suboptimal coding
standard: so my function fits into our standardized editor window (10
lines of code, big font) why not convert it into a table???
I think I will stick with the table version though; I can't be arsed
rolling it back as the difference between the two versions is marginal
by any sensible metric.
That split would be too arbitrary IMO as the functions would be
semantically identical as they are all just creating a different subset
of a set of similar objects.
On 08/12/2010 20:19, Keith H Duggar 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:
On 08/12/2010 21:34, red floyd wrote:
On 08/12/2010 20:19, Keith H Duggar 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.
On 08/12/2010 21:34, red floyd wrote:
On 08/12/2010 20:19, Keith H Duggar 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:This looks like a good candidate for code generation! When I read your
original, I thought it was generated code.<Big Snip>You may have made it worse!
The kind of worse which happens to the codebase of companies which the
likes of Keith Duggar works for. The kind of worse that results from
religiously (blindly) following the doctrine of some suboptimal coding
standard: so my function fits into our standardized editor window (10
lines of code, big font) why not convert it into a table???
I think I will stick with the table version though; I can't be
arsed rolling it back as the difference between the two versions
is marginal by any sensible metric.
Exactly, that's how patterns emerge.
On 09/12/2010 02:24, Ian Collins wrote:
On 12/ 9/10 02:32 PM, Leigh Johnston wrote:
On 08/12/2010 21:34, red floyd wrote:
On 08/12/2010 20:19, Keith H Duggar 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:
This looks like a good candidate for code generation! When I read your
original, I thought it was generated code.
<Big Snip>
I don't think I have gained much though.
You may have made it worse!
The kind of worse which happens to the codebase of companies which the
likes of Keith Duggar works for. The kind of worse that results from
religiously (blindly) following the doctrine of some suboptimal coding
standard: so my function fits into our standardized editor window (10
lines of code, big font) why not convert it into a table???LOL ... no it's the kind of worse that results from a naive
poorly trained programmer who is totally ignorant of superior
solutions and how to implement them. You are getting a bit
closer though. Perhaps combined with hint's from Michael's
post, examining a particular boost library, and dare I say
some /thinking/ (instead of ranting and squirming) you might
figured it out.You are right, the difference is marginal: they both still suck.
But, I think you will make more progress in coming weeks. Until
then I look forward to you digging your rant hole deeper so that,
when you finally have coded one of the superior solutions, you
can look back and see what a vociferous ignorant fool you were.
I am ranting?
lol.
You really are a pathetic troll;
any code you produce for your employer is probably an over
engineered, prematurely optimized bag of shite; I pity your
employer.
Actually whilst this builder pattern (which does not appear to be the
same as the builder pattern in GoF) solution does kind of look clean it
does smell of over engineering the simple problem of allocating a
collection of objects.
The only real problem with my initial solution of a simple list of
explic object allocations in a constructor is the repetition of code and
string literals; is this such a big deal? I think not. Ian pointed out
that this would be fine if the code was generated.
The use of macros would amortize the problems of code and string literal
repetition of course and whilst I usualy eschew the use of macros
perhaps their use in this particular case is warrented? [...]
[Please do not mail me a copy of your followup]
Leigh Johnston<[email protected]> spake the secret code
Actually whilst this builder pattern (which does not appear to be the
same as the builder pattern in GoF) solution does kind of look clean it
does smell of over engineering the simple problem of allocating a
collection of objects.
The only real problem with my initial solution of a simple list of
explic object allocations in a constructor is the repetition of code and
string literals; is this such a big deal? I think not. Ian pointed out
that this would be fine if the code was generated.
I really don't like duplication and in particular repeated duplication
of string constants is something I view as more smelly than just
duplication of code. So yeah, I probably would have introduced some
named constants (if you type the name wrong, its a compilation error,
but if you type the string literal wrong, its a much harder to find
run-time error).
On 09/12/2010 14:04, Michael Doubez wrote:
On 09/12/2010 12:18, Michael Doubez wrote:
On 08/12/2010 21:34, red floyd wrote:
On 08/12/2010 20:19, Keith H Duggar 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).And to preempt the vociferous Duggar yes it is obvious that using design
patterns generally do make things cleaner but I have to admit I have not
*knowingly* used this particular pattern before. I am not ignorant of
design patterns in general; I use the observer pattern a lot for
example.
Actually whilst this builder pattern (which does not appear to be the
same as the builder pattern in GoF) solution does kind of look clean it
does smell of over engineering the simple problem of allocating a
collection of objects.
The only real problem with my initial solution of a simple list of
explic object allocations in a constructor is the repetition of code and
string literals; is this such a big deal? I think not. Ian pointed out
that this would be fine if the code was generated.
The intermediate table based solution probably sucked the most.
On 07/12/2010 00:55, Keith H Duggar wrote:
[...]
First random choice of a *real world* function of mine:
plugins::entry_list::iterator plugins::find(const plugin& aEntry)
{
lib::lock lock(*this);
for (entry_list::iterator i = iEntries.begin(); i != iEntries.end(); ++i)
if (i->path() == aEntry.path())
return i;
return iEntries.end();
}
Certainly. I use for loops myself a lot. In this case, I don't
see any real difference between my while and:
for ( ; i != iEntries.end()&& i->path() != aEntry.path(); ++ i) {
}
Which ever seems more natural or more appropriate is purely a
matter of taste.
I don't have any irrational fear of if or return. I just prefer
to use them when appropriate. I do have a fear of code that I'm
not certain is correct. For a short bit of code like this, of
course, all of the posted forms are fairly readable: the form I
posted (with the while) has the advantage of being the canonical
form: the way linear search was first presented and analysed
(and proved) in literature. I would expect most programmers to
immediately recognize it as such.
The issue of readability is arguable. I can understand your
point of view. The issue of verifiability is less arguable,
since the classical tools (logical or automatic) don't handle
multiple exits as well. And in this particular case, the fact
that it is a classical algorithm with a canonical form argues
strongly for using that canonical form, even for reasons of
readability.
Disagree.Microsoft does not define the canonical forms. Agree.
The standard
literature where the algorithm was present *and* proved does.
Agree. They are small enough to understand them anyway, even though theyAnd in small functions like these, it's not that important.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.