-5

like my all teachers are saying " You can do everything because you can, but your code looks like a sh**"

So I was looking for some good tutorials on internet about Writing PHP code so clean as it's possible. I didn't find anything so I need your help. I need some url with tutorials about how to write clean php code. It can be BOOK as well!

ArturK.
  • 35
  • 1
  • 6

2 Answers2

2

I don't know what your teacher what meant but I'm pretty sure writing clean code is write code what make sense. What good write less if it's really hard to understand? For me, clean code doesn't mean syntax or less code only. Just write such that makes sense, use best practices and standards. It's a great start! Because you brought up on books, here's one of these:

felipsmartins
  • 12,429
  • 4
  • 44
  • 52
1

I think this is a matter of personal taste. When is the code clean? When is it not? This is hard to say, but there are some general guidelines which you can follow to help improve your code. Below I have given some of the guidelines that I use and they have helped me so far.

  1. Keep indentation consistent. For example keep code of the same block (body of an if statement) on the same indentation level.
  2. Avoid cryptic/unclear variable names. Instead of writing $r then write $result or $response. I hope you get the idea.
  3. Avoid cryptic/unclear function/method names. Attempt to name your functions with short and descriptive names, while not sacrificing readability. Its easier to read and understand check_directory_recursively than cdrecur.
  4. As mentioned in the videos (linked below), functions should only do one thing and do it well. If the function relies on some other functionality which is clearly different from what it is trying to accomplish, extract that functionality to another function. This also has to nice side effect that you can more easily reuse that functionality. The naming guidelines are also true for the arguments the function requires.
  5. Avoid code duplication. This extends on code reuse. When reusing code you reduce your code base and help other developers (and yourself in six months) understand/work with the code. When the same function name appears it is easier to maintain. And if an error/bug occurs you have a better idea to where the error comes from.
  6. Stick to a naming convention. There are two widely used types: CamelCase and snake_case
  7. Document your code. Use the PHPDocumentor to write good documentation for your functions/methods. This also has the nice side effect that IDE's can read this and help your code-completion. Just remember not to document every single thing you do. If you should only document code that doesn't immediately show its purpose. if you are doing some very complex calculations, a short comment helps the developer read your code flow.

Then there are something which takes time, but to learn all (or most of them) of the native PHP functions. Take advantage of namespaces, exceptions, The SPL library, the array and string functions and I could go on and on. When using their functions and classes you get 3 main benefits.

  1. Shorter and more concise code, with function names other developers have a better change of knowing.
  2. good documentation (most of the time). There are some areas of the PHP manual which lacks quite much.
  3. Performance increase. The functions written by PHP are optimized with a faster language than PHP self. This may not be directly visible though.

Then to two things that I personally have found useful:

  • Surround your code with enough space. With this I mean it doesn't matter if there is a blank line on both sides of a return statement. It helps me better distinguish early returns inside functions and make the code flow more clear.
  • If an extra variable makes readability/maintainability easier, then add it. But remember the naming guidelines and do not just add it because you feel like it and sun is bright today :D

You can also watch the first two (or more) videos from this awesome answer.

Hope this (though very personal taste) can help you. Happy coding!

Community
  • 1
  • 1
AnotherGuy
  • 615
  • 14
  • 19