Refactoring

This is a discussion on Refactoring within the C Programming forums, part of the General Programming Boards category; Does anyone here know of a tool that will let me remove global variable from a source code ? I ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    Refactoring

    Does anyone here know of a tool that will let me remove global variable from a source code ?
    I understand that removing global variable will affect the semantics of the code but this is for a different analysis purpose.

    Your help is much appreiciated !

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    notepad works. use the delete or backspace keys. do you mean something different than this?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    update

    Quote Originally Posted by robwhit View Post
    notepad works. use the delete or backspace keys. do you mean something different than this?
    Yes I dont want to replace the variable of update the variable name. I mean I want to get rid of the global variable from the source code. That is no existance of the global variable. For exammple:
    Code:
    int x =5;         >>>>>>>>>>>  this line will be removed.
    
    int main {
    a =x;     >>>>>> this line will be replaced by a =1
    
    }
    so the code will still complie. It might not do what it was intended for but it will compile.

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    edit/replace

    replace
    x
    with
    1

    Works in any good text editor.
    Don't quote me on that... ...seriously

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if the variable is just used in the main function, just move it to inside the main braces.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    Its not that simple. Suppose i wanted to remove a global variable from a 15000 line code ? How do i go about doing that. Replace and edit wont work cause a variable can have an assignment or a usage.

    I want a tool that can differentiate. Some some of tool that does parser based transformation

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it depends how it's used. do you know how it's used?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Learn regular expressions then. But I still don't see the point in what your trying to do, nor how the code is going to run after you've edited it :|

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kawshik View Post
    Does anyone here know of a tool that will let me remove global variable from a source code ?
    I understand that removing global variable will affect the semantics of the code but this is for a different analysis purpose.

    Your help is much appreiciated !

    Thanks
    Well, you won't probably appreciate the answer!

    What you want could be done by a smart program using logical inferences, but only on a simple program.

    There is no program or tool that will reliably handle long and complex code, replacing all the global variables with local one's, and still leave a code with the same functionality.

    That's what a programmer does, not a program.

    Although the program may be 15,000 lines long, the real questions are:

    1) Do you understand the code?
    2) How many global variables does it have?
    3) How many functions does it have that need access to one or more global variables?

    You'll probably find that the task is not as difficult as you believed, but you will have to have some programming skills to make these changes. No way around it that I'm aware of.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by kawshik View Post
    Yes I dont want to replace the variable of update the variable name. I mean I want to get rid of the global variable from the source code. That is no existance of the global variable. For exammple:
    Code:
    int x =5;         >>>>>>>>>>>  this line will be removed.
    
    int main {
    a =x;     >>>>>> this line will be replaced by a =1
    
    }
    so the code will still complie. It might not do what it was intended for but it will compile.
    In that case, the best thing to do would be to just put a const in front of the declaration.
    Hey it still gets rid of the global variable, because it becomes a global constant instead!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And what about, something like:

    Code:
    #include <stdio.h>
    
    FILE * thingy = stdin;
    
    int main(void)
    {
        fprintf(thingy, "Hello");
        return 0;	
    }
    or:
    Code:
    #include <stdio.h>
    
    int x = 5;
    
    void DoSomething(void);
    
    int main(void)
    {
        printf("x = &#37;d\n", x);
        DoSomething();
        printf("x = %d\n", x);
        return 0;	
    }
    
    void DoSomething(void)
    {
        x = 10;
        return;
    }
    Which should print:
    Code:
    x = 5
    x = 10
    I don't see how that'll work if you get rid of globals (ie move X's scope).
    Last edited by zacs7; 06-28-2007 at 02:18 AM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you could write it as a local variable in main and then just throw its address around to wherever you want it, but that's likely to be hackery and have worse performance than using globals.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    Wasn't this same question asked some time ago?

    Anyway, try these
    http://www.gccxml.org/HTML/Index.html
    http://hal.cs.berkeley.edu/cil/
    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.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Still, your not going to do it very easily. If you manage to do it via regex's, I'd like to see how complex the expression is

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Refactoring browsers for C and C++
    By dwks in forum Tech Board
    Replies: 22
    Last Post: 11-25-2007, 06:23 PM
  2. C++ Refactoring Tools
    By SpaceCadet in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2006, 07:03 PM

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