New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate Code #623
Comments
You are correct we could reference the You will notice in most cases we are not using active record queries, and instead, write the query ourselves which we found is a performance improvement. Personally, doing lots of string interpolation (to put the table name in) within a custom query string annoys me. You are welcome to use the In the future, we might change the convention, but for now, we would rather it be consistent. Thanks for the feedback. |
BTW, I just made a commit to be consistent and use the table name string in those places where we using Thanks :) |
Mostly performance reasons. DB actions are localized in the service layer and table names are pretty unlikely to change, so introducing an overhead there just does not seem like the right move.
Product Record does not have actual properties you refer to (https://github.com/craftcms/commerce/blob/develop/src/records/Product.php#L17-L37), because it depends on the As far as Product Element goes, those properties should be defined similar to how it's in the Product Record to avoid ambiguity, but even if that wasn't the case, not all properties for the Product should be queryable and stuffing this all under a trait unnecessary couples the product and it's query. Furthermore, the vast majority of the properties don't even have the same definition. The
'commerce_products' is a table alias there, as the actual table can have a prefix. (https://github.com/craftcms/cms/blob/develop/src/elements/db/ElementQuery.php#L1550-L1552) Sticking to DRY is nice, but sometimes duplication is far cheaper than the wrong abstraction. |
@TheFox Please stop posting issues here and other Craft CMS repos if you are unable to follow the code of conduct. |
Why do you use so much duplicate code?
tableName()
function of each record. Why not calling this function instead of writing the same table name over and over again? Beside the mirgrations, you calltableName()
only on 5 different places. You could reusetableName()
from the record classes at least at over 90 places in the code.beforePrepare()
, you have at least 20'commerce_products'
strings in 20 lines. Even more. You could just reuse one string for all places.And those are just some parts I looked at. I could probably write here another 90 examples..
When writing code you shouldn't repeat yourself (DRY). It seems like your main business is not programming. I don't want to teach you programming, but this code looks like it's written by some interns and you even sell this product.
The text was updated successfully, but these errors were encountered: