Redian新闻
>
return null or empty list/set/...
avatar
return null or empty list/set/...# Java - 爪哇娇娃
n*y
1
It works well in Firefox. In Chrome browser, if the content in one row is
wide enough, the content is cut into two rows automatically..
恳请帮忙,谢谢!
avatar
b*y
2
for functions like this:
List search(...) {
}
When the search can't get anything, would you return null, or empty list?
avatar
m*r
3
没看太明白,可以加一个nowrap吗?

【在 n****y 的大作中提到】
: It works well in Firefox. In Chrome browser, if the content in one row is
: wide enough, the content is cut into two rows automatically..
: 恳请帮忙,谢谢!

avatar
Z*e
4
I prefer empty list

【在 b******y 的大作中提到】
: for functions like this:
: List search(...) {
: }
: When the search can't get anything, would you return null, or empty list?

avatar
A*o
5
empty list is easier for caller to handle. but it's hard to enforce because
many frameworks don't have such convention, and in the end, you still want
to check for NPE or risk NPE showing up in front of customer.
so, it's a nice to have convention.
avatar
b*y
6
speaking of convention, 你的函数的开头都validate arguments吗?
照说public method要throw IllegalArgumentExecption,
private method要用assert,然否?

because

【在 A**o 的大作中提到】
: empty list is easier for caller to handle. but it's hard to enforce because
: many frameworks don't have such convention, and in the end, you still want
: to check for NPE or risk NPE showing up in front of customer.
: so, it's a nice to have convention.

avatar
A*o
7
in an ideal world. but with refactor tools so strong,
private and public functions transform back and forth no stop,
not much you can do about these. a good test case coverage may serve better,
again, ideally.

【在 b******y 的大作中提到】
: speaking of convention, 你的函数的开头都validate arguments吗?
: 照说public method要throw IllegalArgumentExecption,
: private method要用assert,然否?
:
: because

avatar
b*y
8
what refactor tools u use?

better,

【在 A**o 的大作中提到】
: in an ideal world. but with refactor tools so strong,
: private and public functions transform back and forth no stop,
: not much you can do about these. a good test case coverage may serve better,
: again, ideally.

avatar
A*o
9
just intellij

【在 b******y 的大作中提到】
: what refactor tools u use?
:
: better,

avatar
b*y
10
o... we use eclipse...
back to that illegalargumentexception Q, it can catch exception
real time, while test case can't necessarily reproduce the situation.
what's the disadvantage of having methods validate their arguments
and throw illegalargumentexception? make system slower?

【在 A**o 的大作中提到】
: just intellij
avatar
A*o
11
it's a good practice if you can enforce it and standardlize it
w/o hurting your developer's morale and speed. that's all.

【在 b******y 的大作中提到】
: o... we use eclipse...
: back to that illegalargumentexception Q, it can catch exception
: real time, while test case can't necessarily reproduce the situation.
: what's the disadvantage of having methods validate their arguments
: and throw illegalargumentexception? make system slower?

avatar
F*n
12
Unfortunately, yes performance could be a big issue.

【在 b******y 的大作中提到】
: o... we use eclipse...
: back to that illegalargumentexception Q, it can catch exception
: real time, while test case can't necessarily reproduce the situation.
: what's the disadvantage of having methods validate their arguments
: and throw illegalargumentexception? make system slower?

avatar
c*t
13
Since IDEs only generate catch statements for checked exceptions and
some programmers just don't handle (either dunno or lazy) unchecked
exceptions. So throwing this kind of exception can be quite dangerous.
Only usable within certain confined areas.

【在 b******y 的大作中提到】
: o... we use eclipse...
: back to that illegalargumentexception Q, it can catch exception
: real time, while test case can't necessarily reproduce the situation.
: what's the disadvantage of having methods validate their arguments
: and throw illegalargumentexception? make system slower?

avatar
b*y
14
assume the validation logic is valid, then not doing it would
only make your system more vulnerable...
well, of cuz in most cases, even if the caller is not calling
the method with valid arguments, the system might still continue
w/o being broken. but in the cases where a call w/ invalid
arguments, not throwing illegalargumentexception here would
make it more difficult to diagnosis.

【在 c*****t 的大作中提到】
: Since IDEs only generate catch statements for checked exceptions and
: some programmers just don't handle (either dunno or lazy) unchecked
: exceptions. So throwing this kind of exception can be quite dangerous.
: Only usable within certain confined areas.

avatar
c*t
15
You misunderstood my position. I was arguing that throwing
IllegalArgumentException itself, or any other runtime exceptions
could be dangerous. For error checking, try to wrap it (and other
runtime errors) with checked exceptions.

【在 b******y 的大作中提到】
: assume the validation logic is valid, then not doing it would
: only make your system more vulnerable...
: well, of cuz in most cases, even if the caller is not calling
: the method with valid arguments, the system might still continue
: w/o being broken. but in the cases where a call w/ invalid
: arguments, not throwing illegalargumentexception here would
: make it more difficult to diagnosis.

avatar
s*n
16
that would be an abuse of checked exeptions.

【在 c*****t 的大作中提到】
: You misunderstood my position. I was arguing that throwing
: IllegalArgumentException itself, or any other runtime exceptions
: could be dangerous. For error checking, try to wrap it (and other
: runtime errors) with checked exceptions.

avatar
A*o
17
i remember there was an arguement saying
the caught exceptions are evil. something like it.
so, i think it's a preference.

【在 c*****t 的大作中提到】
: You misunderstood my position. I was arguing that throwing
: IllegalArgumentException itself, or any other runtime exceptions
: could be dangerous. For error checking, try to wrap it (and other
: runtime errors) with checked exceptions.

avatar
c*t
18
The problem is this, which happens quite often in library codes.
Library A:
try
{
foo (); // through injection, calls user function
}
catch (AException ex) // auto generated by IDE
{
printStackTrace ();
}
catch (... ex)
...
This looks all good, except that it would break on all runtime
exceptions on minor things such as illegalArgumentException.
You can try to fix the library code yourself, but if the user
update the library (e.g. install the latest), code would break.
You can argue th

【在 A**o 的大作中提到】
: i remember there was an arguement saying
: the caught exceptions are evil. something like it.
: so, i think it's a preference.

avatar
A*o
19
you have your arguement. the other side of the problem
being an exception is eation by a catch(Exception e) block
and behaving like everything is ok at the end of call
but in fact something was wrong under.
this happens a lot when you are dealing legacy codes.
and sometimes it also rooted to some careless ex-coder
who simply threw an Exception instance.
i've been in a project with 4k classes, and 7k references
of catch Exception blocks. //orz

【在 c*****t 的大作中提到】
: The problem is this, which happens quite often in library codes.
: Library A:
: try
: {
: foo (); // through injection, calls user function
: }
: catch (AException ex) // auto generated by IDE
: {
: printStackTrace ();
: }

avatar
b*y
20
the leak comes from this user function...
why should the lib cover up for the user function?
in reality case, if not showing odd exception to end user is a concern,
you can add a catch (Exception ex) at the top level of presentation
tier, and wrap it up there.

【在 c*****t 的大作中提到】
: The problem is this, which happens quite often in library codes.
: Library A:
: try
: {
: foo (); // through injection, calls user function
: }
: catch (AException ex) // auto generated by IDE
: {
: printStackTrace ();
: }

avatar
m*t
21
It doesn't just remind the caller, it _forces_ the caller
to deal with it when the caller may not want to or need to
or be able to.

Checked exception is a great way to remind the caller that, hey
check the error before moving on. IllegalArgumentException just
doesn't cut it since unless one is really looking for it, you
won't know there is a problem until things break.

【在 c*****t 的大作中提到】
: The problem is this, which happens quite often in library codes.
: Library A:
: try
: {
: foo (); // through injection, calls user function
: }
: catch (AException ex) // auto generated by IDE
: {
: printStackTrace ();
: }

avatar
g*g
22
I wouldn't mind using RuntimeException as long as the caller
cannot recover from it. Whenever I catch an exception, I would
always call logger.error("...", e) or something like to log
the exception.

【在 c*****t 的大作中提到】
: The problem is this, which happens quite often in library codes.
: Library A:
: try
: {
: foo (); // through injection, calls user function
: }
: catch (AException ex) // auto generated by IDE
: {
: printStackTrace ();
: }

avatar
s*n
23
devolpers will just declare "throws Exception" on all functions. ha!

【在 m******t 的大作中提到】
: It doesn't just remind the caller, it _forces_ the caller
: to deal with it when the caller may not want to or need to
: or be able to.
:
: Checked exception is a great way to remind the caller that, hey
: check the error before moving on. IllegalArgumentException just
: doesn't cut it since unless one is really looking for it, you
: won't know there is a problem until things break.

avatar
c*t
24
You misunderstood. foo () function part was all library code. Just
behind the scene that it calls user function.
In my experience, pretty much in all places where catch statement is
autogenerated, the code really means to catch all exceptions, including
runtime exceptions. However, throwing runtime exceptions would break
the code when actually it should not.
For example, one may check an string argument and throw an
IllegalArgumentException, then tries to use this string to open an file
(whic

【在 b******y 的大作中提到】
: the leak comes from this user function...
: why should the lib cover up for the user function?
: in reality case, if not showing odd exception to end user is a concern,
: you can add a catch (Exception ex) at the top level of presentation
: tier, and wrap it up there.

avatar
c*t
25
... It is not about catched exception, it is about leaking exception.
RuntimeException can easily be leaked and break the existing code for
trivial matters.

【在 g*****g 的大作中提到】
: I wouldn't mind using RuntimeException as long as the caller
: cannot recover from it. Whenever I catch an exception, I would
: always call logger.error("...", e) or something like to log
: the exception.

avatar
w*d
26
An empty list is always better than null, always invoke the method and let
the object handle the behavior is always the correct way to do. You can
create a NullList class implementing List interface and assign several
default behaviors to it.

【在 b******y 的大作中提到】
: for functions like this:
: List search(...) {
: }
: When the search can't get anything, would you return null, or empty list?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。