I think some carefully crafted code formating guidelines would be a good thing. It may be such guidelines are not ever explicitly seen by many members of the community but they would go a long way to informing the actions taken by the more active community members. If we are consistent in following such guidelines that would in turn provide the wider community with implicit examples of what they should do when including code samples in a question or answer.
Like Dominic I am also not fond of having to scroll horizontally to read code samples (or log file snippets for that matter) and think when possible it is a good thing encourage people to format code snippets so community users do not have to scroll sideways to see the whole snippet.
I am also going to add this to my internal list of things to look at when I edit a question or answer as I think this is the answer that prompted Dominic to post the formatting portion of his question and I had missed the horizontal scrolling when I edited the same answer to resolve other formatting issues just prior to his edit.
Regarding syntax highlighting, where appropriate we have configured tags with a default code language to use for the syntax highlighting of questions using that tag. For instance configuration will result in the lang-xml
formatter being used on any code samples in that question (well, sort of as there might be other tags configured with different code languages applied to the same question.)
This issue here is many of our tags, content-delivery as an example, can result in samples from any of a number of languages. In those cases or when different tags applied to a question have different code languages, prettify uses its default syntax highlighting rules.
In cases where the syntax highlighting looks 'off' we can and should encourage posters to apply one of the following language identifies to the sample:
- lang-none
- lang-default
- lang-bsh
- lang-c
- lang-cpp
- lang-cs
- lang-csh
- lang-css
- lang-hs
- lang-html
- lang-java
- lang-js
- lang-lisp
- lang-lua
- lang-ml
- lang-perl
- lang-php
- lang-py
- lang-proto
- lang-rb
- lang-scala
- lang-sh
- lang-sql
- lang-vb
- lang-xml
For snippets posted with common coding issues (or un-common coding issues for that matter) I would say it is always acceptable to edit them if they are posted in answers. I think this gets a little less clear if we are talking about a code snippet in a question as in that case it could be a contributing factor in the problem the OP is experiencing.
In all cases I also think it is acceptable to call out the issue in a comment on the question or answer. For example, on SQL questions posted on SO if the OP’s sample shows dynamically constructed SQL which is directly using user input I often see comments about the danger of SQL injection attacks.
It would seem to make sense to attach a similar comment regarding inappropriate as
casts when you see them. In fact I would say it would make sense to edit an answer with an inappropriate as
cast AND leave a comment mentioning this and the reason why the as
cast was inappropriate and maybe a link to a relevent source that goes into more detail such as Eric Lippert's post 'What's the difference between "as" and "cast" operators?'.