Defensive programming benefit of the bang method ("!") is not correct.
See original GitHub issueFirst off this is a great document, and thanks for sharing it. I have read the entire file end-to-end as I work on my own personal style guide. However, I was looking at your module example and you assert that “!” protects you from concatenation errors. However, I don’t think this is correct. Consider the following example:
// => SyntaxError: Unexpected token !
!function(){console.log("ham");}()!function(){console.log("cheese")}();
Maybe I am misunderstanding how you expected things to work.
I think you still need to prepend a semicolon to truly protect yourself from concatenation errors.
;!function(){console.log("ham");}()
Issue Analytics
- State:
- Created 11 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Defensive programming: the good, the bad and the ugly
This technique is designed to ensure code correctness and reduce the number of bugs. Pre-conditions are one of the most widely spread forms...
Read more >Defensive programming - Wikipedia
Defensive programming is a form of defensive design intended to develop programs that are capable of detecting potential security abnormalities and make ...
Read more >Defensive Programming - Friend or Foe? - Interrupt
Embedded systems can benefit from defensive and offensive programming with the use of asserts, timeouts, and watchdogs.
Read more >Defensive & offensive programming - Programming Duck
Defensive programming is a term that many programmers have heard of. It's related to error handling and having correct programs.
Read more >In Defense of Defensive Programming - DEV Community
Defensive Programming: The practice of treating all inputs to a program as "unknown" ... To be clear, this approach isn't necessarily wrong.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You would have to prepend a semicolon… If the
cat
command concatenated files together that way. Fortunately, though, it doesn’t:The
cat
command (and *nix-land concatenation in general) concatenates files onto separate lines. That’s why bang modules work: the!
at the start of the line allows Javascript ASI to kick in on the previous line and insert any missing semicolons for you. As proof, try evaluating the following in the console:As you can see, there’s no
TypeError
. The only similarly safe equivalent in the traditional module-land is;(function(){}());
, which is just silly.I can’t speak for file concatenation in Windows environments — all I can say is we don’t use them for deployment at Airbnb, and so we don’t worry about their behavior. My hunch is things work the same way over there too, though.
Thanks for the discussion, this was a good read! 🍻