api-design

API pagination best practices

别来无恙 提交于 2019-11-26 08:38:58
问题 I\'d love some some help handling a strange edge case with a paginated API I\'m building. Like many APIs, this one paginates large results. If you query /foos, you\'ll get 100 results (i.e. foo #1-100), and a link to /foos?page=2 which should return foo #101-200. Unfortunately, if foo #10 is deleted from the data set before the API consumer makes the next query, /foos?page=2 will offset by 100 and return foos #102-201. This is a problem for API consumers who are trying to pull all foos - they

Why does String.valueOf(null) throw a NullPointerException?

大城市里の小女人 提交于 2019-11-26 05:52:12
问题 according to the documentation, the method String.valueOf(Object obj) returns: if the argument is null , then a string equal to \"null\" ; otherwise, the value of obj.toString() is returned. But how come when I try do this: System.out.println(\"String.valueOf(null) = \" + String.valueOf(null)); it throws NPE instead? (try it yourself if you don\'t believe!) Exception in thread \"main\" java.lang.NullPointerException at java.lang.String.(Unknown Source) at java.lang.String.valueOf(Unknown

Why are Java Streams once-off?

丶灬走出姿态 提交于 2019-11-26 02:39:22
问题 Unlike C#\'s IEnumerable , where an execution pipeline can be executed as many times as we want, in Java a stream can be \'iterated\' only once. Any call to a terminal operation closes the stream, rendering it unusable. This \'feature\' takes away a lot of power. I imagine the reason for this is not technical. What were the design considerations behind this strange restriction? Edit: in order to demonstrate what I am talking about, consider the following implementation of Quick-Sort in C#:

Should an async API ever throw synchronously?

随声附和 提交于 2019-11-26 02:38:37
问题 I\'m writing a JavaScript function that makes an HTTP request and returns a promise for the result (but this question applies equally for a callback-based implementation). If I know immediately that the arguments supplied for the function are invalid, should the function throw synchronously, or should it return a rejected promise (or, if you prefer, invoke callback with an Error instance)? How important is it that an async function should always behave in an async manner, particularly for

Why is the Java date API (java.util.Date, .Calendar) such a mess?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-25 21:50:05
问题 As most people are painfully aware of by now, the Java API for handling calendar dates (specifically the classes java.util.Date and java.util.Calendar ) are a terrible mess. Off the top of my head: Date is mutable Date represents a timestamp, not a date no easy way to convert between date components (day, month, year...) and Date Calendar is clunky to use, and tries to combine different calendar systems into one class This post sums it up quite well, and JSR-310 also expains these problems.