Skip to content

Instantly share code, notes, and snippets.

@fnbk
Created February 22, 2021 17:49
Show Gist options
  • Save fnbk/ea9c62d890d2857bac4b590c684c6cdf to your computer and use it in GitHub Desktop.
Save fnbk/ea9c62d890d2857bac4b590c684c6cdf to your computer and use it in GitHub Desktop.
#
# SoC - Separation of Concerns
#
Definition:
Order your thoughts by focusing on one aspect at a time, establish boundaries and organize the system into meanigful parts. - Edsger W. Dijkstra
Explanation:
Software development is a multidimensional discipline and it involves looking at different aspects simultaneously.
The idea of separating the different aspects of a problem allows us to solve one problem at a time and eases the process of reasoning. Isolating aspects also lets work to be postponed to a later time and allows collaboration to happen.
By focusing one's attention upon some aspect it does not mean ignoring the other aspects, it is only from this aspect's point of view that the other is irrelevant. It is being one- and multiple-track minded simultaneously.
#
# Key takeaways
#
* Make everything as simple as possible, but not simpler. - Albert Einstein
* Organize the system into elements of cohesive responsibilities (related things are close to each other)
* Establish a well organized system where each part fulfills a meaningful role and is able to adapt to change
#
# Version 1 (Bad)
#
* spaghetti code (difficult to read, error prone)
public bool Try2GetBook(string searchValue, out IResultObject resultObject)
{
resultObject = null;
if (searchValue is null)
searchValue = string.Empty;
if (searchValue.ToUpper().StartsWith("GUID:") && searchValue.Length >= 6 && Guid.TryParse(searchValue.Substring(5), out Guid newGuid))
{
resultObject = GetBookByGUID(searchValue);
if (resultObject == null)
{
resultObject = GetBookByGUID(searchValue.Substring(5));
}
}
else if (Guid.TryParse(searchValue, out Guid newGuid2))
resultObject = GetBookByGUID(searchValue);
else if (int.TryParse(searchValue, out int resourceId))
resultObject = GetBookByID(resourceId);
else if (!string.IsNullOrWhiteSpace(searchValue))
resultObject = GetBookByName(searchValue);
return resultObject != null && !string.IsNullOrWhiteSpace(resultObject.ObjectClass);
}
#
# Version 2 (Good)
#
* Order your thoughts by focusing on one aspect at a time (guard, search type, get computer, return value)
public bool Try2GetBook(string searchValue, out IResultObject resultObject)
{
//
// guard
//
if (searchValue is null)
{
resultObject = null;
return false;
}
//
// Search Type
//
string searchType = "";
if (searchValue.ToUpper().StartsWith("GUID:") && searchValue.Length >= 6 && Guid.TryParse(searchValue.Substring(5), out _))
{
searchType = "GUID";
}
else if (Guid.TryParse(searchValue, out _))
{
searchType = "GUID";
}
else if (int.TryParse(searchValue, out _))
{
searchType = "ID";
}
else if (!string.IsNullOrWhiteSpace(searchValue))
{
searchType = "NAME";
}
//
// GetBook()
//
switch (searchType)
{
case "GUID":
resultObject = GetBookByGUID(searchValue) ?? GetBookByGUID(searchValue.Substring(5));
break;
case "ID":
var id = int.Parse(searchValue);
resultObject = GetBookByID(id);
break;
case "NAME":
resultObject = GetBookByName(searchValue);
break;
default:
throw new Exception();
}
//
// return type
//
var found = resultObject != null && !string.IsNullOrWhiteSpace(resultObject.ObjectClass);
return found;
}
#
# Version 3 (Better)
#
* establish boundaries (enum type)
* organize into meaningful parts (extract into functions)
public bool Try2GetBook(string searchValue, out IResultObject resultObject)
{
var searchType = GetSearchType(string searchValue)
switch (searchType)
{
case SearchType.GUID:
resultObject = GetBookByGUID(searchValue);
break;
case SearchType.ID:
var id = int.Parse(searchValue);
resultObject = GetBookByID(id);
break;
case SearchType.NAME:
resultObject = GetBookByName(searchValue);
break;
default:
resultObject = null;
break;
}
return isValidResultObject(resultObject);
}
public enum SearchType
{
GUID = 1,
ID = 2,
NAME = 3,
UNDEFINED = 0
}
public SearchType GetSearchType(string searchValue)
{
var searchType = SearchType.UNDEFINED;
if (searchValue is null)
{
searchType = SearchType.UNDEFINED;
}
else if (searchValue.ToUpper().StartsWith("GUID:") && searchValue.Length >= 6 && Guid.TryParse(searchValue.Substring(5), out _))
{
searchType = SearchType.GUID;
}
else if (Guid.TryParse(searchValue, out _))
{
searchType = SearchType.GUID;
}
else if (int.TryParse(searchValue, out _))
{
searchType = SearchType.ID;
}
else if (!string.IsNullOrWhiteSpace(searchValue))
{
searchType = SearchType.NAME;
}
return searchType;
}
public bool isValidResultObject(IResultObject resultObject) {
return resultObject != null && !string.IsNullOrWhiteSpace(resultObject.ObjectClass);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment