1
0
mirror of https://codeberg.org/gitnex/GitNex synced 2024-12-22 00:04:56 +01:00
23 Code Standards
M M Arif edited this page 2023-09-26 07:25:36 +00:00

Writing clean and understandable code is GitNex core value from the start. Here are the few standards when writing code for GitNex.

Note: GitNex uses Google Spotless to reformat the code, which the CI can also pass. To apply the formatting, run ./gradlew :app:spotlessApply in the project's root directory before committing the changes.

package org.mian.gitnex.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.MenuItem;

/**
 * @author M M Arif
 */

The 1st line in above code is the package class. After that is new blank line and then the import starts. Import packages should not have any blank lines in between.
After import lines there is one more blank line and after that is the author of the template/code.

If you have copied the template code from another file, then the author section should be:

package org.mian.gitnex.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.MenuItem;

/**
 * @author M M Arif
 * @author 6543
 */

Next is the main class,

public class ExploreRepositoriesFragment extends Fragment {

    private static String repoNameF = "param2";
    private static String repoOwnerF = "param1";
    private ProgressBar mProgressBar;
    private RecyclerView mRecyclerView;
    private TextView noData;
    private TextView searchKeyword;
    private Boolean repoTypeInclude = true;
    private String sort = "updated";
    private String order = "desc";
    private int limit = 50;
    
}    

Note the blank line before declaring any variables and functions.

Usage of if statements:

if (response.isSuccessful()) {

    assert response.body() != null;
    getReposList(response.body().getSearchedData(), context);
} 
else {

    Log.i("onResponse", String.valueOf(response.code()));
}

Tab or spaces?

GitNex app has used tabs(4 spaces) and it is recommended to use tab for indentation.

Multiple statements

All statements should have proper indentation. Example,

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(MenuItem item) {
    
        switch (item.getItemId()) {
        
            case R.id.repoStargazers:

                Intent intent = new Intent(context, RepoStargazersActivity.class);
                intent.putExtra("repoFullNameForStars", fullName.getText());
                context.startActivity(intent);
                break;

            case R.id.repoWatchers:

                Intent intentW = new Intent(context, RepoWatchersActivity.class);
                intentW.putExtra("repoFullNameForWatchers", fullName.getText());
                context.startActivity(intentW);
                break;

            case R.id.repoOpenInBrowser:

                Intent intentOpenInBrowser = new Intent(context, OpenRepoInBrowserActivity.class);
                intentOpenInBrowser.putExtra("repoFullNameBrowser", fullName.getText());
                context.startActivity(intentOpenInBrowser);
                break;

        }
        
        return false;
        
    }
    
});

Variable names and scope

Variable names has to be clear for what it is used. Scope should be defined properly for the variable. Example,

private TextView issueNumber;
private ImageView issueAssigneeAvatar;

Layout ID's name

Every layout must have components with defined ID's. Example,

<TextView
    android:id="@+id/branch_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textIsSelectable="true"
    android:textSize="18sp" />

<TextView
    android:id="@+id/branch_commit_author"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:textColor="@color/colorWhite"
    android:textSize="16sp" />

<TextView
    android:id="@+id/branch_commit_hash"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/colorWhite"
    android:textSize="16sp"
    android:textColorLink="@color/lightBlue" />

String names

All the string names in strings.xml must be meaningful. Also check for the string if it is already defined.

<string name="mergePRSuccessMsg">Pull Request was merged successfully</string>
<string name="mergePR404ErrorMsg">Pull Request is not available for merge</string>

Drawables/Icons

Most of the icons used in GitNex are taken from open source project Lucide icons. Please download an appropriate icon from Lucide site and use the AS tool to upload it to drawable directory. e.g: Right click on drawable directory in the project and choose New -> Vector Asset and select local file.

  1. No gif/png etc are allowed.
  2. All drawables must be SVG.

Also check Tabler icons for a comprehensive list.

Standard Variable Names

App Context should be stored at the beggining once in appCtx. Current Context should be stored at the beggining once in ctx.

Contexts

appCtx example in fragment(change to getApplicationContext in activity):

private Context appCtx;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   appCtx = getContext();
}

ctx example:

final Context ctx = LoginActivity.this;

Static Variables

Static and standard reusable variables should be stored in Constants.java.