Raj KB Profile picture
Aug 1, 2022 16 tweets 7 min read Read on X
0⃣Clean Code Series: Functions 📒💻

A guide to producing readable, reusable, and refactorable software in #php (#laravel #symfony #magento) #javascript #python, etc.

(References)
- Robert C. Martin's book "Clean Code"
- github .com/jupeter/clean-code-php

Mega Thread (14) 🧵👇 Image
1⃣ Clean Code Series: Functions📒🧑‍💻

☑️ Use default arguments instead of short-circuiting or conditionals Image
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. Image
3⃣ Clean Code Series: Functions📒🧑‍💻

☑️ Function names should say what they do Image
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. Image
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. Image
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. Image
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. Image
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
... Image
9⃣ Clean Code Series: Functions📒🧑‍💻

☑️ Encapsulate conditionals Image
🔟 Clean Code Series: Functions📒🧑‍💻

☑️ Avoid negative conditionals Image
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. Image
1⃣2⃣ Clean Code Series: Functions📒🧑‍💻

☑️ Avoid type-checking (part 1)

🔹Consider using consistent APIs. Image
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. Image
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. Image
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
 

Keep Current with Raj KB

Raj KB Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @rajkbnp

Feb 1, 2022
0⃣Clean Code Series: Comparision 📒💻

A guide to producing readable, reusable, and refactorable software in #php (#laravel #symfony) #javascript #python, etc.

- Software engineering principles, from Robert C. Martin's book "Clean Code"

🔽 github.com/jupeter/clean-…

Thread 🧵👇 Image
1⃣Clean Code Series: Comparision 📒💻

☑️ Use identical comparison Image
2⃣Clean Code Series: Comparision 📒💻

☑️ Null coalescing operator Image
Read 5 tweets
Jan 30, 2022
0⃣ Clean Code Series: Variables📒🧑‍💻

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"

(code: github.com/jupeter/clean-…)

Thread (9)🧵👇 Image
1⃣ Clean Code Series: Variables📒 🧑‍💻

☑️ Use meaningful and pronounceable variable names Image
2⃣ Clean Code Series: Variables📒 🧑‍💻

☑️ Use the same vocabulary for the same type of variable Image
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(