Return product ID, if it is specified in a form I often encounter this problem - I need to get some value, if it exists, and then do something with that value. 

Getting that value becomes a separate method, and since that value might not exist, I need to indicate that somehow. For example, I might have a method that returns a productId if a product is attached to the form the user is filling out. If it doesn't exist, I can return 0, which is an invalid productId, which is a magic number. 

My current problem with this instance is this:

public int GetProductIdFromFormIfAny(JObject form)
{
  int productId = 0; //default to 0 
  IList&lt;FormComponentMetadata&gt; formFields = ParseFormForDropdowns(form);
  bool isProductAttached = formFields.Any(field =&gt; field.DataLookupTable == "Products");
  if(isProductAttached){
    FormComponentMetadata productField = formFields
      .Single(field =&gt; field.DataLookupTable == "Products");
    string filterStringContainingProductId = productField.DataLookupFilter;
    productId = filterStringContainingProductId.Split("=")[1];
  }
  return productId;
}


I don't know how to get around the magic number issue. I can create a new class, which holds an id and a bool indication whether or not that id exists, but that seems very heavy-handed and a LOT of extra code that someone has to maintain, even though the tradeoff is clarity. 

As a side note, this is what comes back for the filterStringContainingProductId: "data_lookup_filter":"Product_ID=9" - I am not sure how to make the Split("=")[1] clear without comments or constants, which, again, seem heavy. 



Edit: Looks something like this when refactored to use nullable types.

 public int? GetProductIdFromFormIfAny(JObject form)
{
  IList&lt;FormComponentMetadata&gt; formFields = ParseFormForDropdowns(form);
  bool isProductAttached = formFields.Any(field =&gt; field.DataLookupTable == "Products");
  int? productIdOrNull = isProductAttached ?GetExistingProductId(formFields) : default(int?);
  return productIdOrNull;
}

private int GetExistingProductId(IList&lt;FormComponentMetadata&gt; formFields)
{
  const string keyValueSeparatorChar = "=";
  const int idxOfProductId = 1;
  FormComponentMetadata productField = formFields
  .Single(field =&gt; field.DataLookupTable == "Products");
  string filterStringContainingProductId = productField.DataLookupFilter;
  string productIdSegment = filterStringContainingProductId.Split(keyValueSeparatorChar)[idxOfProductId];
  int productIdAsInt = int.Parse(productIdSegment);
  return productIdAsInt;
}


Seems like a ton of code for something so trivial...
It looks like your code would benefit from a few well-chosen utility methods. It also looks a bit 'stringly typed': things would be much easier if you could store that product ID in an integer field or property in the first place. But it's difficult to tell whether that is possible without more context.

A reusable method that can fetch a specific value from a filter string would have the biggest impact. A method that makes it easier to fetch specific fields from a form might also be useful. And don't forget about the ?. operator. With those, your code can be rewritten as following:

public int? GetProductIdFromFormIfAny(JObject form)
{
    // Returns null if no matching field is found:
    var productsField = GetFieldForTable(form, "Products");

    // Returns null if the given filter is null, or if it does not contain the specified key.
    // Note that the 'magic' index 1 is replaced by a more descriptive key name:
    var productIDValue = GetFilterValue(productsField?.DataLookupFilter, "Product_ID");

    // Returns null if the given string is null or not a valid integer:
    return int.TryParse(productIDValue, out var productID) ? productID : null;
}


I can't tell whether this is exactly what you need - maybe you'll want an exception to be thrown if a filter is malformed, for example - but it should give you some ideas.