Find unused methods

Is there a way to check if the project has unused methods in it ?

thanks

Sometimes i right-click on Method Names in the Navigator and do a Search for the Method Name. This is the only way i know of.

Rename the method and see who is calling it from the error. If you don’t get an error then it isn’t being used.

But this means you need to run a debug build each time. I like my way of searching for unused Methods more, because it’s faster :smiley:

You can also deprecate them. Just add the an attribute to the method named Deprecated. Then analyze the project.

If you’re using this for its intended use, you can put the replacement method in the value field surrounded by quotes and that will also be shown when analyzing the project.

To each their own. I tend to do what Markus does.

One thing to be aware of, though, is that if you use a lot of conditional compile directives by platform (example #if TargetWin32) you need need to exercise the compiler in all targets to find where it’s used. It’s quite possible the method is called only for a particular target.

[quote=208648:@Bob Keeney]To each their own. I tend to do what Markus does.

One thing to be aware of, though, is that if you use a lot of conditional compile directives by platform (example #if TargetWin32) you need need to exercise the compiler in all targets to find where it’s used. It’s quite possible the method is called only for a particular target.[/quote]

A nice enhancement would be if the check project process could check all targets as having to build all targets to find errors is very annoying.

Sign on to case <https://xojo.com/issue/5095>

Have done, thanks.

Keep in mind that only the “find” suggestion may work if you call a method via Introspection.

But then you’ve left the safety of port and should probably be regularly sacrificing a small animal to your diety of choice. The tools can no longer help you.

Since I’ve started with localization I’ve wanted a way to find unused constants so that I find out which translations aren’t needed anymore. Is there a way to do this?

Find
There no analysis that tells you it is unused - regardless of target

Would not that be a job that Arbed could carry ?

[quote=208738:@Norman Palardy]Find
There no analysis that tells you it is unused - regardless of target[/quote]

Not yet :wink:

You might as well write a compiler for Xojo at that point

Without going as far, would it not be possible to collect all sub and function names, then verify which ones are called at least once ?

Anyway, is not the Xojo compiler astute enough to discard unused methods ? Especially the coming LLVM ?

There are a couple of things that make finding unused methods difficult. The biggest two are introspection metadata and the fact that the compiler never sees all of the code at one time. It’s only during the link phase that it can deterministically say what is used or unused.

LLVM is only for code generation. It isn’t magic.

So does that mean that the linker will discard unused code methods ?

Yes, but I suspect the linker has a different unused than you do. From the linker’s perspective, the program is a graph and anything that is reachable from the roots is kept.

The catch is that when a class is referenced, it references all of its members for the purposes of introspection metadata and virtual dispatch. It also refers to the superclass, if it has one, and any interfaces that the class implements.

This can end up bringing in a lot more of the program than you would expect. For example, an empty console application brings in RGBSurface due to this chain:
App → ConsoleApplication.ExecutableFile → FolderItem.OpenAsPicture → Picture.RGBSurface