Wrapping Primitives to Enable Returning null — Bad Practice?

I am frequently tempted to wrap integers, etc, solely for the purpose of writing methods that can return null. Negative 1 can work in many cases, but too often (especially in sound) it’s a valid return value.

Often, to get around this, I am returning a reference to a larger object that contains the primitive in question and calling a getter. This seems less efficient than the wrapper, and in some cases less encapsulating.

So, are there any penalties with wrapping for this reason? Does anybody do this? Does this smell?

7

One technique that I sometimes use in C/C++ is to have the function return a boolean value indicating whether the function was able to compute its result, and then pass a pointer as a function argument to where the function can store its result. If the function returns true you can grab the value you want using the pointer. For languages which don’t have pointers, such as Java, you can pass in some mutable container object (usually an array) to get a similar effect. This isn’t really common in Java, although I do remember seeing it used once or twice before. This approach has the benefit that you don’t need to wrap primitives, but you do need to create the array in languages like Java.

Scala and Google’s Guava library for Java offer implementations of the Option pattern for doing this sort of thing in a different way. Instead of returning null when your function can’t return a value, you return an instance of Option which has no value, and then the caller uses methods of the returned Option object to determine whether the actual return value was computed by the function or not and to also get the value if it is valid. Since the Option classes are generic, you still have to wrap your primitives anyway, so this approach actually increases the amount of overhead needed to handle the return value. However, Options do make the code easier to understand than checking an Integer return value for null. Also, it makes it obvious from the function signature that the function may not always return a valid value.

Here’s a contrived example of the former approach in Java:

boolean divide(int[] out, int a, int b) {
    if (b == 0) return false;
    out[0] = a/b;
    return true;
}

int x = ...;
int y = ...;
int[] out = new int[1];
if (divide(out, x, y)) {
    int d = out[0];
    // do something with d
} else {
    // deal with division by zero
}

and the latter with the Guava classes:

Optional<Integer> divide(int a, int b) {
    if (b == 0) return Optional.absent();
    return Optional.of(a/b);
}

int x = ...;
int y = ...;
Optional<Integer> o = divide(x, y);
if (o.isPresent()) {
    int d = o.get();
    // do something with d
} else {
    // deal with division by zero
}

You should also ask yourself if you should be throwing an exception instead of doing this. For example, in the above, it would probably just be better to always do the division and handle the ArithmeticException, or check if y is zero before calling the function. The right decision depends on a number of factors, such as whether the overhead of exception handling is going to be a problem for your use case.

4

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