java - Is it Good to make so many methods for even small task also? -


i want know many developers same thing decrease size of long method , create many small sized methods same task. want know affects performance of application or not?

functions should short, between 5-15 lines personal "rule of thumb" when coding in java or c#. size several reasons:

  • it fits on screen without scrolling
  • it's conceptual size can hold in head
  • it's meaningful enough require function in own right (as standalone, meaningful chunk of logic)
  • a function smaller 5 lines hint perhaps breaking code (which makes harder read / understand if need navigate between functions). either or your're forgetting special cases / error handling!

but don't think helpful set absolute rule, there valid exceptions / reasons diverge rule:

  • a one-line accessor function performs type cast acceptable in situations.
  • there short useful functions (e.g. swap mentioned user unknown) need less 5 lines. not big deal, few 3 line functions don't harm code base.
  • a 100-line function single large switch statement might acceptable if extremely clear being done. code can conceptually simple if requires lot of lines describe different cases. suggested should refactored separate classes , implemented using inheritance / polymorphism imho taking oop far - i'd rather have 1 big 40-way switch statement 40 new classes deal with.
  • a complex function might have lot of state variables messy if passed between different functions parameters. in case reasonably make argument code simpler , easier follow if keep in single large function (although mark rightly points out candidate turning class encapsulate both logic , state)
  • sometimes smaller or larger functions have performance advantages (perhaps because of inlining or jit reasons frank mentions). highly implementation dependent, can make difference - make sure benchmark!

so basically, use common sense, stick small function sizes in instances don't dogmatic if have genuinely reason make unusually big function.

taken here.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -