-
December 28th, 2011, 09:44 AM
#1
[RESOLVED] Variable name
I have a bunch of functions sharing the variable names like:
void myfunc(int a, string b);
void func2(int c, string b);
void func3(int a, int c);
But they are not consistent in the way they have been called in functions. But I want to write a general checker for input variables. Something of sort:
if inputs are like this, then they each should have this value. E.g. 'a' should not be greater than 10, 'b' should not be less than 0, etc. I ask this cause within the function call, 'a', 'b', 'c' don't really exist per se but they contain values instead.
How do I implement this?
Thanks guys
-
December 28th, 2011, 10:07 AM
#2
Re: Variable name
Why not give variable names logical names instead of 1 letter-stuff. 1 letter variables are an abomination for maintaining code.
-
December 28th, 2011, 10:16 AM
#3
Re: Variable name
One way to impose "automatic" checks on variables is to specify a unique type for the variable. For instance, "'a' should not be greater than 10" could be represented by a class IntLessThan10.
This class would need to be constructable from an int, and optionally, you could even make it implicitly convertible to an int. That way, you could use it almost exactly like an int, but it would have additional logic in the constructor which throws an exception if given an invalid argument.
-
December 28th, 2011, 11:43 AM
#4
Re: Variable name
@Lindley
Can you please elaborate some more on your suggestion, I like your idea but since I am still learning I feel I am missing quite a bit from your suggestion.
Thanks
-
December 28th, 2011, 12:44 PM
#5
Re: Variable name
Originally Posted by Learned
@Lindley
Can you please elaborate some more on your suggestion, I like your idea but since I am still learning I feel I am missing quite a bit from your suggestion.
Lindley described in detail as to what to do.
You need to specify exactly what you didn't understand.
Regards,
Paul McKenzie
-
December 28th, 2011, 04:41 PM
#6
Re: Variable name
This is what I have been thinking:
class lessthan10
{
public:
int a;
lessthan10() {cout<<"Bad data"; //etc.}
};
Now in the original function, if it had been:
void myfunc(int a, string b);
How would the user call it:
myfunc(1000, string b);
This would not work. Wouldn't the user need to define an object first, etc.
Thanks
-
December 28th, 2011, 05:37 PM
#7
Re: Variable name
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class IntLessThan10
{
int m_int;
public:
operator int()
{
return m_int;
}
IntLessThan10(int int_): m_int(int_)
{
if (m_int >= 10)
throw Exception(m_int);
}
struct Exception
{
Exception(int int_)
{
ostringstream str;
str << "IntLessThan10: bad value: " << int_;
msg = str.str();
}
string msg;
};
};
void foo(IntLessThan10 a)
{
cout << __FUNCTION__ << ": Succeeded with " << (int)a << endl;
}
int main()
{
try
{
foo(-12);
foo(7);
foo(17);
}
catch(IntLessThan10::Exception e)
{
cout << e.msg << endl;
}
return 0;
}
Code:
D:\Temp\23>23
foo: Succeeded with -12
foo: Succeeded with 7
IntLessThan10: bad value: 17
Best regards,
Igor
-
December 29th, 2011, 02:30 PM
#8
Re: Variable name
Very nicely done. Thanks. I learn a lot here just reading people's code.
-
December 30th, 2011, 10:22 AM
#9
Re: [RESOLVED] Variable name
Here's a bit more generic solution:
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct LessThanException
{
LessThanException(int int_, int threshold)
{
ostringstream str;
str << "IntLessThan" << threshold << ": bad value: " << int_;
msg = str.str();
}
string msg;
};
template <int t_threshold>
class IntLessThan
{
int m_int;
public:
operator int()
{
return m_int;
}
IntLessThan(int int_): m_int(int_)
{
if (m_int >= t_threshold)
throw LessThanException(m_int, t_threshold);
}
};
typedef IntLessThan<10> IntLessThan10;
void foo(int a)
{
IntLessThan10 dummy(a); // just check
cout << __FUNCTION__ << ": Succeeded with " << a << endl;
}
template <int t_threshold>
void Foo(int a)
{
IntLessThan<t_threshold> dummy(a); // just check
cout << __FUNCTION__ << ": Succeeded with " << a << endl;
}
int main()
{
try
{
foo(-12); // threshold prefefined by foo implementation
foo(7);
Foo<20>(17); // threshold explicitly specified
Foo<15>(17);
}
catch(LessThanException e)
{
cout << e.msg << endl;
}
return 0;
}
Code:
D:\Temp\23>23.exe
foo: Succeeded with -12
foo: Succeeded with 7
Foo: Succeeded with 17
IntLessThan15: bad value: 17
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|