Is there a naming convention for a method that should set properties of the passed object

I am writing a method that will set the properties of an object passed as a parameter. The method takes one type interface parameter, and the return type of the method is the same interface. I was wondering if there is some naming convention for methods like this. I was thinking about something like:

FillInInterfaceTypeData strong> or InitInterfaceTypeData strong>, but both sounds awkward to me. What do you think?

+2


a source to share


4 answers


There are no hard and fast rules here. Both of these names sound good, but I would normally use something like PopulateInterfaceTypeData

.



+3


a source


FillInInterfaceTypeData sounds great to me. Perhaps if you even briefly list the details that I think everything fits. It all depends on the context.



+1


a source


I guess it depends on what you are filling with objects and why?

Are you setting the default values? In this case, something like InitialiseTypeData or DefaultTypeData might make sense.

Perhaps you have cleared your values? It is possible that the state of the objects is moving to another. In this case, something like DeactivateBankAccount or MakeUserMarried or whatever is done with the objects.


Beware of Code Smells Often (not always, of course) you will find that if you have difficulty assigning a name to a method, this indicates that there is something wrong with the design, which can lead to difficulties later.

There may be other things in the design that you should consider.

For example, if this method sets default values, isn't that what should happen in the constructor of the class?

If an interface represents multiple class types that all need the same values, perhaps each of these classes can be split into 1 class that represents the same between each of these classes and another class that represents the difference.

t.

interface IA
{
  int a;
  int b;
}
class X : IA
{
  int a;
  int b;
  int c:
}
class Y : IA
{
  int a;
  int b;
  int d:
}

      

can be reorganized into

class A
{
  int a;
  int b;
}

class X
{
  A a;
  int c:
}

class Y
{
  A a;
  int d:
}

      

Then the constructor of class A can set its default values. You no longer need to put in any effort to come up with a good function name. You also get a more orthogonal system that is easier to test and handle changes.

Obviously this is probably not really your situation, I'm just stressing how when you are trying to find a good name because your design needs improvement!

+1


a source


Depending on which language you are using: if it supports dynamic linking, you can remove the "TypeData" suffix and let the executable do its job.

Just name your methods

SetDefaultsFor(Customer c) { ... }
SetDefaultsFor(Dog d)      { ... }

      

0


a source







All Articles