DEV Community

Discussion on: 5 ways to refactor if/else statements in JS functions

Collapse
 
bjornet profile image
Björn Christensson

Tnx for this list.

Minor feedback in the ✨ guard clauses

guardClauseFun("h") // => undefined with a warning
Enter fullscreen mode Exit fullscreen mode

It will not return undefined since you do not break the function flow, it will continue to the end and return "" (empty string).

Collapse
 
vidhill profile image
David Hill • Edited

yup,
a solution would be to add a return before the console.warn then it would return undefined

if (str.length <= 3) return console.warn("your string should be at least 3 characters long and its length is", str.length) 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sylwiavargas profile image
Sylwia Vargas

Ah! True. I edited that part and didn't edit the examples. Argh I should write unit tests for my blogs 😩 Thank you!

Collapse
 
bjornet profile image
Björn Christensson

That is the way to go! Smart, if all code samples came from actual unit tests eg. Mocha you would also practice the mentality of "placing tests in the first room".

Thread Thread
 
sylwiavargas profile image
Sylwia Vargas

Yeah I might try it in some future blogs. I've always been a fan of TDD but never made space to properly go through all the phases and the process because I felt there was "not enough time". I'm slowly changing this mindset not only because I feel like TDD is really awesome but also because of how the "not enough time" mindset impacts me and my ability to code at times.