Java and elegance

Source: Internet
Author: User

The syntax of the Java language is famous for its elegance. At least many of the books and materials I have been familiar with are so mentioned. Of course, I also feel this elegance in most cases based on my hundreds of lines of Java programming experience, until a special situation occurs.

This is the case. Assume that you have written a Data Access Manager, one of which provides the user with the ability to obtain data, using the familiar C or C ++, I will soon write a function definition similar to the following:

size_t DataManager_GetData(void* buffer, size_t bufferSize);

Or

class CDataManager{    //...    size_t GetData(void* buffer, size_t bufferSize);    // ...};

The input parameter is the buffer for receiving data and the size of the buffer. The returned value is the size of the returned data. Of course, there is no pressure on Java to support such a small requirement, and I will not be ugly about the sample code. Next, the demand for this method is slightly increased. In addition to the data size to be returned, we hope to provide an indication by the way, indicates whether more data is available in the buffer. Based on this requirement, the C ++ method prototype is usually revised as follows:

size_t GetData(void* buffer, size_t bufferSize, bool& moreData);

How should we write out a method prototype that provides the same functions in Java? As a cainiao, I tried to pass in a Boolean parameter (because a friend told me that the Java parameter is actually a "Reference", of course, do not believe it so naively, or I will not be dumpily writing this article.) Obviously, it fails. Later, I cleverly remembered that the native type and object type seemed to be different in Java, so I changed Boolean to boolean, and it was a miracle with hope, and it ended in failure.

So I turned to my colleagues and friends. There are many answers. For example, map the data volume and tag to an entry and return a map-type object. For example, an array of objects is returned. Yes, I know they are all ways to solve this problem, but I think they are not elegant enough to echo the title. Because the two data that I want to return at the same time have a weak relationship with each other. Essentially, they are two types of information that are hard to merge and return as an object (or an object, I think it's ugly. Unfortunately, I have not yet come up with a better solution.

What about everyone? Please kindly advise. Of course, it is best not to question the requirement, that is, to divide it into two calls ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.