Recent

Author Topic: Searching for all unused global elements in the project  (Read 9588 times)

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Searching for all unused global elements in the project
« Reply #15 on: January 16, 2019, 01:32:14 am »
Well, I said there should be a reason, and perhaps you have one, but I could hardly imagine 300 const identifiers in platformer game :).

Hmm… I thought it was not much. 8-)

Many of these constants are used only in one place in the project (outside the Platformer.Constants.pp unit) and are declared so as not to use hard-coded magic numbers and other literals (whose meanings I would forget after a few days).

I chose this way of gathering constants (in a separate unit), because thanks to this unit it becomes something like a control panel. While calibrating various elements of the game, I modify the content of only one unit.

Quote
The only kind of automation I'm thinking about is to use CodeTools' parser and write your own cleanup procedure (not easy).

If no one needs such a tool, I can write it myself, install into the IDE and use. But certainly not in the near future, due to the lack of free time and due to the lack of sufficient knowledge in the field of extending environment functionality.
« Last Edit: January 16, 2019, 02:18:22 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Searching for all unused global elements in the project
« Reply #16 on: January 16, 2019, 06:29:47 pm »
Normally, one should have a reason for identifier to be in global public scope. And if you have a lot of identifiers that became "unused", you should reconsider your code structure. Not just "clean unused".

I do not understand why the need to find unused "identifiers" is considered by you as a sign of poor code quality.
It is in certain way.

It is a symptom of poor design (if there is one) or sloppy implementation. It's very common in projects born as a proof of concept or experiments which suddenly become serious without: stopping to look what was done and how could be improved. Then outline a basic diagram (anyone you think could help you), how you will allocate your resources and constants. Question any global variable, constant, resource, and type: When are needed? Which class or process needs it? Is it OK that could bee seen or used in places where is not needed? etc.

I can tell that because of different reasons I usually end up in the same situation as you are now. And always try to throw everything to the garbage and start over again but with a plan or design.  :-[  :'(

Now every time I have to do a proof of concept or an experiment I treat it like a NEW PROJECT  :(

But with the things we have now, the only workaround I can think of is to move all constants, types and vars to the implementation section.

Yes, but it is a different way of manual search.

It is faster to check the identifiers with the Find in Files tool, which can be easily operated from the keyboard. If the list in the Search Results window contains only one item (declaration line), it means that the constant is not used anywhere else.

Believe me. Probably the project needs it. You will be surprised of what you'll find: methods, functions and procedures which are not used which in turn use constants or types not used in other places, things that should be in other unit, etc.


But I repeat
Yes, but I think it would be good to have a tool to show you unused element in the interface if you want to know them.
A tool or the compiler

You should open a Ticket in the bug tracker as requirement for the compiler....

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Searching for all unused global elements in the project
« Reply #17 on: January 16, 2019, 07:33:24 pm »
It's very common in projects born as a proof of concept or experiments which suddenly become serious without: stopping to look what was done and how could be improved.

Yes, this project was made as PoC and it still is. It was never meant to be a full-fledged game, but it just happened that it almost became it. But that's the way it will be, because I'm not going to develop it in the future.

I have stopped many times and corrected the old code so that it would cooperate with future new functions, but I never thought about throwing away the project and starting again.

Quote
Then outline a basic diagram (anyone you think could help you), how you will allocate your resources and constants.

When they are needed variables are created and right after that they are released.

For example, if a given class is needed throughout the game, it is created during the creation of the main game class and released when it is released. If a class is needed, for example, only in one scene, it is created during the initialization of the scene and released when it is released.

I pay attention to this, so as not to leave unused resources unnecessary.

Quote
Question any global variable, constant, resource, and type: When are needed?

Practically throughout the entire process.

Quote
Which class or process needs it?

Many classes use the same constants, which is why they are global. If any constant is used only in one unit, it is local.

Quote
Is it OK that could bee seen or used in places where is not needed? etc.

No, it's not good, but there is no tragedy either. Sometimes there is no way to easily hide all the elements, that's why stays as global, without complicate the whole code.

Quote
Believe me. Probably the project needs it. You will be surprised of what you'll find: methods, functions and procedures which are not used which in turn use constants or types not used in other places, things that should be in other unit, etc.

Yesterday I took care of it and manually checked all constants and other identifiers. I found two unused constants and no unused class or routine. It cost me almost two hours, and with the help of a dedicated tool it would cost me a few seconds. :P

Quote
A tool or the compiler
[…]
You should open a Ticket in the bug tracker as requirement for the compiler....

I'm not sure. If the compiler will report such elements, then during the compilation of a regular library/package a lot of misleading suggestions will pop up. That's why I would prefer an IDE plugin and use it only when it makes sense.

But who knows, maybe some special option for the compiler (turned off by default) would be a better solution.



This project is quite specific. Certain constructions I used only to check how they will look in practice (because I have never used them before), and some exist only for a joke (e.g. one routine with goto's instead of a regular loops). It is a one big experiment.

I think that with the code evaluation we will wait until the publication of the whole project, because in this thread such discussions do not make sense. But thanks for replies!
« Last Edit: January 16, 2019, 07:53:47 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Searching for all unused global elements in the project
« Reply #18 on: January 16, 2019, 08:30:15 pm »
Quote
some special option for the compiler (turned off by default) would be a better solution.
That will be the ideal. Though I don't know if it easy to implement or not. FPC core developers should know. With a ticket at least one of them will take it if it's possible or a no go.

argb32

  • Jr. Member
  • **
  • Posts: 89
    • Pascal IDE based on IntelliJ platform
Re: Searching for all unused global elements in the project
« Reply #19 on: January 17, 2019, 08:51:07 pm »
In I-Pascal there is an inspection which shows unused local identifiers.
The only reason why there is no such for global identifiers is because it's not needed for each or most projects.
If you are interested I can add the inspection (disabled by default) for global identifiers to I-Pascal.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Searching for all unused global elements in the project
« Reply #20 on: January 18, 2019, 07:27:48 pm »
The only reason why there is no such for global identifiers is because it's not needed for each or most projects.

I agree with this statement only if the majority of created projects are libraries, packages and everything else that is non-executable. I do not know the statistics, so all I can do is suppose. And according to my conjecture, the most often created projects are executable applications.

Quote
If you are interested I can add the inspection (disabled by default) for global identifiers to I-Pascal.

That's nice, but I do not want to change IDE. But if you also think that such a functionality is useful, then for the sake of your project you can add it.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Searching for all unused global elements in the project
« Reply #21 on: January 18, 2019, 11:44:01 pm »
I agree with this statement only if the majority of created projects are libraries, packages and everything else that is non-executable.
I don't think that's the logic behind the "No interface's element will be reported as not used" thing...

I think the logic is "what you put in the interface is something you want to share that element with other units so can be used at will" sort of thing. Thus it SHOULD not be tagged with a note. If so, I do not agree with that logic either. Sometimes unit's header grows bigger and bigger and you can lose track of an element you need to remove (or whatever the reason why an element stay in the code as garbage) and it stays there using resources and not being used...

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Searching for all unused global elements in the project
« Reply #22 on: January 19, 2019, 12:23:28 am »
I don't think that's the logic behind the "No interface's element will be reported as not used" thing...

[…]

I think the logic is "what you put in the interface is something you want to share that element with other units so can be used at will" sort of thing.

Of course, however, this is due to the code structure design of the units.


A short example. We have a class declared in interface section, whose properties are indexed with enums. The enumeration type must be declared above the class to be visible to it and not to be global (becouse it is used internally only, there is no way to use it in other unit anyway).

We have a problem—or the enumeration type will be visible globally (declared outside a class, above it in the same section), or it must be declared (embedded) inside the class, for example in private section. The same applies to constants that are used to specify default property values. Does anyone in such a case declare such elements within the class to actually be internal?

Similar cases may be more.
« Last Edit: January 19, 2019, 12:24:59 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018