Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to go in which class?

Tags:

oop

I have 2 classes, player and game and a method called get_player_games($player_id). Which class does this method belong to?

I seem to run into this problem a bit, where I'm not 100% sure which class the method should go in. Is it just a matter of preference?

Thanks

like image 735
Scott Avatar asked May 21 '26 12:05

Scott


1 Answers

It's a typical cross-reference sort of situation -- to be completely objective, it can go in either class. It really is a matter of preference, assuming that a player can have multiple games and a game can be associated with multiple players.

That said, I've always approached it philosophically from the way you're inclined to name the method in the first place -- get_player_games() sounds like you're interested in the games for a particular player. The player is the subject of interest (based on the limited English-style grammar of the method name), the games happened to be the detail associated with that. If you had said get_game_players() or get_games_for_player(), then I would say that the games are the subject of interest, not the player.

Obviously, I'm biased by the noun you chose first (again, from a biased English grammar point of view), but since you can argue that the method belongs in either class, it's the only possible basis to use in such a case.

Again, it's somewhat of an arbitrary decision, but I've always been inclined to follow what the method naming rhetorically suggests to me about in which class it belongs.

like image 104
kvista Avatar answered May 24 '26 22:05

kvista