Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just read a very compelling blog post from Scott Hanselman where he quoted colleague Erik Meijer as follows:

JavaScript is an assembly language. The JavaScript + HTML generate is like a .NET assembly. The browser can execute it, but no human should really care what’s there. - Erik Meijer

He goes on to give examples of how some of today's biggest sites (the one he shows is from Google+, but cites also Bing, Facebook, etc.) are producing sites where when you do a "View Source", you will see a lot of tight "minified javascript". Essentially, unreadable on the surface.

Simply put, you cannot go into a site and look at their source and see a pretty formatted version of the markup. My question to you is similar to what Hanselman asks: is that important to you? If it works, is that good enough? In my opinion, YES - results are what matters - Google is an example of that. But I must admit, as a developer, it is nice to have the ability to, once in a while, look at the source and get ideas and examples from sites I admire.

Your thoughts?...

share|improve this question
1  
Is there a reason you can't copy+paste the source code to an IDE that will then pretty-format it for you? OR... have that IDE/editor your default source viewer (some browsers allow an external editor to be the default viewer). –  FrustratedWithFormsDesigner Jul 7 '11 at 15:28
    
@FrustratedWithFormsDesigner its not just a matter of formatting the code. The way the code is often structured it is extremely difficult to understand what it is doing; and often the content of what you are seeing is really only in memory as a result of user and asynchronous operations done after the page loads. The best you can do is look at a DOM explorer to at least understand logically what is being represented. –  Jeremy Jul 7 '11 at 15:36
    
If enough users want a properly formatted version of the markup and/or the JavaScript, the obvious solution is to add a browser feature that automatically formats it, no matter what unreadable mess it’s in when transmitted from the server. –  Timwi Dec 14 '11 at 15:24
    
In fact IE and Chrome (and IIRC Firefox too, but I don't have it installed atm) will give you a prettyfied, browsable version of the source anyway with the developer tools. Minification is not obfuscation. What he means is that it's not intended for human consumption - a human can still read it, but that's not what it's there for –  JohnL Jul 16 '12 at 21:36
add comment

5 Answers 5

up vote 13 down vote accepted

Well the reason for minification is not obfuscation but rather to reduce bandwidth consumption.

As far as source code it is important to be readable so that it can be maintained. As far as what is output to the client, functionality is more important. I do not care if they can read my view source, if they can use my site and it appears like it is supposed to then my objective has been achieved. But it is important that someone can come after me and update my source with out having to call me and get me to explain what I did and why. So I try to keep this simple and well ordered.

The code I want to hide is done server side. I do not go out of my way to obscure my JavaScript or View Source. I also do not try to make it pretty for them since the rendered result is my goal.

-- And JavaScript is not assembly language. I can not think of any analogy that would make that even possible. That someone respected would try to make an argument that it is causes me to lose respect for them. JavaScript is an interpreted language that requires an interpreter and an engine to send it to the hardware. Assembly is a low level language that is executed natively by the hardware.

share|improve this answer
6  
But it is an analogy. Javascript obviously isn't an assembly language in the literal sense, but it is used as the fundamental language of browser applications, and many other languages compile to it. In that regard it is used as an assembly language, if you accept the browser as a virtual machine of sorts. –  Jon Purdy Jul 7 '11 at 16:04
    
I would agree if javascript actually did anything. The browser takes the commands and interprets them and calls methods and properties of the DOM. Assembly goes direct to memory locations for data does calculations and writes to memory locations. There is no DOM (or anything like it) to leverage in Assembly. If he wanted to relate it to basic I could agree. Even cobal or fortran. –  Chad Jul 7 '11 at 16:26
    
I also dissagree that no one should know how to do it. Thats kind of like saying no one should know how to make fire with out matches because we have lighters. But he is entitled to his opinion. Understanding how to read it and what it is doing can help you figure out why you are having flakey behavior in applications some times. Granted I have not gone to assembly level in 15 years I could (With some refrence to get reaquainted). –  Chad Jul 7 '11 at 16:29
2  
You're missing the point. It's not about the literal nature of an assembly language versus a high-level language. It's about the function of the language in context. He's saying JS : browser :: asm : system. –  Jon Purdy Jul 7 '11 at 16:33
    
I get his point. I just think it is as horrible of an analogy as a college professor that said that Access was not a database. Access is not a good database but it is a database. I got his point but it leads those that do not understand to incorrect beliefs and assumpions. –  Chad Jul 7 '11 at 16:41
show 2 more comments

In my opinion:

  • If user wants to know what is in code, they can read it/format it in jsfiddle for example
  • Big sites do minify their codes to minify traffic!
  • You don't have to minify your js code everytime you want to add new content
  • I see no reason why you should minify your code if you do not mind about traffic/load speed
share|improve this answer
add comment

As use of JavaScript increases, and the size of scripts increase, compression is necessary. Any significant amount of JavaScript should be minified (or compressed, or whatever term or technique is used).

That said, viewing JavaScript source is often an important part of the learning process. I know it was for me when I first started. The minification of JavaScript, obviously, prevents this (in most cases).

However, as it is usually large amounts of JavaScript that are minified, the effect is reduced. This is because large, complex scripts will be harder to read even when uncompressed, and thus will not be useful to beginners anyway.

As a fairly advanced JS developer, I have found that JS Beautifier is often sufficient to clean up source code so it is more understandable. This is useful for figuring out how scripts work, even if the variable names remain obfuscated down to one or two letters. Chrome, I've recently discovered, has a similar tool built in - simply right-click on the source viewer, and choose to un-obfuscate.

TLDR: Reading scripts can be useful for all levels of coder, and the scripts that are more complex (and thus more useful to advanced coders, rather than beginners) are the ones that will be minified, so there is usually little problem.

share|improve this answer
add comment

There are solutions:

  1. Chrome supports prettifying the javascript sources in the inspector. I'm sure the other browsers have similar abilities. http://cristian-radulescu.ro/article/pretty-print-javascript-with-google-chrome.html

  2. Chrome also supports "source maps". These will eventually ensure that in the debugger you can view the original sources, while what your browser is executing is cross-compiled and minified. http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/

So I think it's a temporary problem that will get solved when IDE's and minifiers automatically start generating source maps in all cases. Then it will be a conscious decision of the developer to not upload the source map. View source will be the default again.

share|improve this answer
add comment

View source is useful for only two things.

  1. Letting a developer debug certain issues.
  2. Allowing the holier-than-thou types to pass judgement on a random site.

The ability to view a page source is really just a relic from an earlier time, it costs essentially nothing for browsers to support it and would probably be more work to delete it, so it stays.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.