Skip to main content
typos ...
Source Link

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even existexists and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenateregenerate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exist and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exists and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenerate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

added 78 characters in body
Source Link

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAVEAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exist and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exist and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exist and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.

Source Link

Real life example: I am working with a third party system wherein "entities" are stored with "fields". Basically an EAV system. As it is fairly easy to add another field, you get access to one by using the field's name as string:

Field nameField = myEntity.GetField("ProductName");

(note the magic string "ProductName")

This can lead to several problems:

  • I need to refer to external documentation to know that "ProductName" even exist and its exact spelling
  • Plus I need to refer to that doc to see what the datatype of that field is.
  • Typos in this magic string will not get caught until this line of code is executed.
  • When someone decides to rename this field on the server (difficult while preventing dataloss, but not impossible), then I cannot easily search through my code to see where I should adjust this name.

So my solution for this was to generate constants for these names, organised by entity type. So now I can use:

Field nameField = myEntity.GetField(Model.Product.ProductName);

It is still a string constant and compiles to the exact same binary, but has several advantages:

  • After I have typed "Model.", my IDE shows just the available entity types, so I can select "Product" easily.
  • Then my IDE supplies just the fieldnames that are available for this type of entity, also selectable.
  • Auto-generated documentation shows what the meaning of this field is plus the datatype that is used to store its values.
  • Starting from the constant, my IDE can find all places where that exact constant is used (as opposed to its value)
  • Typos will be caught by the compiler. This also applies when a fresh model (possibly after renaming or deleting a field) is used to regenate the constants.

Next on my list: hide these constants behind generated strongly typed classes - then also the datatype is secured.