joomla-projects / gsoc16_recording-action-logs

Recording actions logs, accessible by super admin
https://summerofcode.withgoogle.com/projects/#4547036649095168
GNU General Public License v2.0
4 stars 4 forks source link

Question : Using function from another component #26

Closed muhakh closed 8 years ago

muhakh commented 8 years ago

For building #18 I can use the same function used in com_users as in this file https://github.com/joomla-projects/gsoc16_recording-action-logs/blob/staging/administrator/components/com_users/models/users.php#L444 The question is, would it better to follow this link http://forum.joomla.org/viewtopic.php?p=2492279 and just include the model and use this function or I must rewrite the function and use it to solve this issue? Or there is another solution for this problem?

Llewellynvdm commented 8 years ago

To get another components model you can setup a helper function like this getModel function and then simply call the model and use it.

Yet in this case I would copy the function into your model instead, well that is what I would do :)

muhakh commented 8 years ago

Won't this be a violation of the "don't repeat yourself" concept?

Llewellynvdm commented 8 years ago

Yes it may, but as you can see all over the core components methods inside the models are always written and hardly ever are they using other components methods (meaning the banner does not use contents model methods), since mostly there are small little changes in each to justify it being uniquely coded.

Further more since you are already extending the JModelAdmin class you are already not writing a lot of code again and again. So code that is exactly the same for all live down in the libraries, yet in the components we often overwrite them to the unique needs of the component. So not repeating yourself goes for most of the API yet here on the component level we find that the core components often repeats bits of code.

That is why the FOF was build as far as I understand. The fof is an easier way to build components that allows for almost no repeating of one self, since it is mostly xml and a few lines of code. Yet I have found it to not yet implement all the features that the legacy implementation allows for, and the core components are all still heavily reliant on the legacy classes.

But hey, you are most welcome to use the getModel way, as you see I also use that from time to time... but usually not in the model but in other areas of either the view or controller, or even when another component needing data from this model.

chrisdavenport commented 8 years ago

Technically, yes it would be a violation of DRY. But the DRY principle isn't set in stone and knowing when it's okay to bend the rules a bit is part of learning how to code well. If we were building a single monolithic codebase then it would make sense to maximise dryness, but in this case we are talking about separate components which we want to keep as loosely coupled as possible, so sometimes it's okay to copy small parts here and there. Loose coupling is another one of those principles that we try to uphold but isn't set in stone. As you can see, DRY and loose coupling are sometimes in opposition. It's a matter of balancing the different forces that shape the code.

Llewellynvdm commented 8 years ago

I think this a great answer!