For example, if there's multiple lines to be drawn, I could do

function drawLines(lines) {
    lines.forEach(function (line) {
function drawLine(line) {

and then in the "main" section of the code I would merely have to call drawLines(lines) rather than writing a forEach there.

This does prevent the need for multiple forEach's to be written if there's more than one section of code that needs to render some lines. I'd only need to call drawLines(lines) in any such place. But at the same time, it just feels like a bit of a silly thing to do.

(The lines thing is just an example... I'm just talking about the general concept of "do something with each item in an array").

  • It clearly is unnecessary, as you could put all the code in one method. But that would result in a difficult to read mess. As such it's a bit unclear what you are asking. Do you really want to know whether it's necessary, or whether it's good practice, to organise code into small, easy-to-read parts?
    – David Arno
    Sep 8, 2017 at 11:12
  • 1
    Possible duplicate of Should I extract specific functionality into a function and why?
    – gnat
    Sep 8, 2017 at 11:19
  • 2
    Depending on language mechanics, you could always split the difference and do lines.forEach(drawLine);
    – cbojar
    Sep 8, 2017 at 11:23

2 Answers 2


This is actually a good question, and somewhat subtle. I assume your style is object oriented? If you are using functional or imperative, the answer would be different.

An array have no further semantics than being a collection of items, so clients should treat it as that, i.e. use collection operations like forEach.

But the question is if the lines should be passed around as an array in the first place. Since you are tempted to write a method drawLines() which treat the array of lines as a whole, it indicates you should consider encapsulating the array of lines in a distinct Figure object. You would have a Figure.draw() method, which internally would call drawLine() (or preferably line.draw()) on each line.

  • I'd tend to agree with you about using a Figure class but only if the behavior of drawing several lines is somehow different or could potentially be different than drawing them one at a time. Of course it could happen to be the same initially, but it makes for more elastic code. If drawLines only calls drawLine for each line with no intention of extending behavior, it should not be done in my humble opinion.
    – Neil
    Sep 8, 2017 at 13:36
  • @Neil: I disagree - I think the decision should be based on whether the client needs to know if drawLines just calls draw on each individual line or does something more. If the client don't need to know, it should be encapsulate.
    – JacquesB
    Sep 8, 2017 at 13:45
  • The OP's question is implicitly framed in terms of DRY, but separation of concerns, namely interface vs. implementation may have more bearing here. A look at the larger context (not shown by the OP) might suggest an improvement in the abstraction(s) being offered.
    – Erik Eidt
    Sep 8, 2017 at 15:34
  • @JacquesB I wouldn't mind if the client/caller would only need drawLines, but if you offer the client/caller a choice between calling drawLine repeatedly and one call to drawLines, the natural conclusion is that there must be a difference, even if only at the level of optimization. If there is no difference and no intentions of having a difference in the near future, it is only misleading and verbose in my humble opinion.
    – Neil
    Sep 11, 2017 at 7:19

If it's more than a line of two lines I would do it that way. Drawing a single line is easy but drawing in general can involve a bunch of steps and can be confusing for the next person that comes along, for that reason I would generally put drawing logic in a method like this. It kind of has to do with theme and making into bite sized chunks. Drawing uses different concepts to loops and data structures and separating drawing means the person looking at it only needs to think about drawing and then they are looking at the loop they only need to understand that drawing gets done.

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.