Unit 7 lesson 3 Warm Up. Why hating on functions?

Looking at Unit 7 Lesson 3 Warm Up. We are to ask

“What is one way you think programming with parameters and return values may make programming or debugging more challenging?”

I disagree with the assumption that functional programming makes things harder. This should really ask about making things easier. Or at least ask about pros and cons.

With true functions, we can test using the REPL independent of the UI. We don’t need UI problems combined with our calculation bugs. So isolate them and test them independently. This is actually easier.
image
This could lead to testing as part of development.

/*
 * Unit tests for calculate, equationAsString, and isEven
 * 
 */
 console.log(calculate(ADD,1,2) == 3);
 console.log(calculate(MULTIPLY,2,3) == 6);
 console.log(equationAsString(MULTIPLY,2,3) == "2 x 3 = 6");
 console.log(equationAsString(ADD,1,2) == "1 + 2 = 3");
 console.log(!isEven(3));
 console.log(isEven(4));

Dare we even go as far as to say write these first?

In Unit 7 Lesson 7 we introduce the idea of testing our code with console.log kind of like the above. I would move that forward. In Unit 7 Lesson 5 we introduce the idea of functions without side effects in the context of what would be good for a library. Again, I would move that forward.

If we encourage them to isolate calculations (true functions) from functions that work with the screen (side effects). And encourage them to write small functions. We can help them write clean code like this

function updateScreen (operation) {
  var number1 = getNumber("inputNumber1");
  var number2 = getNumber("inputNumber2");
  setText("outputLabel", equationAsString(operation, number1, number2));
  oddOrEvenNumber(calculate(operation, number1, number2));
  resetInputNumbers();
}

Give them the framework to make small functions with one focus instead of large functions with mixed purpose and procedural programming. I think this unit might work smoother than seeming like a big jump from procedural to functional programming. The down side would be a longer instruction but part of that is offset by being ready for libraries.

Hi @jdonwells ,

I thought asking just about the challenges was kinda weird, but not sure if we’re looking at the same thing - but it seems the lesson plan and the slides do ask students to think about both the pros and cons:

IMO there’s not really any challenges, other than the “cons” of almost anything else we learn - that it introduces more complexity into the system and it’s additional stuff we need to learn - … very “ehhhhh” as cons to me.

Not sure if this addresses your concern.

Frank

1 Like

My main concern is the negative tone relative to programming and debugging with functions. Whereas the first question about use is neutral in tone. I feel like it is stacked against students thinking at long last I have found functions and they are a game changer. I am of the belief that parameters and returns reduce complexity, increase testability, increase readability, and will bring you breakfast in bed. Maybe that is just me.

1 Like