Thread: Loops in functions?

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Multiline macros are very powerful and used correctly, is the right thing because it makes the code more managable. But this particular case sounds like "not the right place".

    There are several things to consider when writing code:
    1. Readability. The ability for a new reader [or yourself after 4 months] to read the code and understand what it does. Multiline macros make the code harder to read, because you need to understand what's happening in the macro expansion.

    2. Debuggability. The ability to understand what goes on inside the macro, when single-stepping the code in a debugger is nearly non-existant [unless you switch to assembler mode, and step through the assembly code - doing that for a three-level nested for-loop macro would NOT be my idea of "easy", and I have done my fair share of debugging in assembler]. Most C debuggers treat one source line as one step, so when you step on a line that contains your multi-line macro, then it will just do ONE step all across the entire macro, not one portion at a time.

    3. Portability/extensibility. The ability to change the code to do something slightly different. Again, multiline macros make this very difficult.

    4. Performance. Multiline macros make little difference here.

    In the end, it's your code, you decide how you solve your problems. But I think you are doing this "because it's hard work typing", rather than because you're concerned about some of the above.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Join Date
    Oct 2007
    Inside my computer
    Quote Originally Posted by AmbliKai View Post
    so i'd be as well just typing it out each time?
    Safest bet, yes. Should you do it? Up to you.

    Quote Originally Posted by AmbliKai View Post
    There are a lot of places in my program where i use the above construct. with code sometimes after x or y etc.
    Its not bad programming practice to leave them as is?
    It's bad programming practice to leave your loops intact. If, however, you use similar code after, before and in your loops, you can do the function pointers method, which is much more preffered.

    Quote Originally Posted by AmbliKai View Post
    I sort of thought that if i just typed out the entire program, as is in one source file that it would be considered bad practice?
    Bad practice? Well, maybe. You usually use different files to separate different parts of the program. Different dialogs might have different files. Different classes might have different files. Yes, I would suggest that you do split your project and code in files since it's also much easier to read and work with rather than one file with 100.000 lines of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM
  5. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM