DEV Community

Discussion on: How would you refactor this JS function?

Collapse
auroratide profile image
Timothy Foster • Edited on

The first thing I would do is change the name, perhaps to parseLineToHtml, which is more in line with what it does.

The second thing unfortunately depends on context, but the isFirstLine parameter bothers me. Probably because this function is very close to being able to operate on single lines independent of additional context, if not for that parameter.

The third thing is probably shuffle the br condition to be first so we don't have to repeatedly ask if the line isn't empty. Personally, I don't mind small, non-nested if-ladders as they can sometimes be more readable than ternaries.

And finally, I wouldn't do any of that without tests ( :