Struct, mapping, memory, storage best practices?

Hello,

I came across a tutorial on Structs and Mapping. But, it was written in Solidity version 0.4.18. I made changes to the code to include storage and memory to get it compatible with Solidity version 0.6.12. The code is working fine. However, I am not sure if I am following the best practices. I mean, I want know if I am correctly using storage and memory in the code. I really appreciate if anyone can take a look at the code and let me know if I have to make changes. Here is the code.

pragma solidity 0.6.12;

contract Courses {
    
    struct Instructor {
        uint age;
        string fName;
        string lName;
    }
    
    mapping (address => Instructor) instructors;
    address[] public instructorAccts;
    
    function setInstructor(address _address, uint _age, string memory _fName, string memory _lName) public {
        Instructor storage instructor = instructors[_address];
        
        instructor.age = _age;
        instructor.fName = _fName;
        instructor.lName = _lName;
        
        instructorAccts.push(_address);
    }
    
    function getInstructors() view public returns(address[] memory) {
        return instructorAccts;
    }
    
    function getInstructor(address _address) view public returns (uint, string memory, string memory) {
        return (instructors[_address].age, instructors[_address].fName, instructors[_address].lName);
    }
    
    function countInstructors() view public returns (uint) {
        return instructorAccts.length;
    }

    function updateInstructor(address _address, uint _age, string memory _fName, string memory _lName) public {
        instructors[_address].age = _age;
        instructors[_address].fName = _fName;
        instructors[_address].lName = _lName;
    }
    
}

Regards,
Harish

1 Like

Also, how do I check if an address exists in address[] public instructorAccts; ?

1 Like

How about this contract EnumerableSet in the OpenZeppelin repo.

1 Like