Sign in to follow this  
raptorstrike

Using define to shorten things up

Recommended Posts

I was wondering if this was a good idea, it works and seems flexible but could possibly lead to confusion and might just be bad form which is what Im here, to find out.
#define CLASS OTHER_NAME

class OTHER_NAME
{
    CLASS(){};
    ~CLASS(){};
};

#define CLASS ANOTHER_CLASS

class ANOTHER_CLASS
{
    CLASS(){};
    ~CLASS(){};
};

thats the basic idea, the CLASS would act like a "this" for decelerations, would make typing simpler, you wouldn't have to remember the exact spelling or punctuation of what your class is named (the idea here is that every class defines class as themselves) and might have a few other advantages but could also be confusing. I will be happy with anyone's take on this. Thanks for your time

Share this post


Link to post
Share on other sites
Wow.

Just...

That's a terrible idea.

I can't even put words to how ... yeah. I can't even put words to it.

If you're so..uh...lazy (or whatever), that you aren't even willing to remember the NAME OF THE CLASS that you're WRITING, then perhaps you should choose another hobby/job. Like lawn darts.

Share this post


Link to post
Share on other sites
It was hard for me to reply to this... as this is one of the worst ideas I have ever read so I wasn't sure what to say.

EDIT: well I guess I could have told you to start your lawn darts practice :)

Share this post


Link to post
Share on other sites
With the advent of sane tab/auto-completion, you don't need to remember the exact spelling or punctuation of your classes. #define should be used with the utmost caution.

Share this post


Link to post
Share on other sites
Why not push this further?


#define CLASS THE_NAME
class CLASS
{
CLASS(){};
~CLASS(){};
};
#undef CLASS


Although I'm not sure that it actually saves any typing (the above version uses 17 characters more than if it had used the correct name. To have actual savings, you'd need to have a shortened name that's more than twice as long as the "self-ref" name, and even then, this assumes that a decent number of constructors-desctructors (most classes only have one constructor and no destructor).

Share this post


Link to post
Share on other sites
Well ok, yeah thanks for basically personally attacking me over an idea which I just thought of and just wanted your opinion on. I had no intention of following this convention as indeed I am not that lazy and think it would lead to code being unclear. As for people with respectable responses thank you. I should probably just kill the thread but as it seem that people are trying to do things more efficiently it seemed like something to mention.

Yeah I was pretty sure I would get responses saying what you guys did but thought I would throw it out there. You don't have to act personally offended or attack me.

It was an idea up for review you didn't NEED to say anything.

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
You don't have to act personally offended or attack me.


Other than the Drilian's comment, all the posts were just attacking the idea. The idea is flat out bad.

In general macros should be avoided by better coding practices. Macros obscure code, confuse development environment, and (when they code functions) complicate debugging.

-me

Share this post


Link to post
Share on other sites
Look, it's like this:

if you ask for feedback, expect feedback. Don't expect only POSITIVE feedback. The world, and especially the Internet, is not filled with candy-cane slides and butterscotch waterfalls beneath a pure blue sky with rainbows in it. Asking for feedback means you'll get FEEDBACK, and all that that implies.

If you ask for feedback on a bad idea that seems designed for no other purpose than that (In your own words!) "you wouldn't have to remember the exact spelling or punctuation of what your class is named," expect harsh feedback accordingly, especially when it sounds like your rationale stems from not even wanting to have to know the name of whatever it is that you're programming.

If you EXPECTED people to be like "wtf?" then maybe you should have thought harder about the merits of your idea before posting it. You said you were here to find out if it was bad form: it is.

As Telastyn points out, #define should be used with caution. This idea is not cautious, it's just confusing. You still have to use the name of the class when allocating it (or casting to it), so why not when writing it?

I stand by my assessment. And if you don't want feedback (even though you say you do), don't ask for it.

And if you GET feedback when you asked for it, don't tell people that they "didn't NEED to say anything." That's disingenuous.

[Edited by - Drilian on May 4, 2007 3:32:08 PM]

Share this post


Link to post
Share on other sites
This is how defines can be abused to write pascal code in C, for example.

http://worsethanfailure.com/Articles/_0x23_include__0x22_pascal_0x2e_h_0x22_.aspx

http://worsethanfailure.com/Comments/_0x23_include__0x22_pascal_0x2e_h_0x22_.aspx#25007

Share this post


Link to post
Share on other sites
I agree with you Drillian and I dont expect all positive feedback but if your going to give feed back make it constructive. As I recall the most constructive thing your post mentioned was lawn darts, Palidine and Telastyn have given more constructive responses pointing out what was wrong with the IDEA. Anyway I wasnt sure to post on this topic and in the future I will not post on semi questional ideas on which I want someones opinion.

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
I was wondering if this was a good idea, it works and seems flexible but could possibly lead to confusion and might just be bad form which is what Im here, to find out...


Well, I think that what you are trying to do is a good idea, but the way you propose accomplishing it is not very practical. I think the idea of naming the contructors and destructor functions using the name of the class has problems. I would have preferred the names of the constructors and destructor be "constructor" and "destructor".

Along the same lines, this kind of thing is common:
    class foo : public bar
{
typedef bar BaseClass;
...
};

Share this post


Link to post
Share on other sites
I had a similar(somewhat more complex) idea for using defines to implement a switch for types other than integrals. While it worked, I decided not to use it because actually changing the syntax of the language is not a good thing. For better or for worse, C++ has constructors and destructors named after the class and not aliased with names like "CLASS","~CLASS","__init__" ,"__del__" or what have you. And if you're using C++, you must write in C++, otherwise you're just causing confusion. Using #define like that is nasty, because you're not using an "proper" language mechanism, just brute text substitution.

If you're seeking for ideas like this in the future, I suggest you look into templates and compile-time metaprogramming, they can "transform",so to speak, the language as well, but this time by playing by the rules ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Quote:
Original post by raptorstrike
I was wondering if this was a good idea, it works and seems flexible but could possibly lead to confusion and might just be bad form which is what Im here, to find out...


Well, I think that what you are trying to do is a good idea, but the way you propose accomplishing it is not very practical. I think the idea of naming the contructors and destructor functions using the name of the class has problems. I would have preferred the names of the constructors and destructor be "constructor" and "destructor".


Or you could go with D's style and use this() and ~this...

Share this post


Link to post
Share on other sites
Like most others have said I think its a very bad idea, but, there are also times when very similar things are justifiable.

Example:

/*************************************************************/
#define BINARY_OPERATOR(name, op) \
template<typename LHS, typename RHS> \
struct name \
{ \
name(LHS lhs, RHS rhs): lhs(lhs), rhs(rhs) { } \
LHS lhs; RHS rhs; \
}; \
\
template<typename LHS, typename RHS> \
name<LHS, RHS> op(LHS lhs, RHS rhs) \
{ return name<LHS, RHS>(lhs, rhs); } \
/*************************************************************/

BINARY_OPERATOR(add_op , operator+ )
BINARY_OPERATOR(sub_op , operator- )
BINARY_OPERATOR(mul_op , operator* )
BINARY_OPERATOR(div_op , operator/ )
BINARY_OPERATOR(add_equal_op, operator+=)
BINARY_OPERATOR(sub_equal_op, operator-=)
BINARY_OPERATOR(mul_equal_op, operator*=)
BINARY_OPERATOR(div_equal_op, operator/+)

#undef BINARY_OPERATOR





Edit: forum screwed up macro, had to add spaces after '\'s

Share this post


Link to post
Share on other sites
Boo Aiming to save a few keystrokes is not a terribly admirable goal1. In the original method, given that you're also increasing the strain of identifying constructors and the like2 with macros, you have nothing but sheer madness. This is not Sparta. On the other hand, a class-private typedef can be a sane keystroke saver, although I only use such a thing for returning *this from operators in classes with long names.

[1] It's not necessairly a bad one either, but other considerations trump it in terms of importance.
[2] Because the C++ programmer is used to looking for his class name, not CLASS, as his constructor/destructor identifier.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this