(References)
- Robert C. Martin's book "Clean Code"
- github .com/jupeter/clean-code-php
Mega Thread (14) 🧵👇
1⃣ Clean Code Series: Functions📒🧑💻
☑️ Use default arguments instead of short-circuiting or conditionals
2⃣ Clean Code Series: Functions📒🧑💻
☑️ Function arguments (2 or fewer ideally)
🔹Zero arguments are the ideal case. One or two arguments are ok, and three should be avoided. Anything more than that should be consolidated.
3⃣ Clean Code Series: Functions📒🧑💻
☑️ Function names should say what they do
4⃣ Clean Code Series: Functions📒🧑💻
☑️ Functions should only be one level of abstraction
🔹When you have more than one level of abstraction your function is usually doing too much.
🔹Splitting up functions leads to reusability and easier testing.
5⃣ Clean Code Series: Functions📒🧑💻
☑️ Don't use flags as function parameters
🔹Flags tell your user that this function does more than one thing.
🔹Functions should do one thing.
🔹Split out your functions if they are following different code paths based on a boolean.
6⃣ Clean Code Series: Functions📒🧑💻
☑️ Avoid Side Effects
🔹A function produces a side effect if it does anything other than take a value in and return another value or values. Try to avoid it.
7⃣ Clean Code Series: Functions📒🧑💻
☑️ Don't write to global functions
🔹Polluting globals is a bad practice in many languages because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production.
8⃣ Clean Code Series: Functions📒🧑💻
☑️ Don't use a Singleton pattern
🔹Singleton is an anti-pattern
🔹They violate the single responsibility principle
🔹They inherently cause code to be tightly coupled
...
9⃣ Clean Code Series: Functions📒🧑💻
☑️ Encapsulate conditionals
🔟 Clean Code Series: Functions📒🧑💻
☑️ Avoid negative conditionals
1⃣1⃣ Clean Code Series: Functions📒🧑💻
☑️ Avoid conditionals
🔹You can use polymorphism to achieve the same task in many cases.
🔹Also, a function should only do one thing.
1⃣2⃣ Clean Code Series: Functions📒🧑💻
☑️ Avoid type-checking (part 1)
🔹Consider using consistent APIs.
1⃣3⃣ Clean Code Series: Functions📒🧑💻
☑️ Avoid type-checking (part 2)
🔹If you are working with basic primitive values like strings, integers, and arrays, and you use PHP 7+ and you can't use polymorphism try considering type declaration or strict mode.
1⃣4⃣ Clean Code Series: Functions📒🧑💻
☑️ Remove dead code
🔹Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase.
🔹If it's not being called, get rid of it! It will still be safe in your version history if you still need it.
That's all for the 3rd series of clean-code.
In the next Thread, I will add clean code practices against Classes & SOLID principles.
If you like this thread,
✅ Follow me @rajkbnp
💕 Like it
🔀 Retweet it
Thanks 🙏
• • •
Missing some Tweet in this thread? You can try to
force a refresh
It's a guide to producing readable, reusable, and refactorable software in #php#ruby#javascript#python#java, etc. - Software engineering principles, from Robert C. Martin's book "Clean Code"