[haiku-commits] Re: haiku: hrev46896 - src/preferences/keymap

  • From: Stephan Aßmus <superstippi@xxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 21 Feb 2014 11:03:37 +0100

On 21.02.2014 10:46, Axel Dörfler wrote:
Am 21/02/2014 00:12, schrieb John Scipione:
On Thu, Feb 20, 2014 at 5:41 PM, Jérôme Duval <korli@xxxxxxxxxxxxxxxx>
wrote:
2014-02-20 21:21 GMT+01:00  <jscipione@xxxxxxxxx>:
   Convert private class member methods to static functions.
Why do you convert these?
http://www.akiwi.co.uk/article_001.html

I can see most of them being useful independently from the class, but
pulling _SwapModifiersMenuItem() out of the class context makes no sense
to me.

About that article: don't just believe everything that is written.

There is only one good reason for moving member functions out of the
class context: reusability in other contexts. If stuff belongs to a
class, it should be there, and not live as a static function.

There is another one to make member functions static, though: you save
one argument on the stack (the object) which makes for a slightly faster
call.

Sometimes it makes for more re-usable code if you don't access members of a class directly in a method, but pass them as arguments to the method.

I think writing code so that it "could" be more easily re-used in theory rarely is worth the extra effort. Avoiding duplicated code and refactoring is always worth the effort, so don't get me wrong.

The more important argument for me tends to be what makes the code more readible and easier to follow. If the extra effort to make code potentially re-usable obscures what is going on, or leaves someone wondering... it can be a bad thing. For example, if I read such code, I think the effort to extract the method was done because the code *is* actually used from elsewhere. And then if I want to change some behavior, I can't just right-click the method and invoke "Open call hierarchy" like I can in Java, so I start searching the entire codebase for where this might be used as well in order not to break that code with my change... So keeping somethig local to a class also has benefits. I think static class methods make sense for code that belongs into a class conceptually but should be publically accessible without a class instance. If the method is private, it probably shouldn't be declared static, but maybe const instead. If it doesn't access class members, it is easily extracted and refactored anyway... if the need arises.

Best regards,
-Stephan


Other related posts: