Indent group variable function call code convention [closed]
Asked Answered
W

3

6

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).

Wethington answered 19/4, 2016 at 4:36 Comment(0)
A
2

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.

Anaheim answered 19/4, 2016 at 4:48 Comment(1)
I agree that it's primarily an opinion-based question, but was just wondering whether it's really a bad practice that should be avoided almost all the times. Your comment about how it might looked like it's in a code block was what I was looking for, whether it's really inefficient or might lead to errors. I'd like to think my indentation as a virtual code blocks. So I guess there's nothing really wrong with it. Thank youWethington
W
2

I appreciate your thinking, and I don't think there's anything inherently bad about what you're doing. But the fact is that in organizations where multiple developers will be working on the same code, it's important to have consistent style rules. The exact rules will vary from organization to organization, or from team to team within an organization (where I work, different teams may adopt different styles, although the won't be radically different). But when it comes to things like indentation, spacing, where to start a new line, etc., the benefit of a consistent style usually outweighs any benefits you might get by doing it in a way that seems more logical to you. I sometimes don't like style choices that are imposed on me, but in matters like this it's not worth arguing over. However, if you're on a team, you could try to convince them that your style is better in a case like this. A drawback of doing it your way, however, is that a simpler set of rules can be programmed into an IDE which will then automatically space things according to your team's style.

In this case, if you're trying to use indentation to denote certain chunks of code that go together, might I suggest you move them into their own helper method? That would accomplish what I think you're trying to do.

Also, in the case where you have a list and are adding a number of items to it, you should look into Google Guava, which has some methods for setting up a list with a call that takes a variable number of arguments (e.g. newArrayList in the Lists class.

Weed answered 19/4, 2016 at 4:51 Comment(0)
R
1

This is somewhat a matter of opinion / at the discretion of the team. However, I agree that your indentation is better than no indentation. I think neither is the best solution. How about this:

List<Dog> dogs = initializeDogs();
System.out.println("list of dogs: " + dogs);
initializeCat(dogs);
Dog d1 = dogs.get(1);
d1.catches(cat1);

And then declare helper methods to do what you need:

private ArrayList<Dog> initializeDogs() {
 ArrayList<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));
 return dogs;
}

private void initializeCat(List<Dog> dogs) {
 Cat cat1 = new Cat(1);
 cat.meow();
 cat.startsRunningFrom(dogs);
}

The point is that your indenting convention is probably not necessary if you encapsulate your methods using best practices.

Roche answered 19/4, 2016 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.