Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Basically I ran Intellij Idea 's plug in called FindBugs-Idea to analyze my code . I corrected whatever errror was caught and then ran the tool again but it was still complaining about the same error even though it was fixed in the source file . It only stopped complaining after I recompiled my classses . Why does it require a compilation step ? Shouldn't it basically look in the source file and detect a possible bug ? How do these tools work in general ?

share|improve this question
    
If it strived to be universal across bytecode it looks at bytecode... –  Rig Jul 26 '12 at 16:20
    
Depends on what the error was. Among other things, it's possible that there is code added solely at compile time, which would mean analysis of the code before then won't show the complete picture. I'm also assuming you don't have 'automatically build' (or the equivalent) turned on, as normally (in eclipse), saving a file kicks of an incremental compile. –  Clockwork-Muse Jul 26 '12 at 16:24
add comment

2 Answers 2

up vote 5 down vote accepted

Findbugs examines the bytecode to identify patterns that tend to be buggy.

This is far more efficient than analyzing source code. You would essentially have to parse the source anyway to analyze it, so let the existing compiler do that work for you and focus on finding bugs.

By working with compiled bytecode, FindBugs can treat your code as identical to the third-party libraries that you use, and investigate all of them (to ensure that you're calling the library correctly).

share|improve this answer
2  
"This is far more efficient than analyzing source code." That's not a true statement. You find different types of bugs depending if you analyze source code versus a compiled product (in this case, Java bytecode). For example, PMD looks for problems by analyzing source code and identifies bad form that doesn't apply to compiled code (eg - readability issues) or things that might be optimized out (unused variables). –  Thomas Owens Jul 26 '12 at 17:00
    
@Thomas - it is very much a true statement. If you are going to analyze program flow, then it is far more efficient to work from a representation that has all the distracting bits like comments and variable names stripped out. The fact that PMD can perform simple semantic checks by scanning source does not say anything about its efficiency. Nor does its ability to find "readability" issues say anything about its efficiency. –  parsifal Jul 26 '12 at 17:14
    
For a specific example, let's consider unused variables. For a source implementation, you have to (1) parse the method, (2) identify the variables in the syntax tree, and (3) lexically scan the tree for each variable that you've found. By comparison, if you have the bytecode, you simply walk through it looking for load and store operations. If you find that there aren't any load/store operations to a particular frame cell, then you do the lookup to figure out what was assigned to that cell. –  parsifal Jul 26 '12 at 17:18
    
You can't compare bytecode analysis tools to source code analysis tools because they do different things. Therefore, saying that one method is more efficient doesn't make sense. It might be less intensive to process, but when you consider that you find different defects, there's a different value in performing each analysis. Depending on the quality drivers in the project, it might be more valuable to run one kind of analysis over the other, or to use both in conjunction. –  Thomas Owens Jul 26 '12 at 17:29
    
@Thomas - I just did compare them, in a case where they did do the same thing. I could make a similar comparison for every code-correctness example on the PMD home page. If you want to say that PMD does something different than FindBugs, that's fine, but it's completely irrelevant to what the OP asked. "Readability" is not a bug. –  parsifal Jul 26 '12 at 17:41
add comment

There are different types of static analysis tools - those that operate on the source code and those that operate on a compiled product. Findbugs is an example of the latter, while a tool like PMD is an example of the former. Depending on what type of problems the tool is trying to detect, the choice of what product to analyze can make it easier or more difficult. As an example, tools that analyze the byte code can't be used to find problems that lead to code being less readable, but they can analyze anything that compiles to the same byte code.

share|improve this answer
    
Why do people require a tool to tell them if their code is readable or not? –  James Jul 26 '12 at 20:40
    
@James It's typically used to compare a very large code base against a rule set. It's easier to read consistently formatted code that adheres to certain well-defined rules. The ability to check code against those rules can not only improve readability, but also prevent defects. An example is var == const versus const == var. Some people prefer all expressions to be of the form const == var because const = var is an invalid expression that can be detected - you enforce a rule to improve readability and prevent mistakes in the code that lead to defects being injected. –  Thomas Owens Jul 27 '12 at 10:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.