Accepting a numerical range in a function call

I have encountered two ways of doing it:

void foo(int from, int to); /* 'from' inclusive, 'to' exclusive */

void foo(int startIndex, int rangelength);

Has one style historically been preferred over the other? If so, was it just a matter of convention or was it due to some deeper underlying reason?

I’m currently programming in Java and noticed that the Arrays class uses the former style. The exclusivity of the to argument felt somewhat unintuitive to me, which led me to ask this question.

6

There isn’t a hard-and-fast rule. That’s because one of the fundamental tenets of API design is making your callers’ lives as easy as possible by giving them an interface that takes the kinds of values they’re likely to have on hand.

Your example has parallels to C’s memcpy(dest, src, n) function, which copies a range of memory n bytes long starting at src to a range of n bytes long starting at dest. Most of the function’s callers will be copying a primitive or data structure whose size in bytes can be determined using the sizeof operator:

some_structure src;
some_structure dest;
memcpy(&dest, &src, sizeof dest);  // Easy to understand

Had the function been implemented as memcpy(dest, src_start, src_end) (where src_start and src_end are pointers to the first and last bytes in the structure, the caller would have to do a lot more gymnastics to figure out where the source structure ends:

memcpy(&dest, &src, ((void *)&src) + sizeof src - 1);  // Harder to understand

What doesn’t work well for memcpy() might work very well for functions that do counting. If you know your callers have upper and lower range bounds in hand, it’s much better to give them count(int from, int to) than count(int start, int how_many):

int lower;
int upper;
count(lower, upper);  // No gymnastics

…versus…

count(lower, upper-lower+1);  // Gymnastics

Even if the innards of count() need to know the number of iterations, it’s better to calculate it in one place (inside the function) than force your callers who have upper and lower bounds to calculate it every time.

Sometimes which is better may be a toss-up. In those cases, you provide two functions, one that does the grunt work and another that’s just a wrapper that does the additional calculation:

count_iters(int start, int iterations) {
    // Do whatever this function does
}

count_range(int lower, int upper) {
    count_iters(lower, upper-lower+1);
}

Providing the wrapper centralizes the calculation of the number of iterations, which means that fixing a bug in it will correct the problem for everything at once and you won’t have to find and change every caller that’s doing it wrong.

Bottom line: If you pick one or the other arbitrarily or because someone says one style is better than the other, you haven’t examined the use cases and made a decision.

1

Which language? The answer to API design questions like this one is always language-specific. Many languages have built-in support for ranges. In those languages the answer is easy: use a single argument of whatever type is used to represent a range.

For Java, you might consider com.google.common.collect.Range from Google’s Guava library.

While exclusive upper bounds may seem unnatural, they are far more convenient than inclusive ranges because they eliminate a lot of fiddling around adding and subtracting one. Inclusive ranges over continuous types like time are very troublesome. How often have you seen things like { start: 00:00, end: 23:59 } ? What about 23:59:30 ? Using exclusive ranges eliminates those questions.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Accepting a numerical range in a function call

I have encountered two ways of doing it:

void foo(int from, int to); /* 'from' inclusive, 'to' exclusive */

void foo(int startIndex, int rangelength);

Has one style historically been preferred over the other? If so, was it just a matter of convention or was it due to some deeper underlying reason?

I’m currently programming in Java and noticed that the Arrays class uses the former style. The exclusivity of the to argument felt somewhat unintuitive to me, which led me to ask this question.

6

There isn’t a hard-and-fast rule. That’s because one of the fundamental tenets of API design is making your callers’ lives as easy as possible by giving them an interface that takes the kinds of values they’re likely to have on hand.

Your example has parallels to C’s memcpy(dest, src, n) function, which copies a range of memory n bytes long starting at src to a range of n bytes long starting at dest. Most of the function’s callers will be copying a primitive or data structure whose size in bytes can be determined using the sizeof operator:

some_structure src;
some_structure dest;
memcpy(&dest, &src, sizeof dest);  // Easy to understand

Had the function been implemented as memcpy(dest, src_start, src_end) (where src_start and src_end are pointers to the first and last bytes in the structure, the caller would have to do a lot more gymnastics to figure out where the source structure ends:

memcpy(&dest, &src, ((void *)&src) + sizeof src - 1);  // Harder to understand

What doesn’t work well for memcpy() might work very well for functions that do counting. If you know your callers have upper and lower range bounds in hand, it’s much better to give them count(int from, int to) than count(int start, int how_many):

int lower;
int upper;
count(lower, upper);  // No gymnastics

…versus…

count(lower, upper-lower+1);  // Gymnastics

Even if the innards of count() need to know the number of iterations, it’s better to calculate it in one place (inside the function) than force your callers who have upper and lower bounds to calculate it every time.

Sometimes which is better may be a toss-up. In those cases, you provide two functions, one that does the grunt work and another that’s just a wrapper that does the additional calculation:

count_iters(int start, int iterations) {
    // Do whatever this function does
}

count_range(int lower, int upper) {
    count_iters(lower, upper-lower+1);
}

Providing the wrapper centralizes the calculation of the number of iterations, which means that fixing a bug in it will correct the problem for everything at once and you won’t have to find and change every caller that’s doing it wrong.

Bottom line: If you pick one or the other arbitrarily or because someone says one style is better than the other, you haven’t examined the use cases and made a decision.

1

Which language? The answer to API design questions like this one is always language-specific. Many languages have built-in support for ranges. In those languages the answer is easy: use a single argument of whatever type is used to represent a range.

For Java, you might consider com.google.common.collect.Range from Google’s Guava library.

While exclusive upper bounds may seem unnatural, they are far more convenient than inclusive ranges because they eliminate a lot of fiddling around adding and subtracting one. Inclusive ranges over continuous types like time are very troublesome. How often have you seen things like { start: 00:00, end: 23:59 } ? What about 23:59:30 ? Using exclusive ranges eliminates those questions.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật