5

UPDATE: an issue would arise from stuff that is not strictly defined by a formatter, like consecutive empty lines etc., that would get lost in the process (local to server to local reformatting) which would annoy people (me as well :)

I'm used to adopting some shared code formatter so my question is not about "should I submit to that?"

I wonder does this (code formatting) have to matter at all? Isn't it possible to have your repository store your commmits in some "default" formatting? You would never get to see what that style looks like, because every time you checkout all the sources get saved to your HDD after getting reformatted using YOUR formatter. Also every time you would browse the repository your client would show you the source reformatted using YOUR formatter. Also all diffs would be done after reformatting using YOUR formatter.

Is this possible to do? (E.g. client plugins, repository configuration, etc.)

2
  • +1 Absolutely. I want it now. Where do I have to sign?
    – JensG
    Nov 2, 2014 at 10:44
  • Fundamentally, your proposed solution solves the wrong problem. Nov 2, 2014 at 18:37

3 Answers 3

4

Having no rules is not freedom, it's just chaos. How would one write a book on programming language without any coding standards? Can you imagine each book having completely different formatting?

I have personal preferences about how the code should look like, but my personality is not really important when I work in a team. Collaboration is important, and coding standards make it possible, not just easier.

Is this possible to do? (E.g. client plugins, repository configuration, etc.)

Yes, but with worse performance, bugs, and additional setup phase. I have enough of configuration and plugins thanks to Maven, so I better stick with good old discipline.

However, there is an idea about storing a program not as it's source code, but as it's syntax tree. Like language-agnostic ASTs and stuff.


Update: On comments.

I see and understand your point, but I still believe my concerns are true.

Imagine you have those magic plugins which abstract away the formatting. You team up and code with other people without knowing about their preferences, and it's all double rainbow all across the sky =)

Then, one of your colleagues tells you something like: "I'm writing a book about programming, need your help." Then she sends you a manuscript and you see she's gone completely opposite direction with not only formatting, but even with concrete syntax. Imagine she invented something like CoffeeScript on top of the language you both use: keywords are different, shorter syntax constructs, etc. It's totally possible when you have such a magic plugin and people will do it (because of Murthy's law, and it ain't no joke).

So, you can't help her, unless you both reject your personal preferences and use "standard", "default" formatting and concrete syntax. Ta-a-da-a!

So I claim:

  1. If there are N people and each one is allowed to have his/her own concrete syntax and formatting styles, there eventually be N different styles.
  2. If there are people using those different styles, they would either write books using those different styles, or agree on using "default".
  3. If you end up using "default" anyway to be able to collaborate, why bother with customization?

I'm not insisting on "purity" or something. I work with code which often has a mix of different formatting styles. But that's mostly just slight differences which don't affect the language. I'm totally fine with that.

But I don't want to learn a new language just to feed someone's ego (my own too, for that matter). I sometimes just copy-paste code in my instant messenger and I don't want to have any plugin in it (on both mine and recipient sides, I'll have you know) to send a snippet.

If you have such a magic plugin in your IDE, you have to have such plugins everywhere you want to stick you code to. That's nothing more than additional problems.

Summary: you can do so, but you shouldn't. Standards are better when customization. Conventions are better than plugins.


Update 2:

Image person A prefers to put an opening brace on a separate line:

while (true) // endless loop
{

Then this code can be stores in some kind of syntax tree and then shown to person B, who uses Egyptian style. That can be done either like so:

while (true) /* endless loop */ {

or so:

// endless loop
while (true) {

And this second one has different order of tokens.

This shows that not all syntax constructs would fit in this model. So the language should be probably restricted in many ways to provide ability to customize formatting. Still I think it is possible, since there are programming languages like Google's Blockly which have completely different concrete and "stored" syntaxes.

By the way, Blockly is a proof of concept, since you totally can customize the frontend to fit your artistic taste.

9
  • 1
    "Can you imagine each book having completely different formatting?"- I can imagine a book that has a standard formatting when it is in the bookshelf (=repo), another one when you read/write it and yet another one when I read/write it. Can't you? Formatting is only a way to represent the code on screen/printout. The only thing the compiler is interested in is syntactically valid code, nothing else. We humans are interested in formatting, but even then you'll find that different people have different preferences. That's a feature I'm looking for in modern IDEs for years now.
    – JensG
    Nov 2, 2014 at 10:34
  • 1
    "... storing a program not as source code, but AST" - Good, finally we're heading in the right direction. Anything that can be found in coding standards with regard to formatting is only a crutch that would no longer be needed once everyone gets his own view of the code without affecting anyone else.
    – JensG
    Nov 2, 2014 at 10:40
  • 2
    Sure. But nobody is going to tell me how code looks in my editor. For teaching/whiteboarding, people will have to read my code, and I theirs. Don't think that's a big problem. But at their own desk they can do whatever they want, and so do I. Wouldn't that be cool?
    – JensG
    Nov 2, 2014 at 11:50
  • 1
    I agree with @scriptin. Not to mention pair programming, which is sometimes used not only as a learning tool for new-joiners, but as a full part of a team's development technique. Nov 2, 2014 at 12:25
  • 2
    "You'd have to read their code written in their language" - Remember, I talk only about formatting here. Nothing else.
    – JensG
    Nov 2, 2014 at 18:28
4

Technically, the main problem I see is with line numbers and debugging. If you reformat the code, code might end-up on different lines, making it much harder to make sense of stacktraces and such. Also referring to code in general would be much harder, if your colleagues see different lines than you.

In context of collaboration, I think recognizability of the code is important. If I look at the same code on the screen of a colleague, it would be much harder to collaborate if I don't easily recognize the code.

1
  • Yes, I was automatically assuming you would debug what you compiled, which so often is not the case of course. Nov 3, 2014 at 13:40
3

There's an excellent tool uncrustify that takes a description of the source code formatting from a config file and outputs a formatted file as per specification in config file. However, there's a few problems with this approach, most obvious and important are:

  1. File stored in the repository will be different than view of your file making looking into diffs much harder.

  2. It will be much harder for you to keep whitespace changes separated from the important commits.

I'm sure there's more.

1
  • 2
    As explained, no whitespace changes would get committed, whitespace would get taken care of by default server formatter. Before a diff is made both files would get reformatted by your viewing tool. Nov 2, 2014 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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