🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
nicolas267 ·
Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />Hello, good morning to everyone reading this. I came to present some good practices I found while researching PHP that helped me improve my code.<br /><br />1. Avoid nesting too deeply <br /><br />Too many if-else statements can result in code that is hard to read.<br /><br />How it should not be <br />Código: php
<br /><br />2. Don't add unnecessary context to variables <br /><br /> If the class/object name already tells you what it is, there's no need to repeat it in the variables<br /><br />How it should not be<br />Código: php
<br />class Car<br />{<br /> public $carMake;<br /> public $carModel;<br /> public $carColor;<br /><br /> //...<br />}<br />
<br /><br />How it should be<br />Código: php
<br />class Car<br />{<br /> public $Make;<br /> public $Model;<br /> public $Color;<br /><br /> //...<br />}<br />
<br /><br />3. Ideally, functions should have 2 or fewer arguments<br /><br /> Limiting the number of arguments is very important because it makes testing functions much easier. If you have more than three arguments you'll have to test different situations with each argument, which translates into tons of tests<br /><br /> A function with no arguments is ideal, one or two is fine, three should be avoided, more than three should be consolidated. Usually when a function has more than two arguments it's because it's doing too many things; in cases where it's necessary, an object is passed as an argument<br /><br />How it should not be <br />Código: php
<br />class MenuConfig<br />{<br /> public $title;<br /> public $body;<br /> public $buttonText;<br /> public $cancellable = false;<br />}<br /><br />$config = new MenuConfig();<br />$config->title = 'Foo';<br />$config->body = 'Bar';<br />$config->buttonText = 'Baz';<br />$config->cancellable = true;<br /><br />function createMenu(MenuConfig $config): void<br />{<br /> // ...<br />}<br />
<br /><br />4. Functions should do only one thing <br /><br />This is one of the most important rules of all. When functions do more than one thing, they become hard to compose, test, and reason about. If a function performs a single action, it can be refactored more easily and read more clearly<br /><br />How it should not be <br />Código: php
<br /><br />6. Avoid conditionals <br /><br />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 clean code concept we learned earlier: a function should only do one thing. When you have functions with if statements, you're saying that the function does more than one thing. Remember, just do one thing.<br /><br />How it should not be <br />Código: php
<br />class Airplane<br />{<br /> // ...<br /><br /> public function getCruisingAltitude(): int<br /> {<br /> switch ($this->type) {<br /> case '777':<br /> return $this->getMaxAltitude() - $this->getPassengerCount();<br /> case 'Air Force One':<br /> return $this->getMaxAltitude();<br /> case 'Cessna':<br /> return $this->getMaxAltitude() - $this->getFuelExpenditure();<br /> }<br /> }<br />}<br />
<br /><br />I hope you liked my first post, and please comment your opinions<br /><br />source: Los enlaces son solo para miembros. Registrate o iniciá sesión (English)
ANTRAX ·
Great contribution! I'm going to pin it so it stays fixed at the top of the PHP forum.<br />It's very, very useful<br /><br />Thanks a lot!<br />ANTRAX
nicolas267 ·
Cita de: ANTRAX en Diciembre 30, 2018, 11:29:40 PM<br />Great contribution! I'm going to pin it so it stays fixed at the top of the PHP forum.<br />It's very, very useful<br /><br />Thanks a lot!<br />ANTRAX<br />
<br /><br />thank you very much!, it motivates me to keep making more posts
tinc0h ·
A very good practice when you get tangled up in IFs is ternary operators.<br /><br />The ternary operator is a conditional operator that reduces code length while performing comparisons and conditionals. This method is an alternative to using if-else and nested if-else statements. The execution order for this operator is left to right.<br /><br />a quick example so it's understood.<br /><br />Código: php
($var==$condicion?true:false);
<br /><br />Comparing this simplification with a regular IF conditional, we save many lines of code that are sometimes unnecessary or cumbersome..<br />We can also modify the same variable we use for the condition, depending on whether it's met or not.<br /><br />Código: php
($var==$condicion?$var=$condicion++:$var=null);
<br /><br />If you find it interesting I can write a post about this topic!<br /><br />Regards!