C
C#17mo ago
SWEETPONY

✅ How to improve my code?

I have following 2 methods:
static public UISchemaBuilderBase AddField(
this UISchemaGroupBuilder builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;

switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}

return builder;
}

static public UISchemaBuilderBase AddField(
this UISchemaBuilderBase builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;

switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}

return builder;
}
static public UISchemaBuilderBase AddField(
this UISchemaGroupBuilder builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;

switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}

return builder;
}

static public UISchemaBuilderBase AddField(
this UISchemaBuilderBase builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;

switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}

return builder;
}
the difference is: builder type but another code is the same
2 Replies
Angius
Angius17mo ago
If that's the only difference, why not make the method generic? Or even better, assuming UISchemaGroupBuilder inherits from UISchemaBuilderBase there's no need for the former
SWEETPONY
SWEETPONYOP17mo ago
I don't think it's good solution let's see code of two classes:
public class UISchemaGroupBuilder : UISchemaBuilderBase
{
public UISchemaGroupBuilder AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null)
{
AddBoolean(fieldName, fieldTitle, isRequired, _name);
return this;
}
}

public class UISchemaBuilderBase
{
public UISchemaBuilderBase AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null,
string groupName = null )
{
var schemaControl = new UIControlBooleanAttribute();

CreateUISchemaItem(
fieldName,
fieldTitle,
groupName,
isRequired,
schemaControl);

return this;
}
}
public class UISchemaGroupBuilder : UISchemaBuilderBase
{
public UISchemaGroupBuilder AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null)
{
AddBoolean(fieldName, fieldTitle, isRequired, _name);
return this;
}
}

public class UISchemaBuilderBase
{
public UISchemaBuilderBase AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null,
string groupName = null )
{
var schemaControl = new UIControlBooleanAttribute();

CreateUISchemaItem(
fieldName,
fieldTitle,
groupName,
isRequired,
schemaControl);

return this;
}
}
in this code:
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
I always call AddBoolean from UISchemaBuilderBase but sometimes I need to call AddBoolean from UISchemaGroupBuilder
Want results from more Discord servers?
Add your server