Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Rejecting negative number

Reply
Thread Tools

Rejecting negative number

 
 
JKop
Guest
Posts: n/a
 
      05-24-2004
Take a class like the following:

class Finger
{
public:

double length;
}


I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.


So... I've come up with 3 potential solutions: A1 A2 B .


A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.



Which would you suggest? Any other suggestions?


I myself am leaning toward B.


Thanks


-JKop

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"JKop" <(E-Mail Removed)> wrote in message
news:_uusc.350$(E-Mail Removed)...
> Take a class like the following:
>
> class Finger
> {
> public:
>
> double length;
> }
>
>
> I'm writing reusable code and, being dictator, I dictate that "length"
> cannot be negative. The following would be perfect:
>
> unsigned double length;
>
> But ofcourse doesn't compile.
>
>
> So... I've come up with 3 potential solutions: A1 A2 B .
>
>
> A) Create 2 separate member functions, GetLength and SetLength. SetLength
> will reject negative numbers. When supplied with a negative number it

will:
> A1) Throw an exception
> A2) Return a false boolean value indicating failure
>
> B) Just document that the code will have undefined behaviour if length is
> set to a negative number.
>
>
>
> Which would you suggest? Any other suggestions?
>
>
> I myself am leaning toward B.
>


A1 without a doubt.

Also note that A1 is a subset of B, so you could document B but implement A1
if you really wanted to. When your users complain about the exception, you
just say 'I told you the behaviour was undefined'.

john


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      05-24-2004
John Harrison posted:

> A1 without a doubt.
>
> Also note that A1 is a subset of B, so you could document B but
> implement A1 if you really wanted to. When your users complain about
> the exception, you just say 'I told you the behaviour was undefined'.



Thanks for the input.


I myself think that that would be too kind. "Undefined behaviour" means
exactly what it says on the tin. It's like saying to a child, "Yes, Go play
with the matches and the petrol - Just don't come running to _ME_ when
you've burned yourself". And ofcourse, any responsible parent would say,
"Don't play with the matches and the petrol!" and then actually physically
prevent the children from performing such actions.

But... we're all consenting adults here.


Although I still haven't made my decision yet.


All input appreciated.


Thanks


-JKop
 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      05-24-2004
JKop wrote:
> Take a class like the following:
>
> class Finger
> {
> public:
>
> double length;
> }
>
>
> I'm writing reusable code and, being dictator, I dictate that "length"
> cannot be negative. The following would be perfect:
>
> unsigned double length;
>
> But ofcourse doesn't compile.
>
>
> So... I've come up with 3 potential solutions: A1 A2 B .
>
>
> A) Create 2 separate member functions, GetLength and SetLength.
> SetLength will reject negative numbers. When supplied with a negative
> number it will: A1) Throw an exception
> A2) Return a false boolean value indicating failure
>
> B) Just document that the code will have undefined behaviour if
> length is set to a negative number.
>
>
>
> Which would you suggest? Any other suggestions?
>
>
> I myself am leaning toward B.
>
>
> Thanks
>
>
> -JKop


Make a small property template class that allows for range validation, like
this:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

template <class T, class Modify>
class property
{
protected:
T var;
Modify mod;
public:
property<T, Modify>(const T& init, const Modify& m)
: mod(m), var(init)
{}

operator T()
{
return var;
}

property& operator =(const T& val)
{
if(!mod(val))
throw invalid_argument("bad property argument.");
var = val;
return *this;
}
};

template<class T>
struct RangeValidationPropModified
{
string msg;
T minval, maxval;
public:
RangeValidationPropModified<T>(string m, const T& minv, const T& maxv)
: msg(m),
minval(minv),
maxval(maxv)
{}

bool operator() (const T& val)
{
if(val >= minval && val <= maxval)
{
cout << msg;
return true;
}
else
return false;
}
};

int main(int argc, char* argv[])
{
property<int, RangeValidationPropModified<int> >
p(0, RangeValidationPropModified<int>("the property is being
modifed!\n", 0, 100));

cout << p << endl;
p = 5;
cout << p << endl;
p = 10;
cout << p << endl;
try
{
p = -1;
cout << p << endl;
p = 1000;
cout << p << endl;
}
catch(invalid_argument ex)
{
cout << ex.what() << endl;
}

system("pause");
return 0;
}

- Pete


 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      05-24-2004
Petec wrote:
> JKop wrote:

<snip>
>
> operator T()
> {
> return var;
> }


Should be:

operator T&()
{
return var;
}

To allow operators to be applied.

>

<snip>


 
Reply With Quote
 
*
Guest
Posts: n/a
 
      05-24-2004
?
"Petec" <(E-Mail Removed)> wrote in message
news:CWusc.8842$(E-Mail Removed) nk.net...
> Petec wrote:
> > JKop wrote:

> <snip>
> >
> > operator T()
> > {
> > return var;
> > }

>
> Should be:
>
> operator T&()
> {
> return var;
> }
>
> To allow operators to be applied.
>
> >

> <snip>
>
>



 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      05-24-2004
* wrote:
> ?
> "Petec" <(E-Mail Removed)> wrote in message
> news:CWusc.8842$(E-Mail Removed) nk.net...
>> Petec wrote:
>>> JKop wrote:

>> <snip>
>>>
>>> operator T()
>>> {
>>> return var;
>>> }

>>
>> Should be:
>>
>> operator T&()
>> {
>> return var;
>> }
>>
>> To allow operators to be applied.
>>
>>>

>> <snip>


??

Please don't top-post, BTW.

- Pete


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-25-2004
"JKop" <(E-Mail Removed)> wrote in message
news:_uusc.350$(E-Mail Removed)...

> unsigned double length;
>
> But ofcourse doesn't compile.


> A) Create 2 separate member functions, GetLength and SetLength. SetLength
> will reject negative numbers. When supplied with a negative number it

will:
> A1) Throw an exception
> A2) Return a false boolean value indicating failure
>
> B) Just document that the code will have undefined behaviour if length is
> set to a negative number.
>
>
>
> Which would you suggest? Any other suggestions?


First choice = B for performance reasons, simplicity of code. The C++
standard does it all the time (ie. if iterator points to 2 plus end then
result is undefined, if std::sort compare function does not have strict weak
ordering then undefined behavior).

Second choice = A1. As a variation, only if NDEBUG is defined then do we
throw an exception.

Third choice = A2.


 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      05-25-2004
"Petec" <(E-Mail Removed)> wrote in message news:<CWusc.8842$(E-Mail Removed) ink.net>...
> Petec wrote:
> > JKop wrote:

> <snip>
> >
> > operator T()
> > {
> > return var;
> > }

>
> Should be:
>
> operator T&()
> {
> return var;
> }
>
> To allow operators to be applied.


No. If the restriction is that the value should be positive,
one cannot allow T:perator*=, someonce could pass -1. The same
applies for operator+=, opertaor/=, and (for positive arguments)
to operator-=.

The correct form is operator T() const;, as it won't modify var.

Regards,
Michiel Salters
 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      05-25-2004
Michiel Salters wrote:
> "Petec" <(E-Mail Removed)> wrote in message
> news:<CWusc.8842$(E-Mail Removed) ink.net>...
>> Petec wrote:
>>> JKop wrote:

>> <snip>
>>>
>>> operator T()
>>> {
>>> return var;
>>> }

>>
>> Should be:
>>
>> operator T&()
>> {
>> return var;
>> }
>>
>> To allow operators to be applied.

>
> No. If the restriction is that the value should be positive,
> one cannot allow T:perator*=, someonce could pass -1. The same
> applies for operator+=, opertaor/=, and (for positive arguments)
> to operator-=.
>
> The correct form is operator T() const;, as it won't modify var.


Ahh, yes, I forgot about that. I'll implement the operators on it and post
back here...

- Pete

>
> Regards,
> Michiel Salters




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Adding a positive number and a negative number MRAB Python 2 01-31-2009 02:41 AM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
OE rejecting Attachments to emails Philip Bennett Computer Support 2 05-12-2004 09:35 PM
On-Line compiler not correctly rejecting code.. Andrew Thompson Java 9 05-06-2004 02:54 AM



Advertisments