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.

has there ever been a proposal for establishing a formatting style file for programmers? In web programming, we have CSS files that help separate style from 'code', so two people can see the same code while each can see it in his/her preferred way.

I was thinking something like this could theoretically be achieved for programming. How many times have you read code from another programmer with a formatting that you can't stomach? If you change the formatting, you're changing the source file, so whoever comes after you to read the same file could have the same issue against your formatting that you had.

You could say "define coding standards" but this is usually not done in small or individual teams before the company grows.

Why not separate formatting from code? You could define how you like code formatted (brackets here, new line after this, etc) and the IDE would take the source code and auto-format it to your liking (without changing the source code). I guess it wouldn't be a trivial thing, but I'm curious if there are languages or IDEs in which this has been done.

Has something like this been done or attempted ever? Is it doable? Could something like this be incorporated in, say, Visual Studio?

share|improve this question
    
I like this idea, but this assumes that you'll always want your code to adhere to a well defined style. You wouldn't have the option to go into "unstyled" mode without explicitly declaring an unstyled block... and then you run into the meta-issue regarding styling of untyled blocks (user A wants them unstyled, while user B wants then styled anyhow). –  MetaFight Feb 4 at 16:15
1  
Automatic code formatting has been done many times over. (The first large Pascal application I created in the 80s was a Pascal source code prettifyer.) The question is always what your workflow is, why differences exist and who profits from removing them. –  Kilian Foth Feb 4 at 16:18
    
I seem to remember that with Haskell there was an option to write programs with comments first and code second. And, of course, with Haskell the formatting was important since there were no braces. haskell.org/haskellwiki/Literate_programming#Bird_Style –  Daniel Hollinrake Feb 4 at 16:25
    
I'm sure it's doable. Most IDEs already let you define formatting rules and can reformat code based on those rules on demand. You could probably write a set of plugins for different IDEs and different languages that can export formatting rules in a common, easy-to-use standard format. Maybe based on CSS, if you like. Eclipse and NetBeans would probably be good platforms to start experimenting with. I don't know if such as thing has been done already, and I honestly don't know how much demand there is, but it could be interesting. I would like to see something like this for syntax highlights. –  FrustratedWithFormsDesigner Feb 4 at 16:29
1  
Take a look at JetBrains MPS. –  SK-logic Feb 4 at 19:15

2 Answers 2

up vote 2 down vote accepted

Yes - but not the way you're thinking.

What has been done for decades now is 'beautifying' code. You supply your code to a program that has a set of configuration options, and it formats your code accordingly.

A VS plugin that reformatted your code when you checked it out would be interesting, but... the huge problem with such things (and a formatting document too) is that your code will be re-formatted when you commit it, which will always create huge diffs so you won't be able to see the "good" changes from the automated bits.

I guess you could automatically reformat all code before it goes in, using a standard style, which would minimise this problem, but probably not eliminate it.

share|improve this answer
    
Or you could perform an in-memory/temp file formatting when doing a diff so the diffs are only done on code that's had the same formatting rules applied. –  FrustratedWithFormsDesigner Feb 4 at 16:52
    
that's the thing, just like applying CSS doesn't change the HTML, I think a standard could be defined where formatting is separate from code. When you save/check-in the file, the code would be saved stripping extra lines and what not. If the same 'stripping' algorithm is always used, then only when the actual code changes is where differences would show. –  Rodolfo Feb 4 at 19:13
    
Which is kinda what I said - you just have to format it according to some rule (and TBH you might as well format it with a readable style). The ultimate problem with an IDE that shows code formatted using a stylesheet is that you could never view it in a different editor. Expect this lock-in feature in the next Visual Studio :-) –  gbjbaanb Feb 5 at 8:44
    
@gbjbaanb well if the standard is public, like CSS is for example, then it'd be up to the editors/IDEs to use it. –  Rodolfo Feb 5 at 16:06

In some way, syntax highlighting, as it is done in most editors and IDEs, could be seen as a form of CSS for program text. It seems however that most programmer do not customise the syntax highlighting according to their needs (if it is possible).

share|improve this answer

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.