Skip to content

Instantly share code, notes, and snippets.

@JArmando
Created April 19, 2018 19:07
Show Gist options
  • Save JArmando/788cd48ac69b7a8902e854b9b8456ba3 to your computer and use it in GitHub Desktop.
Save JArmando/788cd48ac69b7a8902e854b9b8456ba3 to your computer and use it in GitHub Desktop.
Single page guide on when to create a class

Do I need a class?

• Do you have multiple properties that go around a specific noun?
	`string BookName`
	`int BookPageCount`
	`bool IsTheBookPretty`
	`string CapitalizedBookName`

	That noun should be class.

• Do you have multiple methods that go around a specific noun?
	`string getBookName()`
	`string getBookNameWithSugar()`
	`string getCountOfPagesThatArePretty()`
	`string setBookNameAsId()`
	`bool IsThisReallyABook()`

	That noun should be class.

• Do you have multiple methods that go around a very similar set of input parameters?
	`string DoSomething(string bookName, bool isTheBookPretty, int numberOfPages)`
	`string AreYouSure(int CountOfPagesThatArePretty, string bookName)`
	`int AddPagesToBook(string bookName, int totalPages, int pagesToAdd)`

	Find a noun that can be described by such properties and create a class.

• Do you have a "helper" class with public methods that work with more than 1 "thing"?
	`string DoSomethingWithABook()`
	`string DoSomethingWithATable()`
	`bool GetBookFromTable()`
	`BoolTransactionResult GetTableFromBookKillMePleaseICantEven`

	- Wrap those into their own concept, even if it means creating a class for each of them.

• Do you have a transformation from a DataSet into a bunch of variables?
	`string bookName = ds[0][1][15]["bookName"];`
	`int pageCount = SID.RedundantHelperThatShouldDie.BookHelper.getPageCount(ds, ds[0][1][15]["pageCount"]);`
	`string author = ds[0][1][15]["author"] != Db.NullStuff ? "Jimmy" : "Location";`

	- Most likely that DataSet is representing some "thing" name it and create a class

If your answer was YES to one or more of these questions then you, my friend, need a new class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment