Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:23:59+00:00 2026-06-03T06:23:59+00:00

I work on a big java project(mobile application), and got the thankful job to

  • 0

I work on a big java project(mobile application), and got the “thankful” job to review and optimize/refactore code, because of the poor performance(high complexity).
Note: I’m totally new to Java(my background is C/C++), therefore I applogize for any dumb question.
The first thing I did, was to use Findbugs and fix all reported issues. Afterwards I used the metric tool Understand to get an overview, which
methods have a high cyclomatic complexity. Unfortunately there were a lot of methods with a cyclometic complexity above the 2^20 range 🙁
And one of them is where I would need some help or good ideas…

Short description:
For the communication with a server data has to be serialized. There is no Serializable interface available on this mobile framework.
Therefore, the colleague, who wrote all of the code(alone) implemented a Serializable interface containing one method called toByteArray().
E.g., the class Customer:

class Customer
{
    Address address;
    AttributeCollection attributes;
    LocationCollection locations;
    int recId;
    int recStatus;
    DateTime recCreated;
    String recCreatedBy;
    String recCreatedByProg;
    DateTime recChanged;
    String recChangedBy;
    String recChangedByProg;
    int refAddressesId;
    int refMandatorsId;
    CustomerPropertyUsage usage;

    /**
    * Serialize the properties of a class into a byte array.
    * @param destData Byte array, where the serialized data should be stored. Minimum 2 bytes.
    * @param serializationIndex Offset within the passed byte array, from which the serialized data of the class
    * should be entered. The offset is increased by the registered number of bytes so that after this method the
    * next call points to the serialized data subsequent byte.
    */
    void toByteArray(byte[] destData, IntClass serializationIndex)
    {
        if (this.address == null)
            this.usage.value &= ~CustomerPropertyUsage.ADDRESS;
        if (this.attributes == null)
            this.usage.value &= ~CustomerPropertyUsage.ATTRIBUTES;
        if (this.locations == null)
            this.usage.value &= ~CustomerPropertyUsage.LOCATIONS;

        this.usage.toByteArray(destData, serializationIndex);

        CatrString catrString = null;

        if ((this.usage.value & CustomerPropertyUsage.RECORD_HEADER) != CustomerPropertyUsage.NONE)
        {
            // Call static method getBytes from SerializationHelper class
            SerializationHelper.getBytes(this.recId, 4, destData, serializationIndex.value);
            serializationIndex.value += 4;

            SerializationHelper.getBytes(this.recStatus, 4, destData, serializationIndex.value);
            serializationIndex.value += 4;

            // recChanged is a DateTime object. For the serialization we need minimum a 7 bytes array,
            // Call method toByteArray() from DateTime class.
            this.recChanged.toByteArray(destData, serializationIndex);

            // call toByteArray of CatrString class
            catrString = new CatrString(this.recChangedBy);
            catrString.toByteArray(destData, serializationIndex);

            catrString.setValue(this.recChangedByProg);
            catrString.toByteArray(destData, serializationIndex);

            // Same as recChanged
            this.recCreated.toByteArray(destData, serializationIndex);

            catrString = new CatrString(this.recCreatedBy);
            catrString.toByteArray(destData, serializationIndex);

            catrString.setValue(this.recCreatedByProg);
            catrString.toByteArray(destData, serializationIndex);

            SerializationHelper.getBytes(this.refAddressesId, 4, destData, serializationIndex.value);
            serializationIndex.value += 4;

            SerializationHelper.getBytes(this.refMandatorsId, 4, destData, serializationIndex.value);
            serializationIndex.value += 4;
        }

        if (next property...)
        {
            ... Serialization ...
        }

        if (next property...)
        {
            ... Serialization ...
        }
    }
}

To keep the GPRS dues low, the server sets a value in this.usage.value and therefore only a specific property will be serialized and transmitted back to the server –>
transmitted messages are small.
This approach creates a lot of if-cases, depending on how much properties are in the class and therefore the path count gets higher and higher.
I think that’s not a beatiful solution but it’s okay. What I would like to change is the serialization calls inside the if-cases.
At the moment they look like this:

---- class SerializationHelper ----

    static void getBytes(long valueToConvert, int numOfBytesToConvert, byte[] destinationBytes, int destinationBytesOffset)
    {
        destinationBytes[destinationBytesOffset] = (byte)(valueToConvert & 0x000000FF);
        destinationBytes[destinationBytesOffset + 1] = (byte)((valueToConvert & 0x0000FF00) >> 8);

        if (numOfBytesToConvert > 2)
        {
            destinationBytes[destinationBytesOffset + 2] = (byte)((valueToConvert & 0x00FF0000) >> 16);
            destinationBytes[destinationBytesOffset + 3] = (byte)((valueToConvert & 0xFF000000) >> 24);

            if (numOfBytesToConvert > 4)
            {
                destinationBytes[destinationBytesOffset + 4] = (byte)((valueToConvert & 0x000000FF00000000L) >> 32);
                destinationBytes[destinationBytesOffset + 5] = (byte)((valueToConvert & 0x0000FF0000000000L) >> 40);
                destinationBytes[destinationBytesOffset + 6] = (byte)((valueToConvert & 0x00FF000000000000L) >> 48);
                destinationBytes[destinationBytesOffset + 7] = (byte)((valueToConvert & 0xFF00000000000000L) >> 56);
            }
        }
    }

---- class CatrString  ----

    void toByteArray(byte[] destData, IntClass serializationIndex)
    {
        //  Number of unicode characters
        SerializationHelper.getBytes(this.textLength, 2, destData, serializationIndex.value);
        serializationIndex.value += 2;

        // Text UTF-16 unicode characters
        for (int charIndex = 0; charIndex < this.textLength; charIndex++)
        {
            destData[serializationIndex.value] = (byte)(this.charCodes[charIndex] & 0x00FF);
            serializationIndex.value++;
            destData[serializationIndex.value] = (byte)((this.charCodes[charIndex] & 0xFF00) >> 8);
            serializationIndex.value++;
        }

        // Code End of string as UTF-16 unicode character
        destData[serializationIndex.value] = 0x00;
        serializationIndex.value++;
        destData[serializationIndex.value] = 0x00;
        serializationIndex.value++;
    }

---- class DateTime  ----

    void toByteArray(byte[] destData, IntClass serializationIndex)
    {
        destData[serializationIndex.value + 0] = (byte) (m_year % 0x0100);  // year low-Byte.
        destData[serializationIndex.value + 1] = (byte) (m_year / 0x0100);  // year high-Byte.
        destData[serializationIndex.value + 2] = (byte) (m_month);
        destData[serializationIndex.value + 3] = (byte) m_day;

        destData[serializationIndex.value + 4] = (byte) m_hour;
        destData[serializationIndex.value + 5] = (byte) m_minute;
        destData[serializationIndex.value + 6] = (byte) m_second;

        serializationIndex.value += 7;
    }

It should be possible to write a more “general” class which does all the serialization stuff, where I say serialize xy
bytes and that’s it. But what I don’t understand is what about “special” toByteArray() methods like for Strings(UTF-16 encoding)
or Date and Time? If I pack them in a class is that a good solution? Did I gain something with that? Maintable code? High-Performance code??
What would be your approach?

Thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:24:00+00:00Added an answer on June 3, 2026 at 6:24 am

    May be it’s better to create a Map of properties for each class. By default the set is empty but on call e.g. setAddress(address) we call fieldsMap.put(ADDRESS_KEY_STRING,address) instead of assigning to the class’ field.

    To store all acesses (existing) properties we just go through the fieldsMap storing each Entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I work in the ui of a quite big MFC application. We tried to
I work in the maintenance team of a big project (around 7k+ classes) and
I am a big java fan but i have to work now on C++
In a big project I work for, I am considering recommending other programmers to
I'm starting a Web Application project in Java and I've been doing some research
I am working on a big Java project, where Jython scripts are interpreted from
I've got a big project ahead of me, basically it's going to be a
We have a big Java application under Android (big just means it's too much
I am using Big query sample code to work with big query. I am
How to work with big numbers in PHP? such as (6*27^0+17*27^1+11*27^2+18*27^3+25*27^4+4*27^5)^65537

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.