Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1 1, 2 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

added 33 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From From what I understand, using Class.cast()Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

I am working on an Android app that has an Abstract view controller class. I am using that Abstraction and implementing it for an Android Spinner view. It has a method that assigns the view to be controlled. I implemented it as:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 mView = (Spinner) Preconditions.checkNotNull(view);
}

I am getting feedback in a code review that would have me revise the method to:

@Override
public void setView(@Nonnull View view) {
 super.setView(view);
 Preconditions.checkNotNull(view, "The view provided is null");
 Preconditions.checkArgument(view instanceof Spinner,
 "view provided was not a Spinner view");
 mView = Spinner.class.cast(view);
}

I am unclear as to why the second is a better implementation. From what I understand, using Class.cast() is only really useful for generics (1, 2).

Other than that, it looks like the second implementation does the same thing. It will crash the app if view is null, or crash the app if view is not an instance of Spinner.

Is there a benefit to using the Precondition instanceOf check to crash the app in testing rather than a standard ClassCastException?

Source Link
user34092
user34092
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /