Are you at risk of choosing a custom indentation style for Java code?

Does it really matter if you choose a custom indentation style?

This is the style that I see most often:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test {
    static public void main(String args[]) throws Exception {
        FileInputStream fin = new FileInputStream("infile.txt");
        FileOutputStream fout = new FileOutputStream("outfile.txt");

        FileChannel inc = fin.getChannel();
        FileChannel outc = fout.getChannel();

        ByteBuffer bb = ByteBuffer.allocateDirect(1024);

        while (true) {
            int ret = inc.read(bb);
            if (ret == -1)
                break;

            bb.flip();
            outc.write(bb);
            bb.clear();
        }
    }
}

But I prefer this style when it all starts with the following line:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test
{
    static public void main(String args[]) throws Exception
    {
        FileInputStream fin = new FileInputStream("infile.txt");
        FileOutputStream fout = new FileOutputStream("outfile.txt");

        FileChannel inc = fin.getChannel();
        FileChannel outc = fout.getChannel();

        ByteBuffer bb = ByteBuffer.allocateDirect(1024);

        while (true)
        {
            int ret = inc.read(bb);
            if (ret == -1)
                break;

            bb.flip();
            outc.write(bb);
            bb.clear();
        }
    }
}

It’s easier for me to read, but will I encounter problems associated with others if I use this style?

+3
source share
13 answers
  • Define a single convention in the team (preferably standard if you want to work with others later)
  • Configure your IDE and everything to use this format and nothing more.
  • Do reformatting automatically and preferably on each Ctrl-S.

, , .

Eclipse , (, ), , . Java → → Ctrl-S, .

,

, , , , , , , NullPointerExceptions .


: .


Edit 2014-08-19: , Eclipse , IntelliJ IDEA , .

+6

. , , , , , ..

+4

, , , .

, , 2 .

+3

. .

.

+1

, , .

, .

. , "" , - , .

, . .

PS - : . . , .

+1

. , . , IDE, . . - , . .

, .

+1

. ?

. 80 * 25. . .

@RequestMapping(value="/hotels/{hotel}/bookings/{booking}",
                method=RequestMethod.GET)
public String getBooking(
        @PathVariable("hotel") long hotelId, 
        @PathVariable("booking") long bookingId, 
        Model model) {                                    // WOW! I SAVED A LINE!
    Hotel hotel = hotelService.getHotel(hotelId);
    Booking booking = hotel.getBooking(bookingId);
    model.addAttribute("booking", booking);
    return "booking";
}
+1

, , , -, .

0

. , , , !

- . , , , .

, .

0

.

, . ( .)

, , .

{edit] Netbeans , IIRC.

0

, . . , Sun Microsystems, , . , .

, , .

, 6 , 5 , .

0

, . - , , . , / . , - .

0

Adopting the standard style established by Sun more than ten years ago will make it easier for people new to your organization to speed up, it will be code formatted as they were used during their previous careers.

In addition, any (well-thought-out) style will remain in effect as long as it is applied sequentially (I had a time room, figuring out a unique code style, where different people working on it used different styles even when changing existing methods).

0
source

Source: https://habr.com/ru/post/1766629/


All Articles