5

Is it OK to have functions that have single statements?

I usually make functions that have single statements. I believe these increases code readability and i am able to write code faster as they make code more like natural language. These functions are often utility functions I call at more than 2 places, at least.

RegExp.escape = function(text) {
 return text.replace(/[-[\]{}()*+?.,\\^$|\s]/g, "\\$&");
};
function makeSelector(text) {
 return '#' + RegExp.escape(text);
}
asked Nov 29, 2016 at 14:52
7
  • 11
    I believe these increases code readability and i am able to write code faster as they make code more like natural language -- That's not sufficient reasons? Commented Nov 29, 2016 at 14:57
  • Possible duplicate of One-line functions that are called only once Commented Nov 29, 2016 at 15:01
  • @gnat Maybe not. These function are often utility functions I call at many places in my code. Otherwise I do not make them. Commented Nov 29, 2016 at 15:17
  • 2
    doesn't matter. These are considered OK even when used only once, and even more so when reused Commented Nov 29, 2016 at 15:33
  • my fave softwareengineering.stackexchange.com/questions/321679 Commented Nov 29, 2016 at 15:40

3 Answers 3

2

In order for refactoring to a function to be useful,

  1. The function should encapsulate some significant bit of reusable functionality
  2. The name of the function should meaningfully embody that functionality.

Your specific example seems worthwhile, provided the first function is used in more than one place. If it isn't, you might be better off simply including it in the second function, as you would simplify the code by having fewer functions to worry about.

answered Nov 29, 2016 at 15:01
2
  • Actually some time ago i just had the first function and because i have used it at many places in my code, i have not deleted it and today i had this crazy idea of making second function for even more convenience otherwise i would have merged these two functions and still use a single return statement with concatenation. Commented Nov 29, 2016 at 15:07
  • 1
    Works for me. .. Commented Nov 29, 2016 at 15:09
0

It's a controversial topic, but as my accepted answer to a recent question, My boss asks me to stop writing small functions and do everything in the same loop, shows, the majority of people appear to agree that single statement functions help improve readability.

The cons to this approach are that if the function is not well named, you have to jump around the code, reading what it does and this can slow down the developer.

One line of code is easy to reason and thus such functions tend to be highly readable, especially if the function is well named. So on balance, it is better to write short, well named functions, than long functions, regardless of how the latter are named.

answered Nov 29, 2016 at 14:55
0
0

Aside from the obvious readability improvements (assuming well named/scoped functions of course), this also has the strong advantage of providing an abstraction point for the functionality.

Let's pretend you have 30 places in the code where want to use this functionality, and you do not put a function wrapper on it. Then, if that functionality needs to change, you need to find and change that in 30 places, which is very error prone (especially in JS, where refactoring tools are immature at best).

If that were in a function, you just change the contents of the function (one point code change) and the rest is isolated.

Perhaps your specific code in the question isn't the best example of how important this can be, but it's a very strong concept in general, IMNSHO. I don't use the word 'always' much, but I'd pretty close to always prefer a function wrapper for that abstraction point alone regardless of the added naming/organizational benefits.

Actually, your function is a decent example of my point. If you ever (for some reason which I can't fathom at the moment, but that's besides the point) wanted to change how you escape the string or get away from regex, you would have to only change that function body, not find every place you use RegEx.escape

Robert Harvey
201k55 gold badges470 silver badges682 bronze badges
answered Nov 29, 2016 at 15:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.