Ad
  • Custom User Avatar

    it is clever but the readability of it is not that great... the condition and output of if statement is on the same line, i would say is not best practice beacause the author has sacrificed readability for realestate.

  • Custom User Avatar

    Best practices are the generally accepted manner of writing code. Doesn't really matter what that manner is, as long as a lot of people agree on it. The fact that 14 people all wrote this code in almost exactly the same manner makes it a pretty good estimate of what best practices are, at least for the problem at hand.

    I disagree with you on the regexp bit. It's a computational efficiency hellhole to do that. It simply wouldn't work well with (for example) parsers, where the order of matches is often very important and implementing a regexp in that way is entirely unfeasible. However, generally when you're writing a parser, you don't write a single regexp, you generate one. I get what you're saying though, and that's one reason named captures were added as a feature to a lot of regexp engines. A quick Google search even tells me that Python (surprisingly not Perl) was the first implementation that included it.

  • Custom User Avatar

    IMO
    Best practice is when a code is small, efficient and easy to understand.

    When people refactor, they sometimes make the code smaller but a bit unreadable. That's not the best practice.
    Sometimes people add unneeded generalisation, that is also not the best practice.

    For example a function that returns if a regex pattern contains 3 groups of chars is better done as 3 separate regex tests for each group. Even if that is 3 times more tests, the code will be much easier to read.

  • Custom User Avatar

    I don't really like the term "best practice", as I can't point my finger to what exactly it means. It would be better if you had something to compare this code with. Is it something in particular that you don't like here?

  • Custom User Avatar

    I can't really find a better implementation going through the solutions. Maybe using the variable names shorter, longer would add some readability, but the scope of the problem is so small that maybe being more concise is better.

  • Custom User Avatar

    That's exactly the point, why this solution isn't the best.