Same question I browsed over the internet but not getting the one point answer.

I'am working on Automation Selenium using Java. I have created one class lets say ProductDetailsPage.java so I have created all PDP related test in this particular class but the code length of this file is around 1800 lines.

I'm not sure weather its comes under recommended code limit. If no then How can i reduce the length as i have to write these many lines to automate the all PDP testcase ?

  • If ProductDetailsPage.java is following the POM and has business logic to be tested, I think you should create a separate test class for this class under test. Aug 2 '18 at 9:51

1800 lines of code is definitely not desirable. You need to refactor your class to separate the responsibilities. On a general rule, check the following

  • Possibility of separating utility methods to a different class.
  • Responsibilities that can be given to some other class.
  • Functionality within the same class which can be broken down.
  • Check if you can use Abstract classes which can be inherited to get the implemented behavior and so on....

In my view a class with 250-350 LoC is a pretty good class. I understand not everything can be broken down to the finest particle. You should also consider the cost of refactoring and maintenance while doing this; this alone should not come as an overhead. Just apply simple SOLID principle.

  • "1800 lines of code is definitely not desirable" why not? How often you read java class from the start to the end. It is not a book :)
    – Alexey R.
    Aug 2 '18 at 8:06
  • There is no definite rule imposed on the number of lines of code in a class. If 1800 lines are readable, maintainable and extendable, there no harm in a class with so many LoC. Aug 2 '18 at 8:46

There is no recommended code limit. Usually when you navigate through the code you navigate through logic, not through the lines. Number of lines can also be dependent on the code style (like whether you put { on the same line of a method signature or on the next line, etc.). Modern IDE navigation capabilities let you not be care of how much code there is in the file.

You should just keep your code in line with the common sense and known approaches to keep your code effective like code reuse, etc.

So if you have your classes defined withing effective structure (inheritance and so on) and do not have code duplicating and still have 2k lines, it is okay.

You can just take any well known framework or even JDK and find there classes with ~1000 or ~2000 lines of code.


Quick Answer

There is no limit to the number of lines of code in a class. For example java.io.File class is about ~2500 lines of code. And it is one of the well written classes.

That said,

Following simple guidelines can help you determine whether your class need some refactoring.

  1. Does the class do one thing and does it properly. If your class contain all the tests related to the ProductDetailsPage - that should be OK.
  2. For test classes, I do not really worry about OCP - open/close principle.
  3. IMO, someone reading an automated test case should be able to run the test manually. Too much of extracting methods might not be a good idea for achieving this goal.
  4. Tempering (3) a bit - if you have same blocks of code recurring too many times - you may want to extract them to a helper method. I do not suggest creating a Helper class for these methods unless these methods are also used from other test cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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