tags:

views:

280

answers:

9

Given all the holy wars surrounding various code formatting styles, and many companies' strict formatting requirements, why don't IDEs allow dynamic reformatting of code?
By that I mean have the IDE format the code the way the user wants it every time, and save the code without any formatting at all. (Well maybe line breaks so that diffs are still easy)
The user wouldn't have to worry about adhering to a coding standard, people wouldn't get bent out of shape over working in code that's not formatted just how they like it, and formatting changes wouldn't show up in repository diffs.
There's have to be some mechanism for turning it off so it doesn't screw up old, pre-formatted code, but otherwise, what's keeping this from becoming a standard feature?

Edit: I'm aware that some IDEs have an reformat feature, but that causes almost as many problems as it solves -- source control diffs become nearly useless as the actual changes are lost in a sea of insignificant formatting changes, and different tab character widths still knock things out of alignment. Also, it doesn't let programmers work with the code in their preferred format.

+9  A: 

I think Eclipse allows you to format code however you like.

EDIT: Yeah, the Java Code Formatter formats based on rules you set in the project.

CookieOfFortune
Even better, it can reformat your code each time you save the file. This way you can configure formatting for project, and be sure that it will be consistent across all files.
Peter Štibraný
... if all your developers use Eclipse :-)
Peter Štibraný
Right. Many IDEs will reformat for you. But that's not the question. He's wondering why bother saving the file with formatting...have the IDE *display* it with whatever formatting is desired, but save it with none.
Beska
Well, sometimes I'm on a computer without an IDE and I would like to look at a source file with notepad (Let's say the computer doesn't have internet access). In that case, having formating would be really useful. I think it also removes a degree of flexibility, what if I wanted the code formatted differently in only one part of my program? I would like that option there without the IDE forcing the format into a different state when I re-open the file, unless you want that formatting exception stored in another file, which kind of defeats the purpose of keeping it out of the original.
CookieOfFortune
+8  A: 

Because most people like to see the code as it will be committed. Unless all of the tools you use to view your code (diff programs, grep, web based source repository viewers, etc) understand how to dynamically format the code the same way, you're going to be confused by different formattings when you look at it in your IDE vs. the other tools.

I agree it would be nice if we had more structural programming tools, rather than fixed width text tools, but it would require updating everything in your tool chain to really make it workable.

Brian Campbell
You might start to see stuff like this based around technologies like Oslo and Compiler Services... at least in the .Net space
Matthew Whited
+1  A: 

I think that the files that live on your actual machine in most cases should be in a common text format, it is beyond useful to be able to open up any source code file that I'm working on in notepad or any other handy text editor. Because of this I wouldn't want my local copies to contain any special characters that the IDE interprets as "This is an indent, please use a tab or a space here as you feel is appropriate". My opinion is that the best point to do this kind of formatting is the source control system, you check a file in formatted as you like, I check it out formatted as I like.

A while back I asked this question about source control tools not understanding code and ascalonx's answer pointing me towards source control in database. It's a shame that this idea doesn't seem to have more traction since it would solve both our problems and much more. I'm a .NET developer so I'm very much out in the cold with this at the moment, but apparently Eclipse and IntelliJ in the Java world are getting closer to this ideal (I don't know how close, since I've never used either)

Martin Harris
Source control in database (SCID) looks wonderful! Even a very primitive implementation would solve a lot of source management problems. It seems like both this question and the one you asked hinge on the idea of parsing code and turning it into well-defined elements, which could then be manipulated intelligently. Seems like a good first step on the way to full-blown SCID.
Whatsit
And the original idea was to store it without any additional characters. In the basic sense, think of it as a function that strips any optional whitespace, including the stuff between operators. So ( val == true ) is saved as (val==true).
Whatsit
If you did strip out all the white-space characters though you'd still have a hell of a time viewing it in anything that didn't include a formatter - imagine how a code file without any unneeded whitespace characters would appear in notepad or windiff. Far better that the codefile contains all the formatting and some outside process changes the file to match your style guidelines.
Martin Harris
A: 

Because most people don't care much about how the code looks as long as it is consistent.

fortran
Ug...this is one of my pet peeves. I'm one of those people that can't stand to see poorly formatted code (which funny enough many developers I have worked with have consistently poorly-formatted their code).
j0rd4n
This is simply not true. See Jeff's excellent blog about the topic: http://www.codinghorror.com/blog/archives/001254.html
JesperE
@JesperE, you thanks for your link, it is very supportive to my theory, in the very first article you can read:"What does matter is that you, and everyone else on your team, sticks with those conventions and uses them consistently."
fortran
A: 

I am sure there are programs and scripts available that format the code in the way you specify.

While some IDE's support such reformatting of code (Eclipse, Intellij for Java, Wing for Python, VS2007? for .net), even if they dont, it should be a matter of just running those scripts on the source code.

Lakshman Prasad
A: 

I think it's about time that the IDEs start storing their code as XML, and then use a user-defined XSL stylesheet to format the code upon presentation.

Gary Kephart
XML: The cause of, and solution to, all of life's problems.
mipadi
Ahhrgg! Please! Let me know when that happens, and I'll get out of the business.
JesperE
Downvoted? Honestly? C'mon. Compilers don't give a damn about the coding style, so why are we bothering storing it? Why not just store the important stuff: the actual code. And why not use some standard format to store the code as data? If not XML, then what?
Gary Kephart
It's a nice idea. After all, both XML and source code are intended to deal with data so that it's both machine-readable and human-readable. The problem is that source code in XML would just be too verbose. Consider the code (val==true). Now my XML's a bit rusty but I think that would translate to something like this: <group type="parentheses"><operator type="equality"><variable name="val" /><literal type="boolean" value="true" /></operator></group> That's not very human-readable. You could translate it through a machine, but then what's the point of storing it in XML in the first place?
Whatsit
@whatsit because XML is a common standard format for data storage. You could then leverage other existing tools, or even future tools, that could manipulate or display that data (code) in unforeseen ways. Human readability is irrelevant in this case.
Gary Kephart
My apologies, I was under the impression that the intent of XML was to store data in a human-readable format. A short bit of googling suggests that that's not the case, which moots my objection. FWIW the downvote's not mine so I can't remove it.
Whatsit
A: 

If you can handle the following scenario I would think it's a good idea but my hunch is that it's not trivial, which means that it's not intuitive and predictable which is bad for user experience.

If the code is written:

if(value!=null)

and dynamic formatter shows:

if (value != null)

and I modify it to:

if ((value != null) && (value.Length > 0))

and then another developer dynamically formats it to:

if ( (value != null) && (value.Length > 0) )

and changes some other line in the code and saves it. Does the above formatting also get saved? How do you reconcile the different formatting preferences of the two developers because his new additions will in a different format.

You'd have to store in a common format but even so the line lengths would be different and the developers would break the line at different positions for 80 or 100 char wrapping and it gets quite messy.

aleemb
That's exactly the sort of thing I'm trying to avoid -- the idea is that code should be stored with no formatting at all. Which *is* the common format, if you prefer to think of it that way. No line length limits, no whitespace. The formatting would be re-applied by the IDE when the user views it, and it would be applied according to the user's formatting preferences.
Whatsit
That would make it really difficult to browse the repository without the dynamic syntax modifier. Someone else writing to the repository would have a hard time reading/writing.
aleemb
A: 

Plagiarism would be hard to detect :)

aleemb
It should be easier -- you would have to do more than shuffle around line breaks to make it look different.
Whatsit
On a serious note, forensic analysis also looks at this sort thing to determine coding style. If style can't be distinguished it will be harder.
aleemb
I didn't know that. I assumed that formatting would be ignored, since -- as many others have stated here -- it's very easy to point an IDE to the source and say 'reformat to look like this'. Do you know of an article or site where I could read up on the role of formatting in forensic analysis?
Whatsit
+2  A: 

I've thought this would be a good idea too, but as a function of the source control software rather than the IDE.

Code could be stored as a list of tokens and then reconstructed when copied to the development machine. This answers aleemb's point as no white space is ever stored. There might need to be more sophisticated formatting rules to cope with all cases though.

It also answers Martin Harris' concerns - the code on your machine wouldn't contain any special characters just white space between tokens.

ChrisF