question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defensive programming benefit of the bang method ("!") is not correct.

See original GitHub issue

First 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:closed
  • Created 11 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
reissbakercommented, Feb 4, 2013

You would have to prepend a semicolon… If the cat command concatenated files together that way. Fortunately, though, it doesn’t:

$ cat ham.js cheese.js
> !function(){console.log("ham");}()
> !function(){console.log("cheese");}();

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:

!function(){console.log("ham");}() // Missing semicolon!
!function(){console.log("cheese");}();

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.

4reactions
hshoffcommented, Feb 4, 2013

Thanks for the discussion, this was a good read! 🍻

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found