80O / PlusEMU

39 stars 26 forks source link

Improve nullability throughout. #51

Open 80O opened 2 years ago

80O commented 2 years ago

Nullable is enabled project wide right now. To improve nullable we should look at functions that might return a null object and add the nullable annotation to the return type (?).

Examples are getting objects from caches or lists, or checking the database if an object exists.

Implementing nullable allows us in the end to remove a lot of null checks as we can guarantee that there will always be an object passed into a method without any nullable annotations on the variables.

IDjinn commented 2 years ago

Also nullable attributes to improve null check

IDjinn commented 2 years ago

Also nullable attributes to improve null check

Example with Habbo.cs

    [MemberNotNullWhen(true, nameof(CurrentRoom))]
    public bool InRoom => CurrentRoom != null;

    public Room? CurrentRoom
    {
        get => _room;
    }

    public void Parse(GameClient session, ClientPacket packet)
    {
        if (!session.Habbo.InRoom)
            return;
        var action = packet.PopInt();
        var room = session.Habbo.CurrentRoom; // not null, checked in InRoom prop
//...
   }

The con is need convert Get*() property methods to properties, then analyzer can check null safety...

Should we use?

80O commented 2 years ago

Im personally not a fan of spamming attributed everywhere. I dont think it adds much value tbh. We can add it for special cases but worried it will be messy.