How do i know when to use a pure function in javascript?
#1
I'm trying to understand when to use a pure function in my JavaScript project. I have a helper that formats a date string, and it feels like it should be pure, but I'm not sure if that's overkill or the correct approach for something so simple.
Reply
#2
I wrote a tiny date formatter as a pure function and it changed how I test it. Given an input string it will always produce the same output and it has no side effects. It felt worth it for confidence, even if it seems overkill for something so small.
Reply
#3
I tried keeping it pure, but locale quirks showed up. Sometimes the same code behaved differently in test env versus production unless I passed in locale explicitly to keep it pure.
Reply
#4
I ended up not making it pure because the app needed to format based on user settings at runtime. I did add a locale param and that made it easier to unit test.
Reply
#5
I once drifted into thinking about time zones and parsing while debugging a date bug; the tiny helper felt like the straw that broke the back, but maybe the real issue was something else. Do you think the real problem is test coverage instead of purity?
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: