function is what we do.fisrt container put codes in. how long ?20,30lines?
there is a simple rule to follow.
how to find classes by getting your function at right size.
do one thing , do it well, do it only. but what does onething mean?
They should be smaller.The second rule of functions that should small than that.
How big should a funtion be?
Back in the 1990s, we have a simple rule, a function should be a screen full.(about 20 lines.)
original the screen full is out of date,doesn't apply anymore."Four lines is ok, maybe five, six? ok,ten is way too big."
sound insame.
In a four line function ,how many indent how many if else statement,try catch blocks your can see.there is not enough room for indenting.
small functions have nice long descriptive names,
what's more, you will extrat the pridicts of your if statements and while loop into even more nice small named boolean functions.
example:
public String invoke() throws Exception{ if(pageData.hasAttribute("Test")){ content+= includeSetups(); content+=page.getContent()+"\n"; content+=includeTeardowns(); pageData.setContent(content.toString()); } return page.getHtml(); }
public String invoke() throws Exception{ if(isTestPage()){ content+= includeSetups(); content+=page.getContent()+"\n"; content+=includeTeardowns(); pageData.setContent(content.toString()); } return page.getHtml(); } private boolean isTestPage() throw Exception{ return pageData.hasAttribute("Test"); }
you are thinking,you will lost in a sea of funtions
a long function is where the classes go to hide
long funtion could always refactor to a class
The old rule that "the function should do one thing, do it well and do it only." But what does onething mean? it could be any thing.
at same level
extract till you can not extract