title: Is validating fields in both constructor and setter considered bad redundant code?
body: <p>I have the following class : </p>
  
  <pre><code>public class Project {
  
      private int id;
      private String name;  
  
      public Project(int id, String name) {
          if(name == null ){
              throw new NullPointerException("Name can't be null");
          }
  
          if(id == 0 ){
              throw new IllegalArgumentException("id can't be zero");
          }
  
              this.name = name;
              this.id = id;
  
      }
  
      private Project(){}
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) { 
          if(id == 0 ){
              throw new IllegalArgumentException("id can't be zero");
          }
          this.id = id;
      }
  
      public String getName()
          return name;
      }
  
      public void setName(String name) {
          if(name == null ){
              throw new NullPointerException("Name can't be null");
          }
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>If you noticed that setName and setId share the same validation for its fields with the constructor. Is this redundant code that could cause issues in the future ( for example if somebody edit the the setter to allow 0 for the id and prevent -1 instead but didn't change the constructor) ? . Should I use a private method to do the check and share it between the constructor and the setter which seems too much if there's a lot of fields. </p>
  
  <p>Note: This is why im not using the setters in the constructor. <a href="https://stackoverflow.com/a/4893604/302707">https://stackoverflow.com/a/4893604/302707</a></p>