- ভূমিকা
- ভেরিয়েবল
- অর্থপূর্ণ এবং উচ্চারণযোগ্য ভেরিয়েবল নাম ব্যবহার করুন
- একই ধরনের ভেরিয়েবলের জন্য একই শব্দভাণ্ডার ব্যবহার করুন
- অনুসন্ধানযোগ্য নাম ব্যবহার করুন (পর্ব ১)
- অনুসন্ধানযোগ্য নাম ব্যবহার করুন (পর্ব ২)
- ব্যাখ্যামূলক ভেরিয়েবল ব্যবহার করুন
- নেস্টেড স্ট্রাকচার এড়িয়ে চলুন (পর্ব ১)
- নেস্টেড স্ট্রাকচার এড়িয়ে চলুন (পর্ব ২)
- মেন্টাল ম্যাপিং এড়িয়ে চলুন
- অপ্রয়োজনীয় প্রসঙ্গ যোগ করবেন না
- তুলনা
- ফাংশন
- সংক্ষিপ্ত বা শর্তাধীন সংযোগের পরিবর্তে ডিফল্ট আর্গুমেন্ট ব্যবহার করুন
- ফাংশন আর্গুমেন্ট (2 টা আর্গুমেন্ট বা কম)
- ফাংশন কি কাজ করে সেই হিসেবে নামকরণ করতে হবে
- ফাংশন-শুধুমাত্র-Abstraction-এর-একটি-স্তর-হওয়া-উচিত
- ফাংশন প্যারামিটার হিসাবে flag ব্যবহার করবেন না
- পার্শ্বপ্রতিক্রিয়া এড়িয়ে চলুন
- Global functions লেখা থেকে বিরত থাকুন
- Singleton pattern ব্যাবহার করবেন না
- Encapsulate conditionals
- নেগেটিভ কন্ডিশনিং এড়িয়ে চলুন
- কন্ডিশনিং এড়িয়ে চলুন
- ফাংশন এর মধ্যে টাইপ চেক থেকে বিরত থাকুন (পর্ব ১)
- ফাংশন এর মধ্যে টাইপ চেক থেকে বিরত থাকুন (পর্ব ২)
- ডেড কোড মুছে ফেলুন
- অবজেক্টস এবং ডেটা স্ট্রাকচার
- ক্লাস
- SOLID
- পুনরাবৃত্তি করবেন না (DRY)
- অনুবাদ
Software engineering principles, from Robert C. Martin's book Clean Code, adapted for PHP. এটি একটি স্টাইল গাইড নয়। এটি পিএইচপিতে পাঠযোগ্য, পুন: ব্যবহারযোগ্য এবং রিফ্যাক্টরেবল সফটওয়্যার তৈরির জন্য একটি নির্দেশিকা।
Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.
Inspired from clean-code-javascript.
Although many developers still use PHP 5, most of the examples in this article only work with PHP 7.1+.
খারাপ:
$ymdstr = $moment->format('y-m-d');ভাল:
$currentDate = $moment->format('y-m-d');খারাপ:
getUserInfo(); getUserData(); getUserRecord(); getUserProfile();ভাল:
getUser();সাধারণত আমরা কোড লেখার থেকে বেশি পড়ে থাকি। তাই এটা গুরুত্বপূর্ণ যে আমরা যে কোড লিখব সেটা যেন রিডেবল এবং সার্চেবল হয় অর্থাৎ পড়তে এবং খুঁজে পেতে যেন সহজ হয়। আমাদের প্রোগ্রাম বোঝার জন্য অর্থবহ হওয়া ভেরিয়েবলের নাম না দিলে যারা কোড পড়বে তাঁদের বুঝতে অসুবিধা হবে। সেজন্য ভেরিয়েবল এর নাম রিডেবল এবং সার্চেবল হওয়া জরুরি।
খারাপ:
// What the heck is 448 for?$result = $serializer->serialize($data, 448);ভাল:
$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);খারাপ:
class User{// What the heck is 7 for?public$access = 7} // What the heck is 4 for?if ($user->access & 4){// ... } // What's going on here?$user->access ^= 2;ভাল:
class User{publicconstACCESS_READ = 1; publicconstACCESS_CREATE = 2; publicconstACCESS_UPDATE = 4; publicconstACCESS_DELETE = 8; // User as default can read, create and update somethingpublic$access = self::ACCESS_READ | self::ACCESS_CREATE | self::ACCESS_UPDATE} if ($user->access & User::ACCESS_UPDATE){// do edit ... } // Deny access rights to create something$user->access ^= User::ACCESS_CREATE;খারাপ:
$address = 'One Infinite Loop, Cupertino 95014'; $cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/'; preg_match($cityZipCodeRegex, $address, $matches); saveCityZipCode($matches[1], $matches[2]);খারাপ নয়:
এটি আরও ভাল, তবে আমরা এখনও রেজেক্সের উপর নির্ভরশীল হয়ে আছি।
$address = 'One Infinite Loop, Cupertino 95014'; $cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/'; preg_match($cityZipCodeRegex, $address, $matches); [, $city, $zipCode] = $matches; saveCityZipCode($city, $zipCode);ভাল:
Naming subpatterns ব্যাবহার করে রেজেক্স এর উপর নির্ভরতা কমিয়ে আনতে পারি।
$address = 'One Infinite Loop, Cupertino 95014'; $cityZipCodeRegex = '/^[^,]+,\s*(?<city>.+?)\s*(?<zipCode>\d{5})$/'; preg_match($cityZipCodeRegex, $address, $matches); saveCityZipCode($matches['city'], $matches['zipCode']);অনেক বেশি if-else statements আপনার কোড পড়া বা অনুসরণ করাকে কঠিন করে তুলতে পারে কাড়ন এটা বুঝতে অনেক ঝামেলা হবে। এক্ষেত্রে বিশদভাবে লেখাই ভাল�(Explicit is better than implicit.)।
খারাপ:
functionisShopOpen($day): bool{if ($day){if (is_string($day)){$day = strtolower($day); if ($day === 'friday'){returntrue} elseif ($day === 'saturday'){returntrue} elseif ($day === 'sunday'){returntrue} returnfalse} returnfalse} returnfalse}ভাল:
functionisShopOpen(string$day): bool{if (empty($day)){returnfalse} $openingDays = ['friday', 'saturday', 'sunday']; returnin_array(strtolower($day), $openingDays, true)}খারাপ:
functionfibonacci(int$n){if ($n < 50){if ($n !== 0){if ($n !== 1){returnfibonacci($n - 1) + fibonacci($n - 2)} return1} return0} return'Not supported'}ভাল:
functionfibonacci(int$n): int{if ($n === 0 || $n === 1){return$n} if ($n >= 50){thrownewException('Not supported')} returnfibonacci($n - 1) + fibonacci($n - 2)}ভেরিয়েবল নাম এমনভাবে লিখুন যাতে যে কোড পড়ছে তার যেন অনুবাদ করে দেখতে না হয়। এমন নাম হওয়া উচিত যেন যেকেউ দেখে বুঝে নিতে পারে। বিশদভাবে লেখাই ভাল�(Explicit is better than implicit.)।
খারাপ:
$l = ['Austin', 'New York', 'San Francisco']; for ($i = 0; $i < count($l); $i++){$li = $l[$i]; doStuff(); doSomeOtherStuff(); // ...// ...// ...// Wait, what is `$li` for again?dispatch($li)}ভাল:
$locations = ['Austin', 'New York', 'San Francisco']; foreach ($locationsas$location){doStuff(); doSomeOtherStuff(); // ...// ...// ...dispatch($location)}যদি আপনার class/object এর নাম দিয়ে কিছু বুঝা যায় তাহলে ওই একই নাম এই class/object এর ভেরিয়েবল এ ব্যবহার করার দরকার নেই।
খারাপ:
class Car{public$carMake; public$carModel; public$carColor; //... }ভাল:
class Car{public$make; public$model; public$color; //... }ভাল না:
এখানে এই সিম্পল কম্প্যেয়ার string কে integer এ রূপান্তর করে ফেলেছে।
$a = '42'; $b = 42; if ($a != $b){// The expression will always pass }এখানে $a != $b, FALSE রিটার্ন করে কিন্তু এটা TRUE হবে! এখানে string 42 integer 42 এর থেকে আলাদা ধরনের/type।
ভাল:
এই identical comparison type এবং value তুলনা/কম্পেয়ার করে.
$a = '42'; $b = 42; if ($a !== $b){// The expression is verified }এখানে $a !== $b, TRUE রিটার্ন করে।
Null coalescing হল নতুন অপেরেটর PHP 7 এ যুক্ত হয়েছে. The null coalescing operator ?? has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). এটি এর প্রথম operand রিটার্ন করে যদি প্রথম operand টি exists করে এবং এটি NULL না হয়, অন্যথায় এটি দ্বিতীয় operand রিটার্ন করে।
খারাপ:
if (isset($_GET['name'])){$name = $_GET['name']} elseif (isset($_POST['name'])){$name = $_POST['name']} else{$name = 'nobody'}ভাল:
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';ভাল না:
এটা ভাল নয় কারণ $breweryName , NULL হতে পারে।
functioncreateMicrobrewery($breweryName = 'Hipster Brew Co.'): void{// ... }খারাপ নয়:
এই উদাহরণ টা বুঝতে আগের উদাহরণ থেকে সুবিধা হবে, কিন্তু এটি ভেরিয়েবলের মানকে আরও ভালভাবে নিয়ন্ত্রণ করে।
functioncreateMicrobrewery($name = null): void{$breweryName = $name ?: 'Hipster Brew Co.'; // ... }ভাল:
আপনি type hinting ব্যাবহার করে এটা নিশিত করতে পারেন যে $breweryName কখন ও NULL হবে না।
functioncreateMicrobrewery(string$breweryName = 'Hipster Brew Co.'): void{// ... }ফাংশন এর প্যারামিটার কম করা গুরুত্বপূর্ণ কারণ এটা ফাংশন টেস্টিং সহজ করে তোলে। ৩ তার বেশি প্যারামিটার টেস্টিং কে অনেক বেশি কঠিন করে ফেলে কারণ তখন আপনার প্রতিটা আর্গুমেন্ট এর জনন অনেকগুলো করে টেস্ট কেস চিন্তা করতে হবে এবং সেগুলো হ্যান্ডেল করতে হবে।
জিরো/০ টা আর্গুমেন্ট হল আদর্শ কেস। ১ টা বা ২ টা আর্গুমেন্ট ও থিক আছে কিন্তু ২ এর অধীক হলে অবশ্যয় এভয়েড করতে হবে। সাধারণত, আপনার ফাংশন এ ২ তার বেশি আর্গুমেন্ট থাকা মানে আপনার ফাংশন টা অনেক বেশি কাজ করতেছে যেটা উচিত নয়।
খারাপ:
class Questionnaire{publicfunction__construct( string$firstname, string$lastname, string$patronymic, string$region, string$district, string$city, string$phone, string$email ){// ... } }ভাল:
class Name{private$firstname; private$lastname; private$patronymic; publicfunction__construct(string$firstname, string$lastname, string$patronymic){$this->firstname = $firstname; $this->lastname = $lastname; $this->patronymic = $patronymic} // getters ... } class City{private$region; private$district; private$city; publicfunction__construct(string$region, string$district, string$city){$this->region = $region; $this->district = $district; $this->city = $city} // getters ... } class Contact{private$phone; private$email; publicfunction__construct(string$phone, string$email){$this->phone = $phone; $this->email = $email} // getters ... } class Questionnaire{publicfunction__construct(Name$name, City$city, Contact$contact){// ... } }খারাপ:
class Email{//...publicfunctionhandle(): void{mail($this->to, $this->subject, $this->body)} } $message = newEmail(...); // What is this? A handle for the message? Are we writing to a file now?$message->handle();ভাল:
class Email{//...publicfunctionsend(): void{mail($this->to, $this->subject, $this->body)} } $message = newEmail(...); // Clear and obvious$message->send();যখন আপনার এক লেভেল এর বেশি abstraction থাকে তার মানে হল আপনার ফাংশন টা অনেক বেশি কাজ করতেছে। ফাংশন ভাগ করে লিখলে সেটা পুনরায় ব্যাবহার এবং টেস্ট করতে খুবই সুবিধা হয়।
খারাপ:
functionparseBetterPHPAlternative(string$code): void{$regexes = [ // ... ]; $statements = explode('', $code); $tokens = []; foreach ($regexesas$regex){foreach ($statementsas$statement){// ... } } $ast = []; foreach ($tokensas$token){// lex... } foreach ($astas$node){// parse... } }এটাও খারাপ:
আমরা কিছু ফাংশন এর কাজ সম্পন্ন করেছি কিন্তু parseBetterPHPAlternative() ফাংশন এখনো অনেক জটিল/কমপ্লেক্স এবং টেস্ট করার মত অবস্থাই নেই।
functiontokenize(string$code): array{$regexes = [ // ... ]; $statements = explode('', $code); $tokens = []; foreach ($regexesas$regex){foreach ($statementsas$statement){$tokens[] = /* ... */} } return$tokens} functionlexer(array$tokens): array{$ast = []; foreach ($tokensas$token){$ast[] = /* ... */} return$ast} functionparseBetterPHPAlternative(string$code): void{$tokens = tokenize($code); $ast = lexer($tokens); foreach ($astas$node){// parse... } }ভাল:
সব থেকে ভাল সমাধান হল - parseBetterPHPAlternative() থেকে dependency গুল আলাদা করে ফেলা।
class Tokenizer{publicfunctiontokenize(string$code): array{$regexes = [ // ... ]; $statements = explode('', $code); $tokens = []; foreach ($regexesas$regex){foreach ($statementsas$statement){$tokens[] = /* ... */} } return$tokens} } class Lexer{publicfunctionlexify(array$tokens): array{$ast = []; foreach ($tokensas$token){$ast[] = /* ... */} return$ast} } class BetterPHPAlternative{private$tokenizer; private$lexer; publicfunction__construct(Tokenizer$tokenizer, Lexer$lexer){$this->tokenizer = $tokenizer; $this->lexer = $lexer} publicfunctionparse(string$code): void{$tokens = $this->tokenizer->tokenize($code); $ast = $this->lexer->lexify($tokens); foreach ($astas$node){// parse... } } }Flags আপনার ইউজার কে বুঝায় যে এই ফাংশন টা একের অধিক কাজ করতেছে, কিন্তু একটা ফাংশন একটার বেশি কাজ করা উচিত নয়। ফাংশন এর কাজ গুল আলাদা করা উচিত যদি boolean ভ্যালু এর উপর বেস করে আলাদা কাজ করে। খারাপ:
functioncreateFile(string$name, bool$temp = false): void{if ($temp){touch('./temp/' . $name)} else{touch($name)} }ভাল:
functioncreateFile(string$name): void{touch($name)} functioncreateTempFile(string$name): void{touch('./temp/' . $name)}A function produces a side effect if it does anything other than take a value in and return another value or values. A side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger.
Now, you do need to have side effects in a program on occasion. Like the previous example, you might need to write to a file. What you want to do is to centralize where you are doing this. Don't have several functions and classes that write to a particular file. Have one service that does it. One and only one.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
খারাপ:
// Global variable referenced by following function.// If we had another function that used this name, now it'd be an array and it could break it.$name = 'Ryan McDermott'; functionsplitIntoFirstAndLastName(): void{global$name; $name = explode('', $name)} splitIntoFirstAndLastName(); var_dump($name); // ['Ryan', 'McDermott'];ভাল:
functionsplitIntoFirstAndLastName(string$name): array{returnexplode('', $name)} $name = 'Ryan McDermott'; $newName = splitIntoFirstAndLastName($name); var_dump($name); // 'Ryan McDermott';var_dump($newName); // ['Ryan', 'McDermott'];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. Let's think about an example: what if you wanted to have configuration array? You could write global function like config(), but it could clash with another library that tried to do the same thing.
খারাপ:
functionconfig(): array{return [ 'foo' => 'bar', ]}ভাল:
class Configuration{private$configuration = []; publicfunction__construct(array$configuration){$this->configuration = $configuration} publicfunctionget(string$key): ?string{// null coalescing operatorreturn$this->configuration[$key] ?? null} }Load configuration and create instance of Configuration class
$configuration = newConfiguration([ 'foo' => 'bar', ]);And now you must use instance of Configuration in your application.
Singleton is an anti-pattern. Paraphrased from Brian Button:
- They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a code smell.
- They violate the single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
- They inherently cause code to be tightly coupled. This makes faking them out under test rather difficult in many cases.
- They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no for unit tests. Why? Because each unit test should be independent from the other.
There is also very good thoughts by Misko Hevery about the root of problem.
খারাপ:
class DBConnection{privatestatic$instance; privatefunction__construct(string$dsn){// ... } publicstaticfunctiongetInstance(): self{if (self::$instance === null){self::$instance = newself()} returnself::$instance} // ... } $singleton = DBConnection::getInstance();ভাল:
class DBConnection{publicfunction__construct(string$dsn){// ... } // ... }Create instance of DBConnection class and configure it with DSN.
$connection = newDBConnection($dsn);And now you must use instance of DBConnection in your application.
খারাপ:
if ($article->state === 'published'){// ... }ভাল:
if ($article->isPublished()){// ... }খারাপ:
functionisDOMNodeNotPresent(DOMNode$node): bool{// ... } if (! isDOMNodeNotPresent($node)){// ... }ভাল:
functionisDOMNodePresent(DOMNode$node): bool{// ... } if (isDOMNodePresent($node)){// ... }This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an if statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second question is usually, "well that's great but why would I want to do that?" The answer is a previous clean code concept we learned: a function should only do one thing. When you have classes and functions that have if statements, you are telling your user that your function does more than one thing. Remember, just do one thing.
খারাপ:
class Airplane{// ...publicfunctiongetCruisingAltitude(): int{switch ($this->type){case'777': return$this->getMaxAltitude() - $this->getPassengerCount(); case'Air Force One': return$this->getMaxAltitude(); case'Cessna': return$this->getMaxAltitude() - $this->getFuelExpenditure()} } }ভাল:
interface Airplane{// ...publicfunctiongetCruisingAltitude(): int} class Boeing777 implements Airplane{// ...publicfunctiongetCruisingAltitude(): int{return$this->getMaxAltitude() - $this->getPassengerCount()} } class AirForceOne implements Airplane{// ...publicfunctiongetCruisingAltitude(): int{return$this->getMaxAltitude()} } class Cessna implements Airplane{// ...publicfunctiongetCruisingAltitude(): int{return$this->getMaxAltitude() - $this->getFuelExpenditure()} }PHP is untyped, which means your functions can take any type of argument. Sometimes you are bitten by this freedom and it becomes tempting to do type-checking in your functions. There are many ways to avoid having to do this. The first thing to consider is consistent APIs.
খারাপ:
functiontravelToTexas($vehicle): void{if ($vehicleinstanceof Bicycle){$vehicle->pedalTo(newLocation('texas'))} elseif ($vehicleinstanceof Car){$vehicle->driveTo(newLocation('texas'))} }ভাল:
functiontravelToTexas(Vehicle$vehicle): void{$vehicle->travelTo(newLocation('texas'))}If you are working with basic primitive values like strings, integers, and arrays, and you use PHP 7+ and you can't use polymorphism but you still feel the need to type-check, you should consider type declaration or strict mode. It provides you with static typing on top of standard PHP syntax. The problem with manually type-checking is that doing it will require so much extra verbiage that the faux "type-safety" you get doesn't make up for the lost readability. Keep your PHP clean, write good tests, and have good code reviews. Otherwise, do all of that but with PHP strict type declaration or strict mode.
খারাপ:
functioncombine($val1, $val2): int{if (! is_numeric($val1) || ! is_numeric($val2)){thrownewException('Must be of type Number')} return$val1 + $val2}ভাল:
functioncombine(int$val1, int$val2): int{return$val1 + $val2}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.
খারাপ:
functionoldRequestModule(string$url): void{// ... } functionnewRequestModule(string$url): void{// ... } $request = newRequestModule($requestUrl); inventoryTracker('apples', $request, 'www.inventory-awesome.io');ভাল:
functionrequestModule(string$url): void{// ... } $request = requestModule($requestUrl); inventoryTracker('apples', $request, 'www.inventory-awesome.io');In PHP you can set public, protected and private keywords for methods. Using it, you can control properties modification on an object.
- When you want to do more beyond getting an object property, you don't have to look up and change every accessor in your codebase.
- Makes adding validation simple when doing a
set. - Encapsulates the internal representation.
- Easy to add logging and error handling when getting and setting.
- Inheriting this class, you can override default functionality.
- You can lazy load your object's properties, let's say getting it from a server.
Additionally, this is part of Open/Closed principle.
খারাপ:
class BankAccount{public$balance = 1000} $bankAccount = newBankAccount(); // Buy shoes...$bankAccount->balance -= 100;ভাল:
class BankAccount{private$balance; publicfunction__construct(int$balance = 1000){$this->balance = $balance} publicfunctionwithdraw(int$amount): void{if ($amount > $this->balance){thrownew \Exception('Amount greater than available balance.')} $this->balance -= $amount} publicfunctiondeposit(int$amount): void{$this->balance += $amount} publicfunctiongetBalance(): int{return$this->balance} } $bankAccount = newBankAccount(); // Buy shoes...$bankAccount->withdraw($shoesPrice); // Get balance$balance = $bankAccount->getBalance();publicmethods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. Modifications in class are dangerous for all users of class.protectedmodifier are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. Modifications in class are dangerous for all descendant classes.privatemodifier guarantees that code is dangerous to modify only in boundaries of single class (you are safe for modifications and you won't have Jenga effect).
Therefore, use private by default and public/protected when you need to provide access for external classes.
For more information you can read the blog post on this topic written by Fabien Potencier.
খারাপ:
class Employee{public$name; publicfunction__construct(string$name){$this->name = $name} } $employee = newEmployee('John Doe'); // Employee name: John Doeecho'Employee name: ' . $employee->name;ভাল:
class Employee{private$name; publicfunction__construct(string$name){$this->name = $name} publicfunctiongetName(): string{return$this->name} } $employee = newEmployee('John Doe'); // Employee name: John Doeecho'Employee name: ' . $employee->getName();As stated famously in Design Patterns by the Gang of Four, you should prefer composition over inheritance where you can. There are lots of good reasons to use inheritance and lots of good reasons to use composition. The main point for this maxim is that if your mind instinctively goes for inheritance, try to think if composition could model your problem better. In some cases it can.
You might be wondering then, "when should I use inheritance?" It depends on your problem at hand, but this is a decent list of when inheritance makes more sense than composition:
- Your inheritance represents an "is-a" relationship and not a "has-a" relationship (Human->Animal vs. User->UserDetails).
- You can reuse code from the base classes (Humans can move like all animals).
- You want to make global changes to derived classes by changing a base class. (Change the caloric expenditure of all animals when they move).
খারাপ:
class Employee{private$name; private$email; publicfunction__construct(string$name, string$email){$this->name = $name; $this->email = $email} // ... } // Bad because Employees "have" tax data.// EmployeeTaxData is not a type of Employeeclass EmployeeTaxData extends Employee{private$ssn; private$salary; publicfunction__construct(string$name, string$email, string$ssn, string$salary){parent::__construct($name, $email); $this->ssn = $ssn; $this->salary = $salary} // ... }ভাল:
class EmployeeTaxData{private$ssn; private$salary; publicfunction__construct(string$ssn, string$salary){$this->ssn = $ssn; $this->salary = $salary} // ... } class Employee{private$name; private$email; private$taxData; publicfunction__construct(string$name, string$email){$this->name = $name; $this->email = $email} publicfunctionsetTaxData(EmployeeTaxData$taxData): void{$this->taxData = $taxData} // ... }A Fluent interface is an object oriented API that aims to improve the readability of the source code by using Method chaining.
While there can be some contexts, frequently builder objects, where this pattern reduces the verbosity of the code (for example the PHPUnit Mock Builder or the Doctrine Query Builder), more often it comes at some costs:
- Breaks Encapsulation.
- Breaks Decorators.
- Is harder to mock in a test suite.
- Makes diffs of commits harder to read.
For more information you can read the full blog post on this topic written by Marco Pivetta.
খারাপ:
class Car{private$make = 'Honda'; private$model = 'Accord'; private$color = 'white'; publicfunctionsetMake(string$make): self{$this->make = $make; // NOTE: Returning this for chainingreturn$this} publicfunctionsetModel(string$model): self{$this->model = $model; // NOTE: Returning this for chainingreturn$this} publicfunctionsetColor(string$color): self{$this->color = $color; // NOTE: Returning this for chainingreturn$this} publicfunctiondump(): void{var_dump($this->make, $this->model, $this->color)} } $car = (newCar()) ->setColor('pink') ->setMake('Ford') ->setModel('F-150') ->dump();ভাল:
class Car{private$make = 'Honda'; private$model = 'Accord'; private$color = 'white'; publicfunctionsetMake(string$make): void{$this->make = $make} publicfunctionsetModel(string$model): void{$this->model = $model} publicfunctionsetColor(string$color): void{$this->color = $color} publicfunctiondump(): void{var_dump($this->make, $this->model, $this->color)} } $car = newCar(); $car->setColor('pink'); $car->setMake('Ford'); $car->setModel('F-150'); $car->dump();The final keyword should be used whenever possible:
- It prevents an uncontrolled inheritance chain.
- It encourages composition.
- It encourages the Single Responsibility Pattern.
- It encourages developers to use your public methods instead of extending the class to get access to protected ones.
- It allows you to change your code without breaking applications that use your class.
The only condition is that your class should implement an interface and no other public methods are defined.
For more informations you can read the blog post on this topic written by Marco Pivetta (Ocramius).
খারাপ:
finalclass Car{private$color; publicfunction__construct($color){$this->color = $color} /** * @return string The color of the vehicle */publicfunctiongetColor(){return$this->color} }ভাল:
interface Vehicle{/** * @return string The color of the vehicle */publicfunctiongetColor()} finalclass Car implements Vehicle{private$color; publicfunction__construct($color){$this->color = $color} publicfunctiongetColor(){return$this->color} }SOLID is the mnemonic acronym introduced by Michael Feathers for the first five principles named by Robert Martin, which meant five basic principles of object-oriented programming and design.
- S: Single Responsibility Principle (SRP)
- O: Open/Closed Principle (OCP)
- L: Liskov Substitution Principle (LSP)
- I: Interface Segregation Principle (ISP)
- D: Dependency Inversion Principle (DIP)
As stated in Clean Code, "There should never be more than one reason for a class to change". It's tempting to jam-pack a class with a lot of functionality, like when you can only take one suitcase on your flight. The issue with this is that your class won't be conceptually cohesive and it will give it many reasons to change. Minimizing the amount of times you need to change a class is important. It's important because if too much functionality is in one class and you modify a piece of it, it can be difficult to understand how that will affect other dependent modules in your codebase.
খারাপ:
class UserSettings{private$user; publicfunction__construct(User$user){$this->user = $user} publicfunctionchangeSettings(array$settings): void{if ($this->verifyCredentials()){// ... } } privatefunctionverifyCredentials(): bool{// ... } }ভাল:
class UserAuth{private$user; publicfunction__construct(User$user){$this->user = $user} publicfunctionverifyCredentials(): bool{// ... } } class UserSettings{private$user; private$auth; publicfunction__construct(User$user){$this->user = $user; $this->auth = newUserAuth($user)} publicfunctionchangeSettings(array$settings): void{if ($this->auth->verifyCredentials()){// ... } } }As stated by Bertrand Meyer, "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." What does that mean though? This principle basically states that you should allow users to add new functionalities without changing existing code.
খারাপ:
abstractclass Adapter{protected$name; publicfunctiongetName(): string{return$this->name} } class AjaxAdapter extends Adapter{publicfunction__construct(){parent::__construct(); $this->name = 'ajaxAdapter'} } class NodeAdapter extends Adapter{publicfunction__construct(){parent::__construct(); $this->name = 'nodeAdapter'} } class HttpRequester{private$adapter; publicfunction__construct(Adapter$adapter){$this->adapter = $adapter} publicfunctionfetch(string$url): Promise{$adapterName = $this->adapter->getName(); if ($adapterName === 'ajaxAdapter'){return$this->makeAjaxCall($url)} elseif ($adapterName === 'httpNodeAdapter'){return$this->makeHttpCall($url)} } privatefunctionmakeAjaxCall(string$url): Promise{// request and return promise } privatefunctionmakeHttpCall(string$url): Promise{// request and return promise } }ভাল:
interface Adapter{publicfunctionrequest(string$url): Promise} class AjaxAdapter implements Adapter{publicfunctionrequest(string$url): Promise{// request and return promise } } class NodeAdapter implements Adapter{publicfunctionrequest(string$url): Promise{// request and return promise } } class HttpRequester{private$adapter; publicfunction__construct(Adapter$adapter){$this->adapter = $adapter} publicfunctionfetch(string$url): Promise{return$this->adapter->request($url)} }This is a scary term for a very simple concept. It's formally defined as "If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)." That's an even scarier definition.
The best explanation for this is if you have a parent class and a child class, then the base class and child class can be used interchangeably without getting incorrect results. This might still be confusing, so let's take a look at the classic Square-Rectangle example. Mathematically, a square is a rectangle, but if you model it using the "is-a" relationship via inheritance, you quickly get into trouble.
খারাপ:
class Rectangle{protected$width = 0; protected$height = 0; publicfunctionsetWidth(int$width): void{$this->width = $width} publicfunctionsetHeight(int$height): void{$this->height = $height} publicfunctiongetArea(): int{return$this->width * $this->height} } class Square extends Rectangle{publicfunctionsetWidth(int$width): void{$this->width = $this->height = $width} publicfunctionsetHeight(int$height): void{$this->width = $this->height = $height} } functionprintArea(Rectangle$rectangle): void{$rectangle->setWidth(4); $rectangle->setHeight(5); // BAD: Will return 25 for Square. Should be 20.echosprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()) . PHP_EOL} $rectangles = [newRectangle(), newSquare()]; foreach ($rectanglesas$rectangle){printArea($rectangle)}ভাল:
The best way is separate the quadrangles and allocation of a more general subtype for both shapes.
Despite the apparent similarity of the square and the rectangle, they are different. A square has much in common with a rhombus, and a rectangle with a parallelogram, but they are not subtypes. A square, a rectangle, a rhombus and a parallelogram are separate shapes with their own properties, albeit similar.
interface Shape{publicfunctiongetArea(): int} class Rectangle implements Shape{private$width = 0; private$height = 0; publicfunction__construct(int$width, int$height){$this->width = $width; $this->height = $height} publicfunctiongetArea(): int{return$this->width * $this->height} } class Square implements Shape{private$length = 0; publicfunction__construct(int$length){$this->length = $length} publicfunctiongetArea(): int{return$this->length ** 2} } functionprintArea(Shape$shape): void{echosprintf('%s has area %d.', get_class($shape), $shape->getArea()).PHP_EOL} $shapes = [newRectangle(4, 5), newSquare(5)]; foreach ($shapesas$shape){printArea($shape)}ISP states that "Clients should not be forced to depend upon interfaces that they do not use."
A good example to look at that demonstrates this principle is for classes that require large settings objects. Not requiring clients to set up huge amounts of options is beneficial, because most of the time they won't need all of the settings. Making them optional helps prevent having a "fat interface".
খারাপ:
interface Employee{publicfunctionwork(): void; publicfunctioneat(): void} class HumanEmployee implements Employee{publicfunctionwork(): void{// ....working } publicfunctioneat(): void{// ...... eating in lunch break } } class RobotEmployee implements Employee{publicfunctionwork(): void{//.... working much more } publicfunctioneat(): void{//.... robot can't eat, but it must implement this method } }ভাল:
Not every worker is an employee, but every employee is a worker.
interface Workable{publicfunctionwork(): void} interface Feedable{publicfunctioneat(): void} interface Employee extends Feedable, Workable{} class HumanEmployee implements Employee{publicfunctionwork(): void{// ....working } publicfunctioneat(): void{//.... eating in lunch break } } // robot can only workclass RobotEmployee implements Workable{publicfunctionwork(): void{// ....working } }This principle states two essential things:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend upon details. Details should depend on abstractions.
This can be hard to understand at first, but if you've worked with PHP frameworks (like Symfony), you've seen an implementation of this principle in the form of Dependency Injection (DI). While they are not identical concepts, DIP keeps high-level modules from knowing the details of its low-level modules and setting them up. It can accomplish this through DI. A huge benefit of this is that it reduces the coupling between modules. Coupling is a very bad development pattern because it makes your code hard to refactor.
খারাপ:
class Employee{publicfunctionwork(): void{// ....working } } class Robot extends Employee{publicfunctionwork(): void{//.... working much more } } class Manager{private$employee; publicfunction__construct(Employee$employee){$this->employee = $employee} publicfunctionmanage(): void{$this->employee->work()} }ভাল:
interface Employee{publicfunctionwork(): void} class Human implements Employee{publicfunctionwork(): void{// ....working } } class Robot implements Employee{publicfunctionwork(): void{//.... working much more } } class Manager{private$employee; publicfunction__construct(Employee$employee){$this->employee = $employee} publicfunctionmanage(): void{$this->employee->work()} }Try to observe the DRY principle.
ডুপ্লিকেট কোড এড়ানোর জন্য সব সময় ই চেষ্টা করা উচিত। ডুপ্লিকেট কোড খারাপ কারণ হল যে আপনার যদি কোন কিছু পরিবর্তন করতে হয় তাহলে যত জায়গায় ডুপ্লিকেট কোড লিখেছেন সব জাইগায় ই পরিবর্তন করতে হবে।
মনে করেন আপনি একটা রেস্টুরেন্ট চালাচ্ছেন এবং আপনার সব ইনভেন্টরি এর হিসাব রাখতে হয়: টমাটো, পেয়াজ, রসুন, মরিচ ইত্যাদি। এখন যদি এই ইনভেন্টরি এর জন্য আলাদা আলাদা লিস্ট করেন তাহলে প্রতি বার ই আপনার সবগুলো লিস্ট আপডেট করতে হবে যদি কোন লিস্ট আপডেট করতে ভুলে যান তাহলে আপনার হিসাব ও ভুল হয়ে যাবে। কিন্তু যদি শুধু একটা লিস্ট থাকত তাহলে কত সহজেই কাজ হয়ে যেত শুদু মাত্র একটা জায়গায় তেই পরিবর্তন এর মাধ্যমে।
যদি এমন হয় যে আপনার ডুপ্লিকেট কোড করার কারণ হল আপনার কাছে ২ টা আলাদা জিনিষ আছে কিন্তু সেখানে কিছু জিনিস একই রকম যেটা আপনাকে ফোর্স করতেছে আলাদা ভাবে কোড করার জন্য। তাহলে আপনি ওই আলদা জিনিসগুলো আলাদা ভাবে abstraction করে জিনিসটা হান্ডেল করতে পারেন আর এর জন্য ওই আলাদা জিনিষ গুলো একটা function/module/class এ নিয়ে কাজ করতে পারেন তাহলে এই function/module/class আপনি অন্য কোথায় লাগলে সেখানে ব্যাবহার করতে পারবেন।
abstraction করা আসলে একটুখানি কঠিন এর জন্য আপনার উচিত হবে SOLID principles এর Classes section তা ফলো করা। কারণ খারাপ abstraction ডুপ্লিকেট কোড এর থেকেও বেশি খারাপ, তাই আপনাকে এই বিষয়ে অবশ্যই সাবধান হতে হবে।
খারাপ:
functionshowDeveloperList(array$developers): void{foreach ($developersas$developer){$expectedSalary = $developer->calculateExpectedSalary(); $experience = $developer->getExperience(); $githubLink = $developer->getGithubLink(); $data = [$expectedSalary, $experience, $githubLink]; render($data)} } functionshowManagerList(array$managers): void{foreach ($managersas$manager){$expectedSalary = $manager->calculateExpectedSalary(); $experience = $manager->getExperience(); $githubLink = $manager->getGithubLink(); $data = [$expectedSalary, $experience, $githubLink]; render($data)} }ভাল:
functionshowList(array$employees): void{foreach ($employeesas$employee){$expectedSalary = $employee->calculateExpectedSalary(); $experience = $employee->getExperience(); $githubLink = $employee->getGithubLink(); $data = [$expectedSalary, $experience, $githubLink]; render($data)} }Very good:
কোড এর কম্প্যাক্ট ভার্সন ব্যাবহার করা সবসময় ভাল।
functionshowList(array$employees): void{foreach ($employeesas$employee){render([$employee->calculateExpectedSalary(), $employee->getExperience(), $employee->getGithubLink()])} }এটা অন্যান্য ভাষাতেও খুঁজে পাবেন এখানেঃ
- 🇧🇩 বাংলা:
- 🇨🇳 Chinese:
- 🇷🇺 Russian:
- 🇪🇸 Spanish:
- 🇧🇷 Portuguese:
- 🇹🇭 Thai:
- 🇫🇷 French:
- 🇻🇳 Vietnamese
- 🇰🇷 Korean:
- 🇹🇷 Turkish:
- 🇮🇷 Persian: