i should know this, but i'm too lazy to look it up

This is a discussion on i should know this, but i'm too lazy to look it up within the C++ Programming forums, part of the General Programming Boards category; actually, i just have a ton of other things to look up... forgive this elementary question can i make this ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    i should know this, but i'm too lazy to look it up

    actually, i just have a ton of other things to look up...

    forgive this elementary question

    can i make this any shorter?

    #define isOp(x) (x=='-'||x=='+'||x=='/'||x=='*')

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Shorter in what - time or space?
    I can make it shorter (by source code character count), but it will take longer to execute.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    either or......i was a bit worried that that way was a bit inefficient (because i have a couple of much large macros that are the same, uh, format as that)


    ...i guess the question is: what's the best way to do that? (define 'best' on your own terms)

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    since its hardly that big i would go with the one that gives better performance... rather than smaller code

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by misplaced
    either or......i was a bit worried that that way was a bit inefficient (because i have a couple of much large macros that are the same, uh, format as that)


    ...i guess the question is: what's the best way to do that? (define 'best' on your own terms)
    If you have to ask this question, I think the answer is:

    The "best" way is the code that you can see, at a glance, is doing exactly what you want it to. Making a function that requires fewer keystrokes or cleverly using some technique that is not at first obvious to you is OK if you like puzzles, but could bring you grief some time in the future.

    For example: This is part of a complicated program that doesn't work. As you are debugging the program, you keep coming back to your "clever" code for this simple function. The problem is actually somewhere else, but this is such a distraction that you can't actually see the real problem. Finally, you go back and plug in some obvious code in place of your cleverness so that you can continue debugging. Why not just put the obvious (to you) code in place from the start?

    Just my opinion. Your Mileage May Vary.

    Regards,

    Dave

    p.s. widescale use of macros (especially complicated ones) has caused me lots of wasted debug time. Lots of times I have taken macros (from other people's code) and made functions of them so that I can find places where they were invoked with inappropriate arguments. Use of functions rather than macros allows type checking of arguments by the compiler. Sometimes this makes errors obvious.
    Last edited by Dave Evans; 10-02-2004 at 08:23 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Well in the first instance you should be going for readability.

    > #define isOp(x) (x=='-'||x=='+'||x=='/'||x=='*')
    Eg.
    #define isOp(x) (strchr("+-*/",x)!=NULL)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    Why not use inline functions instead of such kind of macros.

    mfg JJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Too lazy to chew your donuts...
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 07-22-2004, 08:17 PM
  3. lazy delete
    By kuwait in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2003, 02:45 PM
  4. Programmers getting lazy?
    By Travis Dane in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 02-19-2003, 06:04 PM
  5. IT people - weird genius or simply narrow-minded, antisocial, lazy people
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2001, 05:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21