I do a lot of programming in my class. It's just my first semester, and most of my stuff is review. I've taken to using a verifyInput function to ensure that user inputs match certain criteria. Since I end up using verifyInput so much, I keep coming up with more ways to enhance and expand it. At the moment, my code looks something like this:

function verifyInput(foo, match, response = ": is not a valid input.") {
    const input = foo()
    if (!isNaN(input) && !input || input === null || input === undefined) {
        if (input === null) {
            return null;
        else {
            alert("Not valid.");
        return verifyInput(foo, match, response);
    else {
        if (match && !match(input)) {
            alert("(" + input + ")" + response);
            return verifyInput(foo, match, response);
        else {
            return input;

userInput = verifyInput(() => prompt("Input?"), (x) => x > 10 ? true : false)

Works like a charm. Frequently I think up new things to add to it. Recently I was considering adding an additional parameter to modify the output string. Maybe I want the user to be able to input numbers as spelt strings instead of regular values. A user could input "seven" and it would validate and return 7.

After some thought I realized that I could use my match function as both a validator and an output modifier. However I do this sort of thing a lot when working on old and new functions. I was curious what sort of guidelines and techniques others use to manage their functions and parameters. Also, how do you organize them?

1 Answer 1


I do a lot of functional programming in my class.

What do you mean by this? Functional programming is extremely vague. Please describe what you think it means. You're using JavaScript, which does not have fantastic support for the functional style. So you are already a bit limited by the language you are using.

OK, to get to the crux of the issue: you're not defining what you want your functions to do. You're basically just putting all the logic you can think of into a single function. This is bad design.

A user could input "seven" and it would validate and return 7.

This is not validating input, it is transforming it. Do not do this in a function called verifyInput.

verifyInput is already extremely vague; obviously, there is no single function that validates all input in all programs. That's why no standard library would have a verifyInput function in its standard library. That function only makes sense in a specific context: what input are you verifying? Once you figure this out, you'll know how to design your method. A

I was curious what sort of guidelines and techniques others use to manage their functions and parameters. Also, how do you organize them?

The unit of organization is a feature of the language you're using. In Java it's classes; in OCaml it's modules. Up until recently, JavaScript had no code organization support, but ES6 finally added it in the form of modules.

  • Actually, ECMAScript has very powerful code organization support with functions. Since functions can nest, you can group functions in functions. That's all you need, as is evidenced by the fact that both ES2015 classes and modules are just simple syntactic sugar over functions. Never underestimate the power of nesting, see Beta's nested classes, which are used to perfection in Newspeak, nested functions in Scheme (which are used to implement pretty much everything from modules to objects to classes), etc. If Java had proper nesting, we probably wouldn't need packages, modules, OSGI bundles etc Oct 18, 2016 at 1:37
  • Functional Programming as in the paradigm. For some reason I thought it was necessary background. In hindsight it doesn't fulfill any useful purpose and is more confusing than anything. I edited out. I agree, my function is poorly named. I think my biggest issue is that I fail to define the purpose of my function and thus I start expanding them before I even finish them.
    – Ucenna
    Oct 18, 2016 at 1:37
  • Oh, and +1 for all the rest. Oct 18, 2016 at 1:37
  • 1
    @JörgWMittag Sorry, I have to disagree. Using IIFEs are a horrible way to modularize your code. ECMAScript has no support for modularity. That's why people had to resort to nested function definitions as a crutch. The same could be done with any language that supports nested function definitions, and nested function definitions are so basic and essential that any language that doesn't support them has already failed. Java should not be used as the benchmark of modular language design. Full-blown module support (ala OCaml, or heck even Python) is much much better.
    – gardenhead
    Oct 18, 2016 at 1:46
  • @Ucenna Yes, I know you were talking about the paradigm. I'm saying that the paradigm is ill-defined. For example, what makes the code you posted "functional"?
    – gardenhead
    Oct 18, 2016 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.