How to get rid of orphan definitions?

BlitzMax Forums/BlitzMax Programming/How to get rid of orphan definitions?

Codebox(Posted 2015) [#1]
I am currently refactoring a code which has extensive use of Globals (and Const) and it's only using functions as well. (No Types as abstraction level usage or classes at all)
Bad part about it: it was originally some kind of a game-engine-framework-mix-piece which contains variables for 5 different setups (or games).
Already one game is left (functions), but - hard to guess, I know -, the variables still in the source.
My question is, how can I easily check the source if there are orphan Definitions of Globals. Anyone got a hint... a tool or something specific to blitzmax language?

I use BlIDE for coding and the Analyzer works great, but lacks exactly this feature :-/

any help is appreciated :-)


Derron(Posted 2015) [#2]
Didnt Blide tell if a variable was unused? Last time I checked it (some years ago) I thought it mentions it somewhere. Maybe "today" it just colorizes it differently.

If you have "block definitions" (areas containing global after global) you could just block-comment them (rem...endrem) and then try to compile. Each blamed "missing" or unknown variable is then put back into life.
Tedious but I doubt you have thousands of variables to check. So instead of waiting for the right tool you might already have finished refactoring.

bye
Ron


Codebox(Posted 2015) [#3]
Well, regardless which "blocking" I use (function, type etc.), it doesnt work. So I'll have to use the commenting-out-way.

Oh.. not thousands ... but around 800 ....

Well.. refactoring .. no one told it's easy :D

neverless, Danke :-)


Derron(Posted 2015) [#4]
with blocking I meant

global a:int = 1
function funca()
End function

global b:int = 1
function funcb()
  global c:int = 1
End function



compared to
global a:int = 1
global b:int = 1
global c:int = 1

function funca()
End function

function funcb()
End function


Another option is to "search in all documents" - search for your global in all documents (source files) and if result count = 1 ... then it is unused.


bye
Ron


videz(Posted 2015) [#5]
hmm. im not using bmax but still related to programming and blitz..

What I would do is get a search file content tool and find those global names you are aiming for. There's some search utilities that will list them including the file and line number where they appear. then you can evaluate and start refactoring those orphans ;)

I could not give you a specific tool to use because its been a long trade secret of mine but google is your friend :)


Xerra(Posted 2015) [#6]
Blide does indeed tell you if a variable is unused in a listing. It's underlined in wavy green lines and hovering your mouse over it gives you some message about that "variable is never referenced anywhere in your code"

I can't give the exact information as I'm at work and can't test it but it's something along those lines.


Codebox(Posted 2015) [#7]
I tried some stuff out and the Code-Analyzer, besides some nice checks about bad practices (Type-Declaration within Functions (just testing :-D)) doesn't check orphaned globals... only locals.

Try
SuperStrict
Global unused:int

and the analyzer doesn't "respond".. but
SuperStrict
Local unused:int

and et voila, you've got a warning. regardless which scope the orphaned declaration is.

Actually I use the commenting-out-way and make some good progress... STRG+SHIFT+M, STRG+F5 .. succeed? delete Line, else i will be thrown into error-location, pressing STRG+Z and move on to next line ...


Zethrax(Posted 2015) [#8]
Get yourself a good text/code editor (such as EditPlus - https://www.editplus.com/ ) that has Find-In-Files search functionality. You can go through your identifiers as you encounter them and generate a list of all their instances with the files, lines and character positions listed. In the case of EditPlus you can double-click on the listing and it will open the file at the position where the identifier appears.

I've used this feature to clean up code similar to what you describe in the past. It's time consuming, but far less so than going through all your code and eyeballing it manually.

But if you're looking for a fun coding project then it probably wouldn't be massively difficult to create a program to detect orphaned identifiers. I'm tempted to write something similar as I have quite a bit of code that's suffered indignity from my past experiments in identifier markup standards.


ziggy(Posted 2015) [#9]
@Zethrax: BLIde does this too, there's no need for any additional tool.


Yasha(Posted 2015) [#10]
As an aside, depending on the project, not all definitions that are uncalled in visible code are necessarily "orphaned".

e.g. if you use a lot of reflection, a method might only ever be accessed by dynamic name lookup at runtime, which can't be determined from a static analysis. This is a pattern I use a lot (handy to separate your driver loop/navigator from your data operations, via dynamic delegate).


Derron(Posted 2015) [#11]
But with default reflection code you wont be able to access "globals" by name ... so for "globals" the analysis might work in most cases.

For methods ... of course, that is why for methods the message should be "propably never called" instead "is not called".


bye
Ron


zoqfotpik(Posted 2015) [#12]
This is a good job for unix tools like awk.

Make a list of words immediately following "Function."

Search your code for those words and count occurrences of those words (grep -v blah | wc -l or something like that).

For each word whose count is only 1 (that is, for the function definition itself refers to that word) you have an orphan.

Remove each orphan.

Run the whole process again, since one orphan might be referenced by code inside of another.

Pro Tip: Don't actually make the removal automatic, do that step yourself and comment it out instead of removing it, and it might be a great idea to do this one function at a time, with testing in between (you do have full coverage of unit tests, right?) to make sure your program still runs fully.