:syntax Off
kyleisom.net
kyleisom.net
Clean code is all about structure. Anything that help show structure, symmetry, hierarchy, or other patterns is bringing your reader one step closer to understanding. The difference between a keyword and an identifier is part of the program's structure. Coloring it makes it visual.
Also, properly-implemented syntax highlighting is essentially a very short feedback loop with the compiler that shows you how your code is actually being parsed. Short feedback loops make creativity easier (this is the basis of a lot of Bret Victor's work).
I agree with the author that "paying attention to what the code is doing instead of the individual elements" is important, but I don't think you have to turn off syntax highlighting to achieve this. At least I don't personally. If the colors are distracting, one idea might be to make them more muted so that they don't demand as much attention. But to me there's no going back from being able to see visually that you forgot to close a string or parenthesis, or include a proper line continuation for your macro.
Also, it bugs me when people try to compare code and prose. Just because they're both typed as text using keys on a keyboard doesn't mean they're in any way related. Calling them analogous is like trying to compare building a house and buying a car. Yeah, they're both life investments, but that doesn't mean you use any of the same strategies between them. Money aside, they're unrelated.
Not quite. As I'm sure you know, written languages have evolved over time in order to meet the needs of readers and writers alike. That includes capital/lowercase, roman style (i.e. non-script) lettering, spaces, leading and paragraph indentation (thanks to printers), etc. It isn't helpful to blindly latch on to seemingly cute analogies, but in this case I think it's a pretty fair comparison. I imagine others who don't use highlighting all the time would agree that it's a happy or at least comfortable middle ground. I'm a little sleepy-eyed now and shouldn't still be typing, so sorry if my tone reflects negatively. I think the response below (corwinstephen) brings up the same thing, except if you like highlighting then your middle ground leans further towards an explicit, notated style.
I think this are poor man's syntax hi-lighting. Or at least physical world implementation of syntax hi-lighting. After all, you don't want to carry a lots of colors pen in order to write.
But if computer can do it for us, why not?
Code is closer to poetry than prose. Line breaks and stanzas are very important, there.
I actually think this would be a great idea and it would probably prevent me from having to reread paragraphs every now and then because I thought the wrong person was talking.
However, I can see how turning off syntax highlighting could help one learn to keep more code in their head at once, so they don't have to jump around as much, but to me, it's not worth it.
I opened vim and cycled through syntax on, syntax off a few times, and thought about what I think about when I see it. After this short experiment, I think that most of what I use syntax highlighting for is just to add texture to the code. The pattern of indentations and syntax highlighting gives the code shape.
So when I scroll, or scan the code, I've got another a set of visual anchors in addition to the structure given by indentation. It makes me feel more comfortable. Past that, for me, the syntax highlighting is just noise.
Makes me wonder how to increase the feeling of visual structure in the code without introducing visual noise...
Examples I just thought up:
* Variable-scoping-level highlighting—variables in the same scope highlighted the same color.
* "String only"—highlight only strings and literals. Check for language bugs.
* grep-only highlighting—define a set of things to grep for quickly and highlight them.
* block-focused—highlight code blocks so you get an idea of the overall structure of your program.
* No-highlighting: if you really want a moment of clarity on your code. There is absolutely no reason to give up the information highlighting provides 100% of the time, but I could see it being useful sometimes when you need to focus.
And countless more. If I could switch with a quick keyboard shortcut to a highlighting scheme appropriate for any given programming task, it would be phenomenal. I could see it being useful for all kinds of things, and helping to show more information from the compiler than syntax highlighting does already, in a better way. It would be instant visual information, and there's no reason to limit it to syntax like we do currently.
Hmm. Sublime Text Plugin perhaps?
I played with this idea in the syntax highlighting of the code samples in this article: http://david.rothlis.net/c/compilation_model/#chapter005
but would you want all variables at the same scope in the same colour? Different colours for each variable, with the darkness/lightness indicating the scope?
> grep-only highlighting—define a set of things to grep for quickly and highlight them.
Emacs has this with the "highlight-regexp" command. To be honest I don't use it that much. What I do like is highlighting all matches whenever you're doing a "find in file" (though unlike highlight-regexp, the highlighting disappears as soon as you exit the search). I probably use this more because I use search all the time (who doesn't), whereas I tend to forget that "highlight-regexp" even exists.
> block-focused—highlight code blocks so you get an idea of the overall structure of your program.
A few examples of how this might look: http://web.archive.org/web/20080723220126/http://lemonodor.c...
> No-highlighting
In Emacs the "font-lock-mode" command will toggle all highlighting; and you can specify the "level" of highlighting you want by setting the "font-lock-maximum-decoration" variable (you could set up a specific key to toggle this variable). I think these levels were originally intended for performance reasons, so they're cumulative (enabling level 3 also brings in all the highlighting from levels 1 & 2) rather than individually-toggleable features.
I only bring up Emacs because that's what I'm familiar with, and because I'd like more people to experiment with these types of things in Emacs. But perhaps Sublime Text would be a better platform for such experiments, for some people; I don't know.
As the author already mentioned, strings are really annoying to work with when you don't have syntax highlighting for them. I would group comments in the same category. It's difficult to see where they begin and end without the visual cues provided by syntax highlighting. That can quickly become a disaster.
I did try out going through code just now without syntax highlighting. New and short programs weren't bad at all, but going through files with hundreds of lines quickly became a challenge. This might work for the author, but it's not for me (nor, I suspect, for most people). Colors and font styling make it easy to get a grasp of what's going on. Taking that away does reduce noise, but cuts down far more on signal.
Windows 7, Chrome 24.0.1312.2 dev-m
The font used: http://www.google.com/webfonts/specimen/IM+Fell+English
Why would anyone try and use bad typewriter font imitation on a website is beyond me.
I get just as awful a rendering on Firefox ESR 10.0.5 on Win7-64.
Edit: Perhaps not. The font looks better on the link provided in the sibling comment.
...not everybody is using safari on a mac (and I eagerly await the days when people will start using fonts that are only readable on safari on a mac with retina display...)
The author makes the argument that removing highlighting forces you to be more careful with the code you write. Removing three of my fingers would also force me to be more careful, but I'll take everything I can get to make me more productive.
I'm sure it's possible to get used to, but I doubt I'm the only one that remembers when they first turned syntax highlighting on, and the difference it made to my productivity then.
I might even try it again one of these days.
"Number of Records: $numrec\n"
"Number of Records: \$numrec\n"
"Number of Records: ${\ get_number_of_records() }\n"
"Number of Records: \${\ get_number_of_records() }\n"
Or my $string = <<" HEREDOC";
my $name = "Thompson";
HEREDOC
It's much less important in something like C where you would just being using something like sprintf().I think the sigils alone are sufficient to flag variables in strings...but when things get complicated and span multiple lines, it can become ugly and difficult to work with. But, I'm not convinced this is the most useful function of highlighting in perl (again, because the sigils already provide visual clues)...it think it's more useful in pointing out unclosed parens, brackets, quotes, etc., and letting you know your syntax is wonky.
That said, it may be laziness on my part. I find it soothing to have highlighting enabled, and I get nervous when it's not, as I'm never quite sure I've got the syntax right until I check it.
Personally, I like limited syntax highlighting: I only highlight strings, comments and sometimes keywords (in Common Lisp). It lets me quickly separate the code in useful chunks and also highlights a common source of errors that I make (made): forgetting to close strings.
Here's a screenshot: http://imgur.com/rx053 (My Vim color scheme isn't quite there yet: the colors are too bright.)
[1] http://www.catb.org/jargon/html/A/angry-fruit-salad.html
My main exception is comments, or footnotes on the side. At a quick glance it's very helpful to skim through a file and see what's documented or, if it's a text/config where each section break is.
Edit: after skimming the other comments here it seems that the preference for highlighting is more to do with error checking than anything (pairing enclosures, etc.). If that's the main concern then why not zap the rest and use syntax the same way you'd use spell? I don't use highlighting for much but I might just drop that into my vimrc tomorrow to play around with it. I would rather see glaring red blocks where there are mistakes than a minor variation in my digital bowl of lucky charms.
I've actually gone the opposite way recently. In the past, I'd only have code highlighting. Different parts of the syntax would be rendered as different colors--nothing surprising.
However, recently I've been writing a lot of Haskell and OCaml. At least in Emacs, both Haskell and OCaml have an additional option to syntax highlighting with color: your code can also be highlighted with symbols. That is, parts of the syntax are not only displayed in a different color but actually displayed as a Unicode character rather than ASCII. As a moderately contrived example, something like
\ a b c -> a >= b && b <= c || a /= c
would get displayed as: λ a b c → a ≥ b ∧ b ≤ c ∨ a ≢ c
Now, real code doesn't usually have symbols quite as dense; this example just shows a whole bunch of them at the same time.I find the second snippet far easier to read than the first, especially if I'm just scanning over the code.
This is also a very good reason for using a typeface like Deja Vu Sans Mono: it can support all the symbols I could ever want. It's also very readable.
I personally find this, combined with syntax highlighting, extremely useful for reading code. Actually, thinking about this, I've found syntax highlighting even more useful for Haskell and OCaml than other languages, probably because they have very minimalist syntax. I really like the syntax, but the color does make the overall structure of any given function much more evident, which is nice.
Coincidentally, for the same readability reasons as above, I'm significantly warming up to Unicode identifiers. I think something like:
set1 `union` set2
is not nearly as readable as set1 ∪ set2
And, as a bonus, the second version is easier to type: set1 \cup set2
Additionally, using Unicode identifiers and operators matches the way I write pseudocode. With the right identifiers, my Haskell code actually looks very close to what I would write on a whiteboard to explain some concept. That's one of the reasons I really like Haskell's syntax.As an aside, the expression
set1 `union` set2
is a perfect case study of why I find syntax highlighting useful. First of all, I think it's strictly better than the prefix version: union set1 set2
I've found being able to use arbitrary functions in an infix position can sometimes make certain expressions much more readable and natural.However, if you're just scanning over the code, it might be easy to miss the backticks. Happily, in Emacs, operators get highlighted in a different color from normal identifiers. This includes things in backticks. So the `union` gets highlighted in a different color, making it obvious it's being used in an infix position.
Having said that, I still prefer <- and -> to ← and →, at least on screen: those Unicode arrowheads are too small on all the fixed-width fonts I've ever seen.
set conceallevel=2
syntax keyword lispFunc lambda conceal cchar=λCan't wait for unicode identifiers and this kind of expression composition in general purpose languages like objc.
> set1 ∪ set2
Looks good until someone confuses it with:
> set1 u set2
Having two characters that are similar enough to be confusing is bound to cause problems.
And not just Haskell and OCaml: See for example https://github.com/drothlis/pretty-symbols
(though of course Haskell and OCaml might be much more suited to this kind of visualisation -- in python, say, you just get λ ≠ ≥ ≤).
Lots of examples here
http://stephane.ducasse.free.fr/Teaching/CoursAnnecy/0506-Ma...
What I find is that I don't really rely on syntax highlighting for find my way around the code or understanding it. I rely on it to tell me when I didn't properly close a string literal, or a comment, or fat-fingered a keyword. So, I generally enable it when I can, and find that it makes me more productive.
In the end, what the logicians of the XXth century taught us is that logic (read maths/cs/...) is just syntax (from the formal point of view, mind you).
However, I certainly feel when I am reading someone elses' code, it is not a crutch but a tool. The highlighting helps me see an unfamiliar program's structure just a little more easily, which is nice when you're absorbing a couple hundred lines with sparse comments.
Book comparison is far-fetched imo. Code is not just a regular text, it's a very structured text, and reader of a structured text benefits from structure hints (expressed via color and identification), while writer of such a text benefits even more from on-the-go error checking. Looking through a dictionary without any bolds, italics and structure, just paragraphs and paragraphs of text, would be a waste of time and a pain to the eyes. And this is what I tend to do about 95% of time when 'writing' code - looking at it.
I would recommend searching for a less contrast or vibrant colorschemes like popular Solarized (too low contrast for me actually) or Tomorrow themes.
Another example, TextMate highlights some syntax errors much more than it highlights syntax- type "else if" in a Ruby file and it's bright red. How is that not useful?
My love for syntax highlighting probably goes hand in hand with my love for natural language operators. "if foo and bar then qux" is much harder to parse without colours than "if (foo && bar) qux". Does anyone use a monochrome editor and natural language wherever possible?
Instant feedback for error checking aside (which is incredibly useful) I like the "texture" syntax highlighting adds to the code.
Indentation is great for organizing code into individual chunks, but if it's all the same color, I find that it still starts to feel a little overwhelming when the code starts getting long. Colors help create a visual flow and, at the very least, break up the monotony, thus prevent the "wall of text" effect.... which is fine for a book, but source code isn't a book. It doesn't function like a book, you don't interact with it like you do a book, so it really shouldn't look like a book.
Plus, it always seems that whether I have light-on-dark or dark-on-light terminals set up, one of the colors is so off that e.g. strings become essentially invisible (dark blue text on black background) and it's easier to just shut off highlighting than to fix the highlighting.
The complex houses married and single soldiers and their families.
vs. The Complex houses married and single Soldiers and their Families.
Other languages use different markers to indicate different parts of speech.A point that could be made is that (written) language evolved for a long time and the concept of "syntax highlighting" is embedded in at least some of them. Probably because it has proven useful.
Now they're shifting to proofreading prior to clicking ``reply''.
I'm used to coding in terminal screen sessions, and using an IDE is a bit too shiny for my liking so I created my own terminal for XCode :) Now when I look at my co-worker's color-coded ruby code in Sublime Text it looks like a bunch of unreadable rainbows.
Colors are pretty, but since I turned them off I found that I haven't missed them. They turned out to be distracting, and I can't remember a situation where the colors helped me scan the code faster to help me find something. One color means I now treat all tokens equally.
At a primitive level it allows, for example, coloring nams of variables either by type, or by source (local/field/static field), same with function names (ie, is it a function, method, a static method, etc). See for example: http://doc.qt.digia.com/qtcreator/creator-highlighting.html.
But I'm sure much more is possible. Semantic highlighting could help understand the (non-local) semantic structure of the code, and allow finding mismatches between what the user intends and what he has specified.
The only time I :syntax on is if I get a parse error and can't find the culprit quickly, and it's probably a problem with string being constructed incorrectly (I mostly work with PHP so something like 'and that's that';). In that case having the syntax highlighter to tell me where the string interpolation goes awry is very useful, but I turn it off again once the problem is fixed :)
Now I've turned off syntax highlighting in vim and zsh and my irc client. Just to see what it's like. Will try it for a few days.
For what it's worth, I tried both syntax highlighted and not - and the former results in a measurable improvement in productivity. I believe experimenting with tweaking one's tools is worthwhile in most cases.
<%= render partial: 'user, collection: @user.followed_users %>
I see (and write) errors like this all the time. Turning :syntax on saves me a browser refresh/compile cycle/spec run every time. Not sure if I'd need a rigorous test to tell me how helpful it is :)Please avoid introducing classic flamewar topics unless you have something genuinely new to say about them.