Stylesheet - TecProgGrupo08/Jchess GitHub Wiki

Stylesheet

This document is a set of code conventions to be used at JChess project, taken from EuVou Project.


1. Naming

1.1. Use camelCase to name attributes.

    //good example
    private bool isUserLoggedIn;

    //bad example
    private bool isuserloggedin;

1.2. Use camelCase to name methods and parameters.

    //good example
    private void setCurrentUserId(int currentUserId){
        this.currentUserId = currentUserId;
    }

    //bad example
    private void setcurrentuserid(int currentuserid){
        this.currentUserId = currentUserId;
    }

1.3. The method name should represent what it does, using a verb.

    //good example
    private void registerUser(User user){
        UserDAO userDAO = new UserDAO(getActivity());

        userDAO.saveUser(user);
    }

    //bad example
    private void userRegistration(User user){
        UserDAO userDAO = new UserDAO(getActivity());

        userDAO.saveUser(user);
    }

1.4. Use PascalCase to name classes and interfaces.

    //good example
    public class EventException extends Exception{
        public EventException(String message){
            super(message);
        }
    }

    //bad example
    public class eventexception extends Exception{
        public EventException(String message){
            super(message);
        }
    }

1.5. Use meaningful names to naming attributes. Abbreviations should not be used.

    //good example
    private EditText birthDateField;

    //bad example
    private EditText bdField;

1.6. There should be no attributes names with just one character, like a, b, c. Except the character i in loopings.

    //good example
    private View top5RankView;

    //bad example
    private View v;

1.7. Constants should be written in SCREAMING_SNAKE_CASE.

    //good example
    private int USER_STATUS;

    //bad example
    private int USERSTATUS;

1.8. Treat acronyms as words.

    //good example
    private String urlQuery;

    //bad example
    private String URLQuery;

Back to top

2. Formatting

2.1. Use TAB to indent, with size 4.

    
    //good example
    public Integer getEvaluation(){
        return evaluation;
    }

    //bad example
    public Integer getEvaluation(){
    return evaluation;
    }

2.2. Use a blank line to separate logic groups.

    //good example
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_event_consultation, menu);
        actionBar = getSupportActionBar();

        setSearchBar(menu);
        configActionBar();

        radioGroup = (RadioGroup) findViewById(R.id.search_radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        return true;
    }

    //bad example
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_event_consultation, menu);
        actionBar = getSupportActionBar();
        setSearchBar(menu);
        configActionBar();
        radioGroup = (RadioGroup) findViewById(R.id.search_radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        return true;
    }

2.3. Lines should have less than 100 characters.

    //good expample
    if(userEvaluationInDataBase == null){
            query = "INSERT INTO evaluate_user(grade, idUser, idUserEvaluated) VALUES (" +
                    "\"" + evaluation.getUserRating() + "\"," +
                    "\"" + evaluation.getIdUserLoggedIn() + "\"," +
                    "\"" + evaluation.getIdUserEvaluated() + "\")";
        }  

    //bad example
    if(userEvaluationInDataBase == null){
            query = "INSERT INTO evaluate_user(grade, idUser, idUserEvaluated) VALUES (" \"" + evaluation.getUserRating() + "\","\"" + evaluation.getIdUserLoggedIn() + "\"," ,"\"" evaluation.getIdUserEvaluated() + "\")";

2.4. The opening brace must be in the same line of the function, loops or conditional structure, and the closing brace must be in a level behind the code.

    //good example
    public Vector<String> getCategory(){
        return category;
    }

    //bad example
    public Vector<String> getCategory()
    {
        return category;
    }

2.5. There should be spaces around operators, comparators, =, : and ?.

    
    //good example
    private void setIdOwner(int idOwner){
        this.idOwner = idOwner;
    }

    //bad example
    private void setIdOwner(int idOwner){
        this.idOwner=idOwner;
    }

2.6. There should be no spaces after (, [ and before ), ].

    //good example
    private void setPrice(Integer price){
        this.price=price;
    }

    //bad example
    private void setPrice  ( Integer price ) {
        this.price = price;
    }

2.7. In loops, there should be one space after semicolons.

    //good example
    for(int i = 0; i < placesDataSize; i++){

               ...

                Place aux = new Place(idPlace,placeName, placeEvaluate, placeLongitude,
                        placeLatitude, placeOperationTime, placeDescription, placeAddress,
                        placePhone);

                int placesArrayListSize = places.size();

                places.add(aux);

                assert places.size() == placesArrayListSize + 1;
                assert places.get(placesArrayListSize) == aux;
     }

    //bad example
    for(int i=0;i<placesDataSize;i++){

               ...

                Place aux = new Place(idPlace,placeName, placeEvaluate, placeLongitude,
                        placeLatitude, placeOperationTime, placeDescription, placeAddress,
                        placePhone);

                int placesArrayListSize = places.size();

                places.add(aux);

                assert places.size() == placesArrayListSize + 1;
                assert places.get(placesArrayListSize) == aux;
    }

2.8. There should be no spaces after the method name and after for, if, else if and switch.

    //good example
    private boolean getUserLoginStatus(){
       boolean isUserLoggedIn = false;
       LoginUtility loginUtility = new LoginUtility(this.getActivity());

       if(loginUtility.hasUserLoggedIn()){
            isUserLoggedIn = true;
        }
        else{
            isUserLoggedIn = false;
        }
        
        return isUserLoggedIn;
    }

    //bad example
    private boolean getUserLoginStatus () {
       boolean isUserLoggedIn = false;
       LoginUtility loginUtility = new LoginUtility(this.getActivity());

       if(loginUtility.hasUserLoggedIn()) {
            isUserLoggedIn = true;
        }
        else {
            isUserLoggedIn = false;
        }
        
        return isUserLoggedIn;
    }

Back to top

3. Comments

3.1. The comments should have the same indentation as the code.

    
    //good example
    //Sets the required adapter to adapt items of the places array in items of the places list
    PlaceAdapter placeAdapter = new PlaceAdapter(getActivity(),places);

    //bad example
    //Sets the required adapter to adapt items of the places array in items of the places list
        PlaceAdapter placeAdapter = new PlaceAdapter(getActivity(),places);

3.2. The comments should start with uppercase.

    
    //good example
    //Saves the user evaluation set at database
    UserEvaluationDAO userEvaluationDAO = new UserEvaluationDAO(getActivity());

    //bad example
    //saves the user evaluation set at database
    UserEvaluationDAO userEvaluationDAO = new UserEvaluationDAO(getActivity());

3.3. The comments should be written in the language used in the code.

3.4. It has to be used // to comments with just one line and /* ... */ to comments with more than one line.

    
    //good example
    /**
    * File: ShowTop5Ranking.java
    * Purpose: Show the five places with better evaluation
    */

    //bad example
    //File: ShowTop5Ranking.java
    //Purpose: Show the five places with better evaluation

3.5. It is necessary to put one blank space after the opening of a comment and after the ending of the comment in case of comments with more than one line.

    
    //good example
    /* Gets user's ID by username. 
       OBS: IT'S ASSUMED THAT USERNAME DOES EXIST. */

    //bad example
    /*Gets user's ID by username. 
       OBS: IT'S ASSUMED THAT USERNAME DOES EXIST.*/

3.6. Must be placed @Override before all the methods overridden.

    
    //good example
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    //bad example
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

Back to top

4. Good Practices

4.1. Numbers or strings can not be used to indicate discret values.

    
    //good example
    enum Position{
       Museu, 
       Parque,
       Teatro,
       Shop,
       Unidade
    }

    switch(position){
        case Position.Museu:
            category = "Museu";
            break;
        case Position.Parque:
            category = "Parque";
            break;
        case Position.Teatro:
            category = "Teatro";
            break;
        case Position.Shop:
            category = "shop";
            break;
        case Position.Unidade:
            category = "Unidade";
            break;
    }
    

    //bad example
    switch (position){
        case 1:
            aux = "Museu";
            break;
        case 2:
            aux = "Parque";
            break;
        case 3:
            aux = "Teatro";
            break;
        case 4:
            aux = "shop";
            break;
        case 5:
            aux = "Unidade";
            break;
    }

4.2. Use constants instead of fixed numbers and strings in the code.

    
    //good example
    private final Double INVALID_LATITUDE = Double.valueOf(91);
    private Double latitude = INVALID_LATITUDE;

    //bad example
    private Double latitude = Double.valueOf(91);

4.3. A method must do only one task.

    
    //good example
    public JSONObject searchUserEvaluation(int userEvaluatedtId, int userId){

        final String QUERY = "SELECT * FROM evaluate_user WHERE idUser = \"" + userId
                + "\" AND idUserEvaluated = " + userEvaluatedtId;

        JSONObject userEvaluation = executeConsult(QUERY);

        return userEvaluation;
    }

5. Exceptions

5.1. Exceptions should not be ignored.

5.2. Generic exception should not be catched.

⚠️ **GitHub.com Fallback** ⚠️