This is a bit of a big one to answer to be honest, but I'll try anyway, with an explanation of the worst project I've came across in my 20 years of contracting.
A client I worked with a few years back had a project they'd started back in early 2000s, It had had contractors and full timers working on it, and one of the php files (it wasn't even a class) had 7000 lines, and was a mix of css, js, sql queries and html markup.
No-one wanted to do any modifications to the file as it was a whopper and as a result, a lot of code got duplicated, and with every new feature, the soup got bigger.
Why was it a problem?
- no-one knew what was going on with it. things broke.
- the file was too large & logic was mixed with markup.
- formatting was inconsistent & there was no coding style.
so to fix that
- write some tests. make sure every bit of functionality has a test to cover its functionality. TBH make sure you write new tests before you write functionality. TDD. We're doing MVC in Laravel so that's should be ok. Being able to understand how to test code makes its easier to write code.
- make it smaller. Split up your code and views. Make smaller classes with related functionality. Use DI
- be consistent. follow coding standards. PSR-2
They didn't want to fix it, so I left and went to another company (where I've been for 2 years).
Theres a huge amount of resources on Laracasts that will help here. Below are a few that have stuck with me:
- https://laracasts.com/series/solid-principles-in-php
- https://laracasts.com/series/whip-monstrous-code-into-shape
- https://laracasts.com/series/ten-techniques-for-cleaner-code
- PSR-12: Extended Coding Style https://www.php-fig.org/psr/psr-12/
I also discovered 'Object Calisthenics'
"Object Calisthenics are programming exercises, formalized as a set of 9 rules invented by Jeff Bay in his book The ThoughtWorks Anthology. The word Object is related to Object Oriented Programming. The word Calisthenics is derived from greek, and means exercises under the context of gymnastics. By trying to follow these rules as much as possible, you will naturally change how you write code. It doesn’t mean you have to follow all these rules, all the time. Find your balance with these rules, use some of them only if you feel comfortable with them.
These rules focus on maintainability, readability, testability, and comprehensibility of your code. If you already write code that is maintainable, readable, testable, and comprehensible, then these rules will help you write code that is more maintainable, more readable, more testable, and more comprehensible."
- Only One Level Of Indentation Per Method
- Don’t Use The ELSE Keyword
- Wrap All Primitives And Strings
- First Class Collections
- One Dot Per Line
- Don’t Abbreviate
- Keep All Entities Small
- No Classes With More Than Two Instance Variables
- No Getters/Setters/Properties
see
In essence:
Make sure you understand what your code does, and its covered with tests before you make any changes. Also be sure to only work on a branch of your codebase to do your refactor so that if for any reason your new code isnt production ready, you don't break your main site.
Good luck!