简体   繁体   中英

Automatically converting coding conventions

When working on different projects, with different people and using different frameworks you often struggle to keep your code compliant to their conventions. Some teams get very strict about naming variables/methods/classes and other things the others make holy wars around the topic. I understand them and I fully support, but as any developer I have my own preference I wish I could code with comfortably. This makes me think whether there is a simple solution.

Are there any tools or editors that can automatically convert code to follow a different standard? I imagine there can be no such smart tool that will support naming conversions, so I'm ok with that, but I really wish to see

foreach($lala as $lalala) {

and not

foreach($lala as $lalala)
{

same goes with statements:

if(I_LIEK_COOKIES) {
    eat_cookie();
} else {
    toss_cookie();
}

and not

if ( I_LIEK_COOKIES ) {
    eat_cookie();
}
else
{
    toss_cookie();
}

(note the spaces between and around the parenthesis too)

I won't even mention spaces/tabs, I can convert it in my IDE with a shortcut but it would be awesome.

So the things I would like to get customized are

  1. spaces between parenthesis
  2. tabs/spaces and spaces per tab
  3. mustache brackets on the end of the line or on the new line
  4. always attach mustache brackets to any if/ifelse/else/for/foreach etc.

Some of the extras anyone would appreciate:

  1. Line ending style
  2. Delete extra spaces on the line endings (like sublime text 2 can do on save, but would be great for other IDE/editors)

The perfect workflow would be like this:

  1. I pull from git
  2. The code gets converted to my style
  3. I code stuff
  4. I commit and push
  5. Before everything gets pushed(or even commited) code gets converted to the convention style

Of course, someone may wish not to use git, then it would be simply converted when opening and after saving the file but as I understand it's impossible to do outside of an IDE/editor with a tool of some kind.

Has someone stumbled upon something like that? Could not find anything anywhere but tab/space conversion.

PS I wish to mention I'm working with PHP/JS so it's prioritized but I code using other languages on my spare time.

You could store configurations (eg vim .vimrcs, Eclipse preferences etc.) in each project's version control repository.

However , I think there's a big problem wrt. converting code when pushing/pulling to/from repositories. If someone reports an issue with your code (eg exception at line 100), converting the code when pulling from your repository is going to give you a different line 100. I don't think you can practically operate without working on the exact code that your compatriots are working with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM