6

I'm doing code review on a change my co-worker made to our Java application, and I've found something I'm not very familiar with - a nested class.

From reviewing the code, it seems like the nested class is being used as if it were a normal class - and asking my co-worker about it, the reason for putting it in as a nested class is because of a source control issue preventing her from creating a new class on the day she coded it.

Now this is bothering me - because while there's no reason to introduce this element into our code (very few classes use nested classes in our application), there's also no drawbacks to it that I can think of either. The nested class is, in a very loose way, related to the class it came from, and re-writing the code so that the nested class is an independent one would take some time.

Is there any good reason to have my co-worker redo the code so that this nested class is independent? Or would I just be asking them to waste their time on something that does not matter?

Note that there does not appear to be any functional affect on implementing the class this way - so any argument would have to be from best practice or bad structure, rather than trying to prove that it doesn't work (because it does work - I'm just not sure it's appropriate).

14
  • 4
    Are there any drawbacks to using a nested class instead of declaring a new one? -- Yes; you can't use it anywhere else. In practice, nested classes should be a very rare occurrence; they are an indicator that the containing class is already too complicated. Commented Nov 9, 2018 at 16:40
  • 1
    Voting to close. If this question were focused on the pros and cons of a nested class in general, it would be more objectively answerable. As written, you're effectively asking us to mediate in a code review (generally out of scope here). And worse, you're asking us to do so without any relevant context other than, "My coworker did this because " <a reason that doesn't actually make sense> ... Why aren't you discussing this with your coworker and/or the rest of the team???
    – svidgen
    Commented Nov 9, 2018 at 17:45
  • 1
    @svidgen I don't think I can re-word this as such a question - though I probably should have asked it as such from the start. Really, I was trying to answer two questions at once - what are the practical purposes of Nested Classes, and should an unnecessary nested class fail a code review. I'm not sure if the latter would be appropriate though - but I should have started at least with the former.
    – Zibbobz
    Commented Nov 9, 2018 at 18:14
  • 1
    @svidgen, "If this question were focused on the pros and cons of a nested class in general, it would be more objectively answerable". No. such a question would be 100% off-topic. We do not do pros and cons questions here. Your vote to close is completely misguided.
    – David Arno
    Commented Nov 9, 2018 at 22:16
  • 1
    Nowadays, you can extract a nested class using an IDE refactoring capabilities. So it is not so much work to do. I think anything that's surprising for no good reason should fail a review. And I think nested classes are surprising.
    – Adrien H
    Commented Jul 25, 2019 at 15:04

3 Answers 3

3

If the nested class is private then it is part of the implementation details of that class. There are various valid reasons why such a class might exist: data encapsulation, proving a private implementation of an interface to name two.

If the class is accessible outside of its containing class, then the single responsibility principle likely comes into play. Is that inner class genuinely a responsibility of the outer one? Since you say it’s only loosely related, then the answer is likely, no.

Nesting classes tightly couples them. It makes the outer class more complex as it now contains two class’s worth of functionality. And that outer class now has two responsibilities. And all this exists because of a problem with a check-in.

So I’d definitely recommend restructuring the code in this case too move that inner class out into its own file where it belongs.

3

I'm obligated to post this cartoon about the measure of code quality.

this cartoon about the measure of code quality

If the only reason for creating the nested class instead of a free-standing class was a clerical restriction [source control is an automated clerk], then the nested class should be refactored into a free-standing class. If there are no other reasons, this should be an easy refactoring.

2
  • Yeah. If the answer to "Why is this a nested class?" is "Because source control wouldn't allow me to create a new file" then it definitely garners one or two WTF's. And maybe an AYFKM!? ... and maybe a link to Let Me Google That For You with the search terms "how to use <Source Control X>" Commented Nov 9, 2018 at 17:36
  • 1
    Sarcasm aside, this is a teaching opportunity. Teach the coder to reach out for help in situations like this, so you don't fail a code review for such a silly reason. Commented Nov 9, 2018 at 17:37
2

Given that it was only created due to a problem with source control rarther than any design considerations then yes. You should probably refactor it.

However, if the code is functional and you don't have a "no nested classes" coding style rule then I would not recommend you fail the code review because of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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