Code formatting:

  • Order your parameter based on the importance/relevance
  • No blank spaces, unless it add value such as breaking two logic..etc
  • Always indent your code properly
public class Todo {

    private final String id; 

                                 - Extra spaces here! 


    private final String name;

    public Todo(String id, String name) { - Order based on importance, 'id' is more important than 'name'
        this.id = id;
        this.name = name;
    }

        public String getId() { - Not indented properly 
            return id;
        }

    public String getName() {
        return name;
    }

                                - Extra space

}

When you have too many parameters:

Encapsulate many related parameters into a single object :

public class PersonService {

    //Not using an object to encapsulate the parameters
    public void createPerson(String firstName, String lastName, String age, String gender, String address, String mobileNumber, String homePhoneNumber){
        //Logic to create person, store into database..etc
    }

    //Using an object to encapsulate parameters
    public void createPerson(Person person){
        //Logic to create person, store into database..etc
    }

}

public class Person {

    private final String firstName;
    private final String lastName;
    private final String age;
    private final String gender;
    private final String address;
    private final String mobileNumber;
    private final String homePhoneNumber;

    //Constructor
    //Getters
    //Setters

}

https://stackoverflow.com/questions/2244860/when-a-method-has-too-many-parameters

results matching ""

    No results matching ""