Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name the "magic numbers" #24

Open
eximius313 opened this issue Feb 24, 2023 · 5 comments
Open

Name the "magic numbers" #24

eximius313 opened this issue Feb 24, 2023 · 5 comments

Comments

@eximius313
Copy link

BerTlvBuilder contains a lot of "magic numbers" (0x80, 0x81, 0x100 and so on). What are they? Could you make them constant?

    private void fillLength(byte[] aBuffer, int aOffset, int aLength) {

        if(aLength < 0x80) {
            aBuffer[aOffset] = (byte) aLength;

        } else if (aLength <0x100) {
            aBuffer[aOffset] = (byte) 0x81;
            aBuffer[aOffset+1] = (byte) aLength;

        } else if( aLength < 0x10000) {

            aBuffer[aOffset]   = (byte) 0x82;
            aBuffer[aOffset+1] = (byte) (aLength / 0x100);
            aBuffer[aOffset+2] = (byte) (aLength % 0x100);

        } else if( aLength < 0x1000000 ) {
            aBuffer[aOffset]   = (byte) 0x83;
            aBuffer[aOffset+1] = (byte) (aLength / 0x10000);
            aBuffer[aOffset+2] = (byte) (aLength / 0x100);
            aBuffer[aOffset+3] = (byte) (aLength % 0x100);
        } else {
            throw new IllegalStateException("length ["+aLength+"] out of range (0x1000000)");
        }
    }
@martinpaljak
Copy link

These are constants.

@eximius313
Copy link
Author

Great, and what are their names/meanings?

@eximius313
Copy link
Author

by constant I meant:

static final int FIELD_DELIMITER = 0x80

@martinpaljak
Copy link

What would that add? Did you read TLV specs? Why is "CONSTANT_100" better than "100" ?

@eximius313
Copy link
Author

eximius313 commented Feb 26, 2023

"100" is a value. But in the code it has a meaning. And the name should express that meaning.
For example - why it's 0x80 and not 0x83 or 0x42? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants