Skip to content

Java code linting - #1593

Merged
FridaTveit merged 82 commits into
exercism:masterfrom
mirkoperillo:java-linting
Oct 29, 2018
Merged

FridaTveit merged 82 commits into
exercism:masterfrom
mirkoperillo:java-linting

Conversation

@mirkoperillo

Copy link
Copy Markdown
Contributor

This is a early proposal for issue #1006.
I used robot-simulator exercise to this spike adding findbugs plugin to gradle.
Actually the build fails every findbugs best practise warning during the check task
Yo can try it adding something bad to RobotTest.java, i.e

List badList = new ArrayList();
badList.add(new Integer(12));

A HTML report file is produces in build/reports/findbugs folder.
The findbugs analyzes the main e test code (not the reference implementation for the moment)

Is this the right way ?


Reviewer Resources:

Track Policies

@sjwarner-bp sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mirkoperillo - thanks for picking this one up.

I've never used FindBugs, so please correct me if I am wrong, but I think this plugin is normally used to flag possible exceptions etc. and runs against byte code.

When the idea of linting was proposed, I think it was more geared towards coding style and convention than anything else.

What do you think @exercism/java ? 🙂

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

Hi @sjwarner-bp , findbugs runs on byte code and try to found smell code, if you prefer focus on coding style I'll try to give a check to Checkstyle

@FridaTveit

Copy link
Copy Markdown
Contributor

Thanks for this work @mirkoperillo! It's great that someone wants to help us with this 😄

I think I agree with @sjwarner-bp that linting (e.g. something like CheckStyle) is what we'd prefer to focus on. And I think the code we'd prefer to focus on having checked is the test code and the reference solution. After all the user impemented solution will be reviewed by mentors and it might be confusing for beginners to have to deal with linting. Do you agree @sjwarner-bp @mirkoperillo? 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

That's exactly right @FridaTveit - I think that we only have to worry about things internal to this repo so we end up with a bit more consistency (and hopefully it will pick up common small issues in code to give maintainers better focus on the actual logic)! 🙂

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

@FridaTveit @sjwarner-bp now I understand what you need.
I'll move from findbugs to checkstyle plugin, I think to propose something to you in a couple of days, stay tuned 😆

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

Added checkstyle plugin and removed findbugs one.
Actually I used a checkstyle configuration found online as sample, you can find it in config/checkstyle/checkstyle.xml file.
Checkstyle tasks work on reference main and test classes. If you try to run gradle check you can see some style violations.

@Smarticles101 Smarticles101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way for it to print directly to the console instead of generating a report?

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

@Smarticles101 the plugin does both things, do you intend that the report should not be created ?

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking promising @mirkoperillo, thanks for taking the time to do this! 😄

My main issue with it at the moment is that I believe the build.gradle file inside each exercise gets delivered to the users when they download the exercise. So I'd rather that file didn't mention checkstyle at all, so as to not confuse the users. Maybe we could add a checkstyle task to the build.gradle file in the exercises directory instead? To add a step that will be run as part of the travis build and preferably that contributors can run on its own as well if possible? 🙂

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

@FridaTveit ok, I'll work on this.
Another thing: actually the configuration of the style is in the directory of the exercise, maybe it is better to move it in a more centralized place. Do you think that config repo folder could be a good place ?

@FridaTveit

Copy link
Copy Markdown
Contributor

Good question @mirkoperillo 🙂 I believe that config folder contains config for what to display on the exercism website for this track, so that might not be the best place for it. I would suggest either the exercises directory or the root of this repo. What do you think @sjwarner-bp @Smarticles101 @mirkoperillo? 🙂

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great @mirkoperillo! 😄 I've gone through the rules and added some thoughts and questions. Let me know what you think! 🙂

-->
<property name="format" value="((//.*)|(\*.*))TODO[^: (]" />
<property name="message" value='All TODOs should be named. e.g. "TODO: (ENG-123) - Refactor when v2 is released."' />
</module>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we care about the two rules above. What do you think @sjwarner-bp @Smarticles101 @mirkoperillo? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you are right, I think it is too strict to the example I found online

<!--&lt;!&ndash; Checks that each Java package has a Javadoc file used for commenting.-->
<!--Only allows a package-info.java, not package.html. &ndash;&gt;-->
<!--<property name="severity" value="warning"/>-->
<!--</module>-->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the rule above instead of commenting it out 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<property name="severity" value="error" />
</module>

<module name="RedundantImport">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, this rule and the unused import one should make our lives much easier! I always forget to look for unused imports in PRs 🙂


-->

<module name="AvoidStarImport">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree with this rule? I don't think star imports are necessarily bad? Thoughts @sjwarner-bp @Smarticles101 @mirkoperillo? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you are right

<property name="severity" value="error" />
</module>

<module name="ImportOrder">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I care too much about import order? Thoughts @mirkoperillo @sjwarner-bp @Smarticles101? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<property name="severity" value="error" />
</module>

<module name="RightCurly">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, this rule should be really useful! 😄

</module>

<!-- Checks for braces around if and else blocks -->
<module name="NeedBraces">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be really useful! 😄

<property name="format" value="e\.printStackTrace\(\)" />
<property name="illegalPattern" value="true" />
</module>
</module>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the rules above? I don't think it'll come up, no one should need to add them. But I also don't think there should be rules against it. Thoughts @mirkoperillo @Smarticles101 @sjwarner-bp? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


<!--module name="SuppressionFilter">
<property name="file" value="suppressions.xml"/>
</module-->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the rule above instead of commenting it out 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


<module name="Checker">

<module name="FileTabCharacter">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a rule to check that files are indented correctly with four spaces? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give a look to rule syntax, but I imagine that it's possible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

I have a proposal where to move config/checkstyle folder.
What do you think about move it in exercises folder named as .config to show clearly that folder isn't an exercise ?

@FridaTveit

Copy link
Copy Markdown
Contributor

Since it's just one file I don't think it needs to be in its own folder. I think just putting checkstyle.xml in the exercises folder is fine 🙂 What do you think @sjwarner-bp @Smarticles101 @mirkoperillo? 🙂

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

Yes thinking in a lean mode you are right: it's a single file so there is no need of a folder, thinking in a more structured way a folder can be used for future shared plugins configurations. I think there isn't a best solution, you are the mantainers so you have to choose the more suitable solution for you 😄

@FridaTveit

Copy link
Copy Markdown
Contributor

Good point 🙂 I think just a single file in the exercises folder for now and then we can always create a separate folder for it later if we realise we need more config files 🙂

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really great @mirkoperillo! 😄

I've left one comment about a couple of rules that I think can be removed 🙂 Apart from that I think the only thing left to do is fix the linting errors. Unfortunately that will need to be done before this can be merged so that the build will pass.

If you would prefer it I could look through the errors and open issues for them for anyone to solve so that you don't have to do it all 🙂 That will mean that this PR could be open for a while though until all the issues have been fixed. But that's fine if you would prefer that 🙂 Or you could fix the errors and add those changes to the PR (or do separate PRs for them), it's up to you 🙂

Comment thread exercises/checkstyle.xml Outdated
<!-- . matches any character, so we need to escape it and use \. to match dots. -->
<property name="format" value="System\.err\.println" />
<property name="illegalPattern" value="true" />
</module>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the two rules above can be removed as well? 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@FridaTveit

Copy link
Copy Markdown
Contributor

Also is it possible at the moment for contributors to run just the checkstyle linter locally @mirkoperillo? If so then we should probably document how to do that somewhere 🙂 But I'm happy to open another issue for adding that functionality (if it doesn't exist) and documenting it 🙂

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

@FridaTveit I have some issues with some validations. Actually StaticVariableNameCheck with regExp ^[a-z][a-zA-Z0-9]*$ doesn't accept values as

private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};

Validation wants something like this
private static double[] discountTiers = {0, 5, 10, 20, 25};
So actually checkstyle permits a variable name as DISCOUNT_TIERS only for constants (static final non-private variables), in all other cases something like this breaks the validation.
Some other violations:

LocalFinalVariableName validation

final String COUNTABLE_NUCLEOTIDES = "ACGT";

MemberNameCheck

What I have to do ?

@FridaTveit

FridaTveit commented Oct 25, 2018 •

Copy link
Copy Markdown
Contributor

Good question @mirkoperillo 🙂 Is there any reason all those variables can't be private final static variables? 🙂

@mirkoperillo

mirkoperillo commented Oct 26, 2018 •

Copy link
Copy Markdown
Contributor Author

@FridaTveit Any reasons, I changed them as private static final

Last problem: one test in ParallelLetterFrequencyTest throws me OutOfMemory exception as you can see.

ParallelLetterFrequencyTest > multipleThreadsGetUsed FAILED
    java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:3332)
        at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
        at java.lang.StringBuilder.append(StringBuilder.java:136)
        at ParallelLetterFrequencyTest.multipleThreadsGetUsed(ParallelLetterFrequencyTest.java:150)

I fix it on my machine putting maxHeapSize on build.gradle of the test(actually I haven't pushed it). Do you have the same problem or it's caused only by my local settings ?

test {
  testLogging {
    exceptionFormat = 'full'
    events = ["passed", "failed", "skipped"]
    maxHeapSize = "2048m"
  }
}

@FridaTveit

Copy link
Copy Markdown
Contributor

This is looking amazing, thanks @mirkoperillo! 😄

For the out of memory error, no I haven't seen that before. So I think as long as the travis build doesn't fail we don't have to make any changes for it 🙂

The only thing left to do I think is to rebase your changes on master (or merge master into your branch) as there are a few conflicting changes at the moment. Does that sound okay? 🙂

Thanks again for your amazing effort in making all these changes! 😄

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

@FridaTveit ok, I'll merge master in a couple of days

@mirkoperillo

Copy link
Copy Markdown
Contributor Author

merged the master

@mirkoperillo mirkoperillo changed the title WIP: Java code linting Java code linting Oct 28, 2018

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @mirkoperillo! Thanks again for this amazing effort, it should make a great difference to us as maintainers and everyone wanting to contribute! 😄

@FridaTveit
FridaTveit merged commit 956e92d into exercism:master Oct 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants