Skip to content
David Zafrani edited this page Mar 7, 2017 · 6 revisions

Code Formatting

The project has a code formatter configuration checked into git. Ideally, all new code should try to match the style of existing code in the project as closely as possible. You can run the code formatter to help with this.

Declare each extended class or interface on separate lines to improve readability.

public interface BaseOutput extends HelpOutput,
                                    FlowOutput,
                                    FakeOutput

The same one item per line rule applies for passing multiple parameters through constructors, methods, and using Builder patterns.

   new ErrorDialog.Builder().withTitle("Error connecting to wifi")
                            .withMessage("Please try again")
                            .withOperation("Pairing Sense")

   public ErrorDialog.Builder(String title,
                              String message,
                              String operation){ ... }

   public ErrorDialog buildErrorDialog(String title,
                                       String message,
                                       String operation){ ... }

Naming Conventions

  • The project currently does not follow Google style for member fields. All fields in the project are not prefixed with m. E.g. name, not mName.

  • Prefix when declaring final static variables:

Use Case Prefix Examples
Argument Bundle ARG ARG_SCAN_RESULT
SavedInstanceState Bundle KEY KEY_HAS_SENT_ACCESS_TOKEN
Intent Argument Bundle EXTRA EXTRA_SEND_ACCESS_TOKEN

Annotations

All new code should use the @NonNull and @Nullable annotations for method parameters and return values. Any fields in an object that can be null should be annotated with @Nullable. Non-null fields generally do not need to be annotated.

Class Member Visibility

When possible, prefer public final fields over a private field and getter pair. Single method interfaces should be implemented using lambdas unless a reference to the implementation of the interface (this) is required.

Android Specific Conventions

Activities

Control how to start an activity from various contexts by providing static getIntent() methods. Allows keeping static extra intent fields private too.

public static Intent getPairOnlyIntent(@NonNull final Context context) {
    final Intent intent = new Intent(context, OnboardingActivity.class);
    intent.putExtra(OnboardingActivity.EXTRA_START_CHECKPOINT, Constants.ONBOARDING_CHECKPOINT_SENSE);
    intent.putExtra(OnboardingActivity.EXTRA_PAIR_ONLY, true);
    return intent;
}

XML Layouts (Optional)

Use xmlns:tools="http://schemas.android.com/tools" to preview text or images with styles applied in Android Studio. It prevents accidentally leaving a placeholder that could be viewed in production.

Use Case property Examples
TextView text tools:text="@string/voice_rc_title"
ImageView src tools:src="@drawable/icon_settings_user
RecyclerView listitem tools:listitem="@layout/item_voice_command"
<ImageView
        android:id="@+id/item_voice_command_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:paddingEnd="@dimen/x2"
        tools:src="@drawable/icon_conditions_large"/>