0

Consider a code, where I need to find square of sum of x largest elements in an array. ( this is NOT a which data structure question, so please dont post replies recommending heap etc.).

I initially code it up :

OPTION1

singleFunction {
   // code to sort
   // code to sum
   // code to sqaure
   return;
}

Soon, I realize I could leverage helper functions and break them into functions.

OPTION 2

getFinalAnswer() {
   // sort;
   return sumAndSquare();
}

sumAndSqaure() {
   // sum
   return square();
}

square() {
   // return square.
}

Now I realize sort, sum and square can be used as utility methods rather than simply helper methods.

Now I break down functionality into 3 functions (1) sort (2) sum x (3) square()

OPTION3

someFunction(int[] arr, int x) {
    sort(arr);   
    b = sumOfLastXElements(arr, x);
    c = sqaure(b);
    return c;
}

Now questions:

  1. Looks like option 3 is the best of the lot, still so many times we find a function calling another. What is an advantage of option 2 over option 3 ?

  2. A method by definition is supposed to do a single task/responsibility. but somefunction is doing 3 different things. What are such functions called ?

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

2 Answers2

1

First, being strict, I must say that Java has no functions, only methods due to its OO nature.

1) Looks like option 3 is the best of the lot, still so many times we find a function calling another. What is an advantage of option 2 over option 3 ?

As you said, sort, sum and square methods hold a single responsibility each one, so there's no need to have a single monster method that do al the three. Also, each one can be reused later in other methods.

Option 2 has a sumAndSquare method that may or may not be reusable. This will heavily depends on your needs. The fact that you need this method or not will be noted if you have lots of these along your code (and by lots, I mean is at least 10 times in different methods):

long theSum = sum(array);
long theSquare = square(theSum);

2) A method by definition is supposed to do a single task/responsibility. but somefunction is doing 3 different things. What are such functions called?

It's task or responsibility is:

  • sort a list of numbers (I guess?)
  • sum the largets numbers
  • apply the square of the sum

So, the method is doing its task as expected. IMO you can even split the sumOfLastXElements into two methods: int[] findLastXElements(array) and long sum(array).

To answer this: What are such functions called? there's no specific nor special name, they are just methods. But the process to go from option 1 to option 3 is called Code Refactoring

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Answer to your first question:

Number two has more overhead, since each function/method has to be instantiated. Option 2 has more flexibility, and if you're going to use those individual pieces all the time, might be worth your time. However, if they are only used in code together, consider grouping them together.

As a wise professor of mine once said, separate what changes from what stays the same. If they are only used together, no need to have individual methods/functions.

Answer to your second question:
Difference between a method and a function

(don't get too hung up on terminology, IMHO.)

Hope this helps.

Community
  • 1
  • 1
Plasmarob
  • 1,321
  • 12
  • 20