Jump to content

Find/remove unused variables from source?


Recommended Posts

Is there a tool that will read my source code and report any variables that I declare but never use? For the past three years, I've been modifying and expanding a script that is now 1,500 lines long, and I declare around a hundred variables at the top of the script.

After all the editing I've done on the script, I think some of these variables never get used. Is there an easy way to generate a list of them so that I can edit them out of my source?

Many thanks for any help.

Link to comment
Share on other sites

write a script that tries to find each variable, and if it doesn't have it delete that variables declaration.

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

@Edward Mendelson

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Maybe theres easier way to do that...ask to Jos for it >_<

Cheers, FireFox.

Link to comment
Share on other sites

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Thanks for that. However, I had already tried it - the obfuscated script still contains statements such as

Dim $unusedVar

even though $unusedVar is never used in the rest of the script.

Perhaps the best way is to write a script that modifies my script...

Link to comment
Share on other sites

I think your best bet is another script to read your script... How about something like this?

#include <Array.au3>

Dim $filename = "Your Script File.au3"
Dim $variables[1]
Dim $RegExp

$varFileContents = FileRead ($filename)

$RegExp = StringRegExp ($varFileContents, "(\$\w{1,50})(?:\s|\[)", 3)
If Not @error Then
    For $i = 0 to UBound($RegExp) - 1
        StringReplace ($varFileContents, $RegExp[$i], "")
        
;~      MsgBox (0, "", "Variable Name: " & $RegExp[$i] & @CRLF & "Number of times it occurs: " & @extended)
        
        If @extended = 1 Then
            $variables[UBound($variables)-1] = $RegExp[$i]
            ReDim $variables[UBound($variables+1)]
        EndIf
    Next
EndIf

_ArrayDisplay($variables, "Variables Used Only Once")

I'm pretty sure the Regular Expression should cover most variable/array names, but it could require a little tweaking.

Edited by exodius
Link to comment
Share on other sites

I think your best bet is another script to read your script... How about something like this?

That script worked beautifully - it even found a variable that I had misspelled in a comment block. Many thanks for that.

I think this script is worth making available permanently in the Example Scripts section of this forum. I've been using it on my other scripts and finding it very useful.

Thanks again!

Link to comment
Share on other sites

  • Developers

@Edward Mendelson

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Maybe theres easier way to do that...ask to Jos for it >_<

Cheers, FireFox.

The simple way is to add the AutoIt3Wrapper directive which will run obfuscator and compile the stripped source.

A work of warning about the posted Regex script: Better be sure your script isn't using anything like Eval() because that uses variables without using the $.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...