hfurubotten / autograder

Automatic management and build tool for lab assignments. Moved to organization autograde: https://github.com/autograde
https://github.com/autograde
Other
14 stars 7 forks source link

Unnecessary nil checks #1

Closed meling closed 9 years ago

meling commented 9 years ago

In ListTeams and ListRepos (and possibly other places) the following code template has lots of unneeded nil checks:

        team = Team{}
        if t.ID != nil {
            team.ID = *t.ID
        }
        if t.Name != nil {
            team.Name = *t.Name
        }
        if t.Permission != nil {
            team.Permission = *t.Permission
        }
        if t.MembersCount != nil {
            team.MemberCount = *t.MembersCount
        }
        if t.ReposCount != nil {
            team.Repocount = *t.ReposCount
        }
        teams[team.Name] = team

If the *t.<struct_var_name> is nil, so will the corresponding team.<var_name> be. Suggest to construct the Team{} object passing in the relevant fields from the *t object, e.g. something like:

team = Team{*t.ID, *t.Name, *t.Permission, etc...}

Or if you foresee that the Team (and Repo) structs will be expanded, you may wish to name the destionation fields.

If you agree with this suggestion, I can fix it. It would be helpful to have some unit tests to ensure that we don't mess things up.

hfurubotten commented 9 years ago

The problem I was trying to overcome was that when using the * symbol to dereference the content within the pointer, it would give a nil pointer exception when it was nil. Any other way to get the content and at the same time avoid the nil pointer exception?