Code Style

Style == Success

What is code style?

Code style is the idea that code should be formatted in a standardized, readable, and functional way that does not inhibit debugging or peer reviews. Another benefit of good code style (especially in object oriented programming languages) is the ability to code to be easily expandable and added to.

Reasons for code styles:

As the code for your robots become more complicated, you will spend as much reading code as you do writing it. This is especially true if you have several programmers on your team, all writing code for your robots.

This is why having good code style for your robots is important. Being able to easily understand and read the code makes it easier to iterate and make your code better. Otherwise, you'll spend lots of time figuring out what your teammates and your past-self had written.

Here are a couple examples:

// Good Style Example
class Robot {
public:
    Robot() : speed(0), direction("North") {}

    void setSpeed(int newSpeed) {
        speed = newSpeed;
    }

    void setDirection(std::string newDirection) {
        direction = newDirection;
    }

    int getSpeed() const {
        return speed;
    }

    std::string getDirection() const {
        return direction;
    }

private:
    int speed;
    std::string direction;
};
// Bad Style Example
class robot{
    public: robot():speed(0),direction("North"){}
    void setspeed(int newspeed){speed=newspeed;}
    void SetDirection(std::string newDirection){direction=newDirection;}
    int getspeed(){return speed;}std::string GetDirection(){return direction;}
    private:int speed;std::string direction;};

While both of these pieces of code do effectively the same thing, the inconsistency and lack of proper spacing makes reading the bad code much more difficult.

Rules of code standards:

There are many different code style standards, and the specific one you use doesn't mater so much as you adhere to the following principles:

  1. Your code style is consistent across your project(s)

  2. All members of the team understand and cooperatively enforce the standards.

  3. The style should not inhibit the functionality of the code.

Appendix: Professional Code Style-Guides

These are reference style guides that exhibit the traits mentioned above.

Teams Contributed to this Article:

  • BLRS (Purdue SIGBots)

Last updated

Logo

This work is licensed under a Attribution-ShareAlike 2.0 Generic License