aevitas / flakeid

Discord-like implementation of highly optimized decentralized, K-ordered unique IDs in .NET.
Apache License 2.0
38 stars 9 forks source link

Remove magic numbers in IdExtensions.IsSnowflake() #8

Closed haruki-taka8 closed 9 months ago

haruki-taka8 commented 9 months ago

These lines in IdExtensions.IsSnowflake() contains constants that we can derive from the Id class:

        long timestamp = id >> 22;
        long thread = (id >> 17) & 0b11111;
        long process = (id >> 12) & 0b11111;
        long increment = id & 0b111111111111;

The numbers 22, 17 and 12 are just the sum of the (private) constants xxxBits, and the bitmasks are immediately available from the Id class.


I propose changes to the lines so that they use descriptive names:

        long timestamp = id >> TimestampOffset;
        long thread = (id >> ThreadOffset) & Id.ThreadIdMask;
        long process = (id >> ProcessOffset) & Id.ProcessIdMask;
        long increment = id & Id.IncrementMask;

I have a local branch with the required changes, and it has passed the unit tests. If this idea works with you, I can create a pull request.

aevitas commented 9 months ago

Hi Haruki, thanks for opening this issue! I agree this would improve the legibility of the code and am looking forward to your PR :)