4 ms·
Is it "always" correct to extend the String prototype? No. Is it "always" correct to NOT extend the String prototype? No. Like all things it depends. If you a
by rpedela 13y ago
Is it "always" correct to extend the String prototype? No.
Is it "always" correct to NOT extend the String prototype? No.
Like all things it depends. If you are writing a library for others to use then you shouldn't be extending globals for the reasons you have given. And if you do, you need a good reason and make it very clear to the user. If you are writing an application or a closed-source library, then it is usually safe to extend globals because you are in complete control. Like others have said in the String case, it makes its a lot easier to read the functions that do string manipulation.
The only absolute rule in software engineering is that there are no absolute rules.
- onestone 13y agoExtending a core language prototype is never necessary. And you never know which library will break when you do this (unless you review carefully the entire code of all modules you use). So it is a bad practice, despite sometimes being ok. I admit that extending the String prototype is less dangerous than e.g. the Object or Array prototype.
- secoif 13y agoAgreed, but it'd be different if this was aimed at seasoned developers, but it's aimed at beginners, as such, it's irresponsible.