filmov
tv
Java Clean Code Tutorial #13 – Hero & God Methods!

Показать описание
Ever needed to change a method or class that is like 10,000 lines long?
Unfortunately the majority of codebases have a few classes that grown to contain methods that are simply hundreds or thousands of lines long. Usually there are no tests or low quality tests to go with it!
This can happen for many reasons:
- Developers may be unaware of good software design (SRP SOLID)
- No code reviews
- Programmers simply don't really care about the product / software
- I'll tidy it up later mentality
- 5 lines more logic wont hurt mentality
- Cheap outsourcing of development by contractors without good software design
- Disgruntled developers
If you need to add some logic into a method that is already 1000 lines long I recommend you:
- Create a new class to contain the new logic
- Write all associated unit tests for the new class's logic
- Add a method call to the new class from the 1000 line long method.
Extracting any new logic into a new class & method and making a reference from the god method will:
- Enable your application to have at least some tests for the new functionality
- Start to introduce good design patterns into the code base
- Chances are if another developer sees this pattern they will follow the same in the future
- If you need to further change that functionality you have a unit test suite already made! This means you can make changes with confidence.
- Overall easier for you to understand the codebase
Till next time!
Cheers!
Philip
Sorry about the wind at the start! I now have a deadcat.... :)
Unfortunately the majority of codebases have a few classes that grown to contain methods that are simply hundreds or thousands of lines long. Usually there are no tests or low quality tests to go with it!
This can happen for many reasons:
- Developers may be unaware of good software design (SRP SOLID)
- No code reviews
- Programmers simply don't really care about the product / software
- I'll tidy it up later mentality
- 5 lines more logic wont hurt mentality
- Cheap outsourcing of development by contractors without good software design
- Disgruntled developers
If you need to add some logic into a method that is already 1000 lines long I recommend you:
- Create a new class to contain the new logic
- Write all associated unit tests for the new class's logic
- Add a method call to the new class from the 1000 line long method.
Extracting any new logic into a new class & method and making a reference from the god method will:
- Enable your application to have at least some tests for the new functionality
- Start to introduce good design patterns into the code base
- Chances are if another developer sees this pattern they will follow the same in the future
- If you need to further change that functionality you have a unit test suite already made! This means you can make changes with confidence.
- Overall easier for you to understand the codebase
Till next time!
Cheers!
Philip
Sorry about the wind at the start! I now have a deadcat.... :)
Комментарии