Does the PHP method have line limits?

Asked

Viewed 667 times

4

The size of the method is 22 lines. (20 Allowed)

Netbeans gives me this "warning," so why? It’s a PHP rule?

What’s best:

  1. A great method with all the code he needs.
  2. Split this code into smaller methods
  3. There is an appropriate number of lines in a method?
  • 1

    Related: https://answall.com/a/31473/101

  • I recommend the book Clean Code, by Robert Martin, it addresses in a very clear way the best practices for creating classes, methods, and several other extremely important points for an agile development

3 answers

3


That’s a suggestion netbeans no line or character limit for a method. The ideal is that it solves only one problem or has only one responsibility.

1) A large method with all the code it needs.

In this situation there is a great possibility that the method is doing several things.

2) Divide this code into smaller methods

In some cases it is the best solution.

3) There is an appropriate number of lines in a method?

Not.

  • Thank you, rray! Good night!

  • 1

    Just remember the code units.

  • 1

    This is the worst kind of "good practice" I know, forced by the IDE without any context.

0

The rray response is good, just add one thing: You let the less specific methods have to do with code reuse, with less specific methods you can reuse them more.

Maybe this is best seen when you’re at the design part of the application or later when you’re going to review the code and see that there’s a lot of repeated stuff that could be done once and repurposed more than once.

  • I think I understand what you mean, but "less specific methods" gives the wrong idea.

0

Netbeans gives me this "warning," so why? It’s a PHP rule?

According to this thread in the bug tracking of Netbeans, is just one hint which can be disabled in Editor > Hints.

And no, that’s not a rule of PHP. To the PHP we usually follow the PSR, acronym for PHP Standards Recommendations, where there are standards defined by the community to standardize development with language. In your specific doubt, there is the topic 2.3. Lines which says the following:

  • The line should not have a strict limit on its length. The maximum should be 120 characters, displaying a warning but not causing error. The ideal is that it is 80 characters or less, but if necessary it can be broken.
  • One command per line;
  1. A great method with all the code he needs.
  2. Split this code into smaller methods
  3. There is an appropriate number of lines in a method?

These three questions go deeper into development patterns and architectures, so I’ll try to answer in a single shot:

According to this thread in Engineering / se software, Robert Martin in his book, Clean Code: A Handbook of Agile Software Craftsmanship, says:

The first rule of function is that they should be small. The second rule is that they should be smaller than that. They should not have more than 100 lines. They should hardly exceed 20 lines.

If a method is large and there really is a need for it, it is a specific case of your software. But if you can take a closer look at it, make sure she comes in according to Princípio da Responsabilidade Única, also Robert Martin says that she should be responsible for only one thing.

The division into smaller codes also comprises the following topics:

  • Testability
  • Cohesion and Coupling
  • Understanding

Reading tip:

  • There’s one thing I need to agree on 100%, these numbers are almost arbitrary and are meant to sell products, make it look like the IDE is better than books, lectures, etc. People love these things, but they create more problems than solutions.

  • I don’t get it, @bigown. What numbers? The hint of ide? I don’t agree with those either style code down her throat.

  • Everything that tries to put in numbers something that cannot be put this way. IDE, books, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.