I have this habit of indenting a group of function calls like this:
List <Dog> dogs = new ArrayList<>();
dogs.add(new Dog(1));
dogs.add(new Dog(2));
dogs.add(new Dog(3));
dogs.add(new Dog(4));
dogs.add(new Dog(5));
System.out.println("list of dogs: " + dogs);
Cat cat1 = new Cat(1);
cat.meow();
cat.startsRunningFrom(dogs);
Dog d1 = dogs.get(1);
d1.catches(cat1);
Are these really a bad practices in a code convention, or these haven't really being talked about? Because I've tried to find some code conventions that would recommend such indentations on function calls from certain variables/class.
For me, the above code is much more readable than without:
List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog(1));
dogs.add(new Dog(2));
dogs.add(new Dog(3));
dogs.add(new Dog(4));
dogs.add(new Dog(5));
System.out.println("list of dogs: " + dogs);
Cat cat1 = new Cat(1);
cat.meow();
cat.startsRunningFrom(dogs);
Dog d1 = dogs.get(1);
d1.catches(cat1);
The indentations for me provide a clear separation from variable declarations and other function operations, with the following tightly related operations on the variable.
Can anybody comment on why at all this is a bad practice, or if it's generally acceptable outside of the provided codes (outside of a List operations).
Are these really a bad practices in a code convention, or these haven't really being talked about?
Well, it depends! If you are working on open source then you have to stick to the guidelines pretty much. There is nothing like a bad/good unless your peers are fine with it.
As a matter of fact, all people in my Team use code formatters and have a set pre-defined rules for comments and white-spaces etc. We try to follow the same rules so that it is easy to spot the differences while merging the code back to the main repository.
The other thing is that, I'm so used to of seeing the generally accepted conventions that below code snippet tricks me in assuming the start and end of a block.
List<Dog> dogs = new ArrayList<>();
----> dogs.add(new Dog(1));
dogs.add(new Dog(2));
dogs.add(new Dog(3));
dogs.add(new Dog(4));
dogs.add(new Dog(5)); <----
System.out.println("list of dogs: " + dogs);
There is no harm in using such conventions, but as I said earlier, it should be accepted by your peers in the environment which you are working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With