Normally it's easy to see unused variables in Netbeans, just a grey squiggly line.
But how would I find all of these unused variables in my project or of a single class?
Reason: I'm debugging a code base which had lots of copy and paste, but it wasn't done carefully. There's many bug of not replacing with the right variable after copy and paste.
You could run something like FindBugs on it.
FindBugs
Looking at the bug list it has
UuF: Unused field (UUF_UNUSED_FIELD)
This field is never used. Consider removing it from the class.
You could filter on just this, but it is a good idea to run this on all code all the time, it is amazing what it finds.
PMD will find unused local variables for you (among many other things). There's a NetBeans plugin that I give installation instructions for here (Warning: Shameless Plug, that links to my blog).
In Eclipse, that gray squiggly line is a yellow squiggly line called a Warning. Then the warning is propagated up to the package level, and up to the project level (such that your project is almost always underlined in yellow with a warning icon). Anyway it really helps you see which source files have warnings in them.
Then your task is to solve every warning in the entire project, and you will have caught the unhandled variables.
I assume netbeans has the same feature somewhere.
The compiler will warn about unused variables giving you your list.
Unused method variables will be removed by the compiler but unused member variables remain, which depending on the state of your codebase may make this a cosmetic problem that can be handled as and when each file is modified rather than a conserted effort to remove all unused variables in one go.
Saying that, I generally like my builds to run with no warnings, so that when I genuinely break something I notice the warning. Maybe this is the clean-up you are looking for ;-)
Related
I've been working as a Java developer for just short of a year now and am gradually trying to increase my knowledge to speed up development times. One of the habits I find myself doing is forcing an occasional clean without really knowing if I need it.
I get that if you use it, your project will be completely re-built, however I don't fully understand under what circumstances I would want to use it.
If I understand correctly, most, if not all, changes to a project will be built automatically, so with that there should only be a rare occasion that I would actually need to use a project clean.
Clean is useful if some external tool modifies your output folder. For example, you are using Eclipse, but occasinally compile via command line compiler into the same folder. In this case Eclipse may fail to do incremental build and display errors in code when they are actually absent.
Another case is working around some bug in Eclipse compiler itself. In very rare cases and in specific Eclipse versions/updates some classes which are necessary to be recompiled after specific code changes might be overlooked by compiler. If you were (un)happy enough to encounter such case, clean will help you.
It is for example useful if you delete a class, and then you forget to remove a reference to it in another class. If you do not clean the project, your local copy will still work, but what you actually did is that you just broke the build. ;)
Your local copy of the project will still work because the .class of the deleted class will still be there. To find out problems like this, is usually a good thing to compile the project from scratch on the integration system.
In my application lot of warnings are coming. For removing that warnings I'm using #SuppressWarnings annotations, anything would be happen in my code if I used several suppress warning annotations.
The #SuppressWarnings annotation does not change anything to the way your code works. The only thing it does is not make your compiler or IDE complain about specific warnings.
If you feel you need to use #SuppressWarnings a lot, then you should take a close look at why you get those warnings. It's a sign that you might be doing things incorrectly - you get warnings for a reason.
The #SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ("deprecation") and unused local variables or unused private methods ("unused"). This article explains the possible values.
Depends on what warnings you are suppressing. If they are related to APIs that available only in new versions, your app will crash on older versions. Some warnings on the other hand are informational and point to common causes of bugs, so it really depends on what warning you are suppressing.
If you mean will my project break? or will it run slower most probably the answer is no. You can be fine suppressing warnings if they are trivial and you understand what are they signaling you and why are they there.
For example, an unused variable warning. Maybe you have defined it and plan to use in the near future, but the warning annoys you. Although I strongly suggest you to use a Source Code Version Control System like Git/Mercurial so you can safely delete code and recover a few days later.
But always check every warning you're suppressing: they are there for a purpose. For example, deprecated warnings: maybe your code runs fine, but in the next version of the JVM that deprecated method/class may have disappeared.
Always understand what you're doing
is it possible to suppress all warnings in IntelliJ for all unchanged lines? I am working in a project with java files larger than 2000 lines and there are warnings everywhere. To get a better overview of my code I want to only inspect my changed or added lines.
Summary from comment conversation:
Bad luck. Turning warnings off is the only solution (no one care about them anyway).
I don't think there is a way to do this. However, going forward you can use the annotation #SuppressWarnings( "unchecked" ) to suppress warnings that you do not care about. This does nothing for existing code unless you apply it to each warning (not what I would recommend), but will achieve the desired effect going forward.
This question is related to, but not a duplicate of, this question.
My issue is slightly different; I have a "utility module", shared between the client and server code, and it contains no GWT-specific code.
I understand that normally, all the sources are pulled into one specific project, where everything is compiled together. But there is one issue with that: I only get to know if my utility project is "GWT compatible", when I compile the main project. This is way too late; I haven't even got around to start on the main project, but I want to know before I make a "commit" to my SCM, that my utility project is "GWT compatible".
In other words, I want to validate the utility project for GWT compatibility, independently from it's use in a separate project (module).
There's a large part of the JRE that is not covered by GWT, and it is particularly likely in a utility module that non-GWT-compatible classes or method be used. That is what I want to validate against.
EDIT: I could add a "dummy entry point", I suppose, but that makes the project depend on GWT, which I don't want to, since it is "general" code, also to be used by people that don't use GWT. If it matters, I use Maven as build system.
EDIT2: No matter what I do, I will only get real compilation/validation with an entry point (does NOT need to reference any of the classes). Neither <force>true</force>, nor <failOnError>true</failOnError> will do. Is there a way I can define that entry point, for the shared project, such that only gwt-maven-plugin sees it, but not javac (so as not to add an unneeded dependency in the Java code)?
The compiler actually always visits all code on the source path (note: not quite the same as the classpath), by starting at the requested module with any <source> tags, and then checking each <inherits> along the way. If it finds something that isn't compatible or isn't compilable, it will mark it as broken, and move on - as long as nothing actually depends on it (i.e. an EntryPoint, or something that an EntryPoint depends on) you'll just see this message:
Validating newly compiled units
Ignored 1 unit with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
If you include that -strict flag, the compile will actually fail when it hits something that can't be included correctly.
This work is done in the very early stages of the compile, while constructing the TypeOracle, which is used for Generators, long before any JS is built. That type oracle is passed to generators, which need to be able to ask questions like 'what interfaces on the sourcepath have a JSO implementation' and 'what are all possible subclasses of List'. Generators can do a huge number of things, including emit even more types which then need to be parsed, compiled, and the process continues until a full JProgram is created of all possible types, based on the current set of modules.
That JProgram then gets compiled down based on what can be reached from the roots - the entrypoint, as well as a few other details such as how to emulate Java details like casts, arrays, longs, exceptions, etc.
If -strict was not specified, and the compiler ends up needing to reach something which is unavailable due to earlier compilation problems, that is the time you find out. Using -strict to stop earlier will help ensure that you catch those issues sooner.
One more fun fact: By default, with com.google.gwt.user.User in your module (or any other <inherits> that depends on it), you already have an entrypoint, or several! These do some quick checking that your page is working correctly, such as using a strict doctype, or the browser actually matching the expected user.agent setting. This means that it is usually possible to compile a module even without an entrypoint (except with gwt-maven-plugin:compile, which will not consider a module for compilation just by those built-in ones).
EDIT: Okay, even one more: From http://www.gwtproject.org/doc/latest/DevGuideCompilingAndDebugging.html, combined with -strict, it looks like you can force the validation to run without actually compiling to JS:
-validateOnly Validate all source code, but do not compile
I don't think it's possible because the GWT compiler does not compile any unused code.
This means that your shared utility "module" may have code in it that is not compatible with GWT, but it will not cause any problems as long as GWT code never calls such incompatible classes or methods. Without an entry point GWT compiler won't know which code is used and which is not - it will assume that all of it is unused.
I was wondering what are the main reasons why eclipse warns developers about the following things:
The value of the local variable xxx is not used
The import XXX is never used
I'm thinking that one reason is to make code cleaner. Anyone knows a list of reasons behind those warnings (like memory concerns, and things like that)?
Thanks!
It's mostly to make the code readable and avoid clutter - it's easy to loose track when a file starts getting large, and having non-functional lines of code are therefor best to avoid. It's mostly for readability... Well, and by removing unused imports, you ease the compilers job ever so slightly (read: very slightly).
If Eclipse notice an unused import or variable, you can be damn sure that the compiler does so as well (since eclipse is actually compiling the code while you code to generate those warnings). The compiler will try to optimize it as much as possible, so if it sees an unused variable (or import), it doesn't bother to include them in the compiled byte code.
But it's generally good coding style not to have unused code;
In Java and (most) other high-level languages, you generally don't have to worry about those things, since you don't have to manage memory-allocation. But supposing the compiler didn't catch it for whatever reason when compiling (perhaps if you were writing in another programming language), the object or data referenced by a variable would then be taking up memory space, and if you a lot of those unused variables that all took up memory space... that could potentially be a lot of memory used for nothing.
Yes why should you include code, that is never used?
I guess the compiler will remove this unused code anyway, but you should keep the code as clean as possible by yourself.
The value of the local variable xxx is not used
It is declared when you only declare or assign a value to a variable, but you never used it for anything. It is warning you because it does nothing in your code, so it would be better to remove it.
The import XXX is never used
The same thing as the other one. There is no need to import a classe you won't use, so Eclipse recommends you to remove the import, since it does nothing but pollute the code.
This prevents you that you writed Unnecessary code. According to the help of Eclipse (Java Compiler Errors/Warnings Preferences) this says :
"When enabled, the compiler will issue an error or a warning whenever
a local variable is declared but its value never used within its
scope."
you are unnecessarily declared/imported the variable/import.
Removing them keeps the code cleaner and easier to read.By default Eclipse warns you about unused private variables and methods.
But do not change these warning preferences,It's helpful to identify your unused code.