Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 2
    This does a for each loop each time getTotalHp() is called. That is not really what I would call good practice. The __destruct() method looks painfully slow as well having to search and splice an array. Finally your $monsters property is non static but you use it a static context in getTotalHp(). Commented Jun 8, 2012 at 20:29
  • @scaraveos Why is that not good practice? It's not going to take long, and it keeps you from having to update the total value every time the hp is changed. Premature optimization if you ask me. Lost your upvote! Commented Jun 8, 2012 at 20:32
  • "Its not going to take long" is not an excuse. Practices like this will eventually add up in a real world application and the results will not be good (or fast). Commented Jun 9, 2012 at 1:05
  • @scaraveos Code for readability first, if performance is a problem, than you find the bottlenecks. I guarantee you you won't find more than 1ms difference when counting 20,000 monsters. Your approach requires maintenance of the static variables in at least four places, mine is just in the constructor/destructor. Here's a good article about premature optimization. geekswithblogs.net/BlackRabbitCoder/archive/2010/06/13/… You owe it to yourself to at least give it a read. Commented Jun 9, 2012 at 1:47
  • The defintion of premature optimization (called an Anti-Pattern) from Wikipedia: Coding early-on for perceived efficiency, sacrificing good design, maintainability, and sometimes even real-world efficiency Commented Jun 9, 2012 at 1:50