1
rehman
8y

what's the best practice to divide long task into functions? suppose I have function of 200+ lines then from the code readability point doing short functions would be better?

Comments
  • 2
    You might want to read about Clean Code - it contains a lot of useful methods to deal with that.

    In your case, try to find blocks of code in that monster function that do only one thing and move them in a different function or in a separate class - if you have related stuff.
  • 2
    It's simple, really.
    Piece of code used twice in the same class?
    -make a function.

    Piece of code used in 3 or more classes?
    -make a static class with static functions.

    Piece of code used in more classes and there's no way to know for sure if values are being given to certain variables?
    -make a singleton class that includes functions and variables.

    Cannot read code?
    -make regions (c#).

    I simply use something like that to make it through.
  • 1
    Single responsibility rule. Chances are your function is doing a gazillion things now. extract functions so hopefully they only do one thing or at most two.
    https://sourcemaking.com/refactorin...
Add Comment