DEV Community

Calin Baenen
Calin Baenen

Posted on

Respecting someone's gender in a Person object.

Here's how I'd respect someone's gender in TypeScript

/*
 * A 3-bit number that represents the person's gender.
 * (bits in the order of: MFT.)
 * (MFT stands for "Male Female Trans".)
 * (Special blends:
 *  0x111 = Gender Fluid
 *  0x110 = Bigender
 *  0x001 = Nonbinary
 *  0x000 = Agender
 * )
 * (
 *  Expansion upon this format is welcome. If you want
 *  to add more attributes, for more non-binary genders,
 *  feel free, and tell me your format below!
 * )
*/
type Gender = number;
function sterilizeGenderId(g:Gender):Gender {
    return g & 0b111; // 0b111 = 7.
}

class Person {
    constructor(
        name:String|string, age:number, gender:Gender
    ) {
        this.gender = sterilizeGenderId(gender);
        this.name = name;
        this.age = age;
    }
    gender:Gender;
    name:String|string;
    age:number;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
grahamthedev profile image
GrahamTheDev • Edited

If you are going to go down this route and try to improve the inclusivity of your forms etc. then storing a string and letting them type their own Gender (with a combobox so common gender identities are listed, but you can overtype whatever you want) would be far better.

So storing a persons gender as just a plain string would be better.

Then if you do need an index for whatever reason use string matching on save to match a gender identity to an ID, or a join on a table of all options if you are using relational DBs is the way to go.

Ideally though, if you are doing this, just remove the need for Genders entirely, most companies don't actually need it and using first names to address people is now considered much more acceptable in written communications.

Obviously there are edge cases where a gender is needed, such as car insurance where men are more likely to crash than women so get worse rates (although whether transgender people get a better or worse deal is a minefield I would not want to try to work out as an insurer!) or medical forms (but then that is another story entirely we could write a book on!), but most of the time the answer is to remove the gender field entirely if you want to be inclusive.

Just to be clear though, none of this takes away from your solution, it is space efficient and clever!

Just suggesting better alternatives! Any step towards inclusion is awesome in my book ❤🦄

Collapse
 
calinzbaenen profile image
Calin Baenen

I like the idea of using a string, but I don't really like strings, at least for small things.
What I would do to incorporate your suggestion is to have Gender be number|string, where the 3-bit code is still an option, but now it allows for custom (unrecognized*) gender names.

Collapse
 
calinzbaenen profile image
Calin Baenen

I've thought of a pattern that not only encapsulates gender identity, but also sexual orientation (not like I'd know who'd want that from there forms -- maybe dating sites \;p).

// Format: MFTmfS
// Stands for "Male Female Trans masculinity femininity Skolio".
// With this, the first 3 bits are the last pattern, and the last 3 are the SO.

// Special blends:
// 0b111 = Pansexual
// 0b110 = Bisexual
// 0b101 and 0b010 = Demisexual
// 0b000 = Asexual
Enter fullscreen mode Exit fullscreen mode