Tweeted twitter.com/StackProgrammer/status/701258322498949120
5 edited body
source | link

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I tried and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. ActaullyActually the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I tried and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. Actaully the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I tried and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. Actually the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

4 edited title
| link

“Smart create”methodcreate” method naming

3 added 2 characters in body
source | link

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I trytried and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. Actaully the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I try and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. Actaully the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

Every class of a model in my application has a create method. The framework gives me the default implementation of create which is “create in the DB”. Sometimes I need to perform some extra actions during create. There are numerous of ways to achieve this: I can override create (and call super thereafter), subsribe to signals (if possible), override hook methods (such as before_create), it has never been a problem. The problem is, I don't actually want to perform this actions every time, i. e. these actions are not actually part of the create procedure, it's a part of some other high-level (comparing to the original create) procedure. They call it smart_create in one of the biggest projects I've ever worked with.

Before proceeding any further, I want to give you an idea of what these smart_create methods usually are. So, here is the example. When you create some entity for a user, you may want to notify him with an email. But you may not want notifications during tests (it may slow them down), stand-alone scripts (when you repair something you just broke) or when an entity is created via admin panel (for whatever reason). The temptation is there to name this method create_and_notify, but this won't work when you need some other actions: create_and_notify_and_add_bonus_credits is not an appropriate name.

So, what is the question? I don't like to name my methods smart_create, but I can't really tell what name is better. Here is what I tried and why I still don't like it:

  • smart_create. What if I have two untrivial ways of creating entity? What is the name for the second one? wise_create? More than this, how do other developers know what method to call? Do they need to “go smart”?
  • EntityCreator class. Actaully the same but with the whole class instead of just one method. Nothing really changes: it's still unclear what this “creator” do and why it's better than bare “create”.
  • No name at all. Because controller knows what to do. That's just dirty. The problem with this approach is clear: I will not be able to reuse the same logic again.
  • create_by_user. Here. This is what method really does, but I don't actually want to mention some users or clients in my models: they are not supposed to know about them at all.

I believe a lot of developers encountered such situations, and I want to know what is your usual approach to this problem.

2 added 167 characters in body
source | link
1
source | link