All Projects → balsikandar → Best-Coding-practices-in-android

balsikandar / Best-Coding-practices-in-android

Licence: Apache-2.0 License
This repo is to add best practices that developers can apply to write clean, short and testable code in android.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Best-Coding-practices-in-android

Bookmarks
🔖 +4.3K awesome resources for geeks and software crafters 🍺
Stars: ✭ 210 (+144.19%)
Mutual labels:  programming, best-practices, clean-code, articles
php-best-practices
What I consider the best practices for web and software development.
Stars: ✭ 60 (-30.23%)
Mutual labels:  best-practices, clean-code
State Of The Art Shitcode
💩State-of-the-art shitcode principles your project should follow to call it a proper shitcode
Stars: ✭ 2,207 (+2466.28%)
Mutual labels:  programming, best-practices
android-clean-code
Writing Clean Code in Android
Stars: ✭ 22 (-74.42%)
Mutual labels:  clean-code, android-development
Write Readable Javascript Code
📖 All about writing maintainable JavaScript
Stars: ✭ 244 (+183.72%)
Mutual labels:  best-practices, clean-code
Programming Book Recommendations List
My personal list of books that I recommend to read if you are a software developer
Stars: ✭ 22 (-74.42%)
Mutual labels:  programming, clean-code
tutorials as code
so that stuff read/seen don't get muddled up with time
Stars: ✭ 42 (-51.16%)
Mutual labels:  programming, articles
Run Aspnetcore
A starter kit for your next ASP.NET Core web application. Boilerplate for ASP.NET Core reference application, demonstrating a layered application architecture with applying Clean Architecture and DDD best practices. Download 100+ page eBook PDF from here ->
Stars: ✭ 227 (+163.95%)
Mutual labels:  best-practices, clean-code
theBookOfNoah
Everything ive learned developing web applications
Stars: ✭ 22 (-74.42%)
Mutual labels:  programming, best-practices
emperor-os
(new released v2.5 LTS.2022-06-25) It has focused on developing an All in One operating system for programming, designing and data science.Emperor-OS has over 500 apps and important tools
Stars: ✭ 32 (-62.79%)
Mutual labels:  programming, android-development
ResourceCollection
A collection of handy resources, as recommended by Pharap
Stars: ✭ 18 (-79.07%)
Mutual labels:  programming, articles
Awesome Android Complete Reference
Awesome Android references for everything like best practices, performance optimization, etc.
Stars: ✭ 2,701 (+3040.7%)
Mutual labels:  best-practices, android-development
Movies Kotlin Kata
Katas for practice Kotlin( Coroutines, dataclasses, delegate properties...) Clean Architecture and best practices in Android(DI, Dagger, MVP, Espresso) implemented by Jorge Sánchez (Xurxodev)
Stars: ✭ 240 (+179.07%)
Mutual labels:  best-practices, clean-code
Clean Code Javascript Tr
JavaScript için Uyarlanmış Temiz Kod Kavramları
Stars: ✭ 232 (+169.77%)
Mutual labels:  best-practices, clean-code
angular-clean-code
My personal best practices when I'm working with Angular.
Stars: ✭ 39 (-54.65%)
Mutual labels:  best-practices, clean-code
common-coding-conventions
A concise and universal guide to clear software design.
Stars: ✭ 306 (+255.81%)
Mutual labels:  best-practices, clean-code
Clean Code Java
Clean Code concepts adapted for Java. Based on @ryanmcdermott repository.
Stars: ✭ 155 (+80.23%)
Mutual labels:  best-practices, clean-code
Architecture
.NET 6, ASP.NET Core 6, Entity Framework Core 6, C# 10, Angular 13, Clean Code, SOLID, DDD.
Stars: ✭ 2,285 (+2556.98%)
Mutual labels:  best-practices, clean-code
blog
No description or website provided.
Stars: ✭ 33 (-61.63%)
Mutual labels:  programming, articles
clean-code-javascript
🛁 Clean Code-Konzepte adaptiert für JavaScript.
Stars: ✭ 37 (-56.98%)
Mutual labels:  best-practices, clean-code

This repo is to add best practices that developers can apply to write clean, short and testable code in android.

Android Weekly Open Source Love License

There is also an article related to this check here

Table of Contents

Writing clean code

Nested If's

I hate this, I seriously do, you have statements which require multiple checks like this below code and it goes so deep really deep, In coding which is bad actually.

if (vehicle != null) {
       if (vehicle.getCar() != null) {
           if (vehicle.getCar().getModel() != null) {
               int price = vehicle.getCar().getModel().getPrice();
           }

       }
   }

And the thing is, It can be avoided, you totally can, like this. As you see below one is more readable and easy to understand.

if (vehicle == null || vehicle.getCar() == null || vehicle.getCar().getModel() == null) return;
 


int price = vehicle.getCar().getModel().getPrice();

Cognitive complexity

Definition: It's a psychological characteristic or psychological variable that indicates how complex or simple is the frame and perceptual skill of a person.

In programming a method with nested if else's and larger size causes high cognitive complexity means less understandability. So better to split large methods into logically separated smaller ones and use above Nested If's trick to reduce it. Also SonarLint a static code analysis tool calculates this for you in realtime in android studio you can use sonar to see how you doing.

Region

Use regions to separate your code fragments in big classes like britisher's did with divide and rule policy, very effective ask indians.

//region meaningful name of your logically separated region
do your work here.
//endregion

Naming

Short names for short living variables and good and meaningful names for long living ones because they'll be with you for a long-long time. They are family.

For e.g. index variable within for loop can be 'i' but as class variable should be 'index'

Parameter Object pattern

There is no restriction to number of paramters passed in methods but it's a bad practice to pass more than 3 or 4, So if your methods contains a repeating group of parameters which are passed among several methods. We can avoid that by replacing these parameters with an object.

No comments

"Code never lies, comments sometimes do." - Ron Jeffries

  • Always try to explain yourself in code because you're a coder not a commenter.
  • Don't add obvious noise.
  • Add comments for Public APIs.
  • Use it where necessary as time passes by code will change comment wouldn't.
  • You're being paid for coding not commenting.

Use Abstract classes

Create abstract classes to implement only those callbacks which you need. As SimpleTabSelectedListener below, No need to add onTabReselected, onTabUnselected if you don't need it.

public abstract class SimpleTabSelectedListener implements TabLayout.OnTabSelectedListener {

   @Override
   public void onTabReselected(TabLayout.Tab tab) {
   }

   @Override
   public void onTabUnselected(TabLayout.Tab tab) {
   }
}

Must follow rules

Horizontal and vertical formatting rule

It's not a constraint but a formatting rule that should be followed to keep your code vertically and horizontally small so with just one glance you can read it all.

Boy Scout rule

Definition: Leave the campground cleaner than you found it

Consistency

If you do something in one way then do it the same way everywhere- Uncle Bob

Keep it simple rule

Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible - Uncle Bob

Must have Android tools

SonarLint

I recommend this, i have been using it and i got to know about this from a colleague, sometimes they can be helpful, kidding. It has several features best one to just scan the modified classes and it'll automatically criticise you for your bad code and how ugly you can be sometimes. BTW Cognitive complexity we talked earlier it helps.

FindBugs

It's a program that uses static code analysis to find bugs in java code just like SonarLint. To know more about FindBug check this. Flip a coin or whatever but pick one of these tools.

Codota

It's a great tool to help you with code segments that you might otherwise have to search on github and stackoverflow and it's totally available in your android studio.

Stupid mistakes by Developers

Not using parcelable

If you're not using it then you're making a mistake.

  • Parcelable is designed for android.
  • Parcelable is faster than serializable interface
  • Parcelable interface takes more time for implemetation compared to serializable interface but there are plugins to automate it.
  • Serializable interface is easier to implement
  • Serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  • Parcelable array can be pass via Intent in android

Directly accessing variables

If you're accessing another class variables using dot operator it'll become ugly with time. There is a reason why we have access modifiers keep your variables private and se getters to access those variables.

This'll reduce code coupling. People who code in activity and fragments might know what i'm talking about.

Creating too many class variables

If you still remember class as in OOPs concept class represents an entity a noun. A class has attributes which define it's states and methods it's behavior.

I have a totally different understanding from my experience I believe attributes are the real deal and methods they just work on it. So try to keep attributes which are relevant to class and trim those extra global variables(mostly booleans) you added for whatever reason.

Parameter object pattern can be of some help her.

Integer.valueOf

Integer.valueOf returns Integer object whereas Integer.parseInt returns primitive int. Avoid using Objects for primitive types they're not meant for it.

Looping to add elements to arraylist

People there is addAll method in ArrayList use it for adding another list.

Avoid overdraw

In nested layouts we usually set backgroundColor property to ViewGroups without realising that it'll result in overdraw. This reduces your layouts performance. In Developer Options setting you can enable Overdraw to pinpoint your bad layouts.

Some good articles

High performance layouts

Anko Layouts

  • XML layouts are somewhat slow and aren't typesafe or nullsafe
  • Whereas Anko Layouts are 400% faster than XML layouts.

Proteus

Uses json for layout creation ans as you'll know json parsing is faster than xml it improves layout performance.

The coding principles

SOLID

It's a mnemonic acronym that helps define the five basic object-oriented design principles:

  • Single Responsibility Principle
  • Open-Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

DRY principle

Never ever write same piece of code twice make it your ironclad rule and strictly prohibit this in your kingdom.

The Critics principle

Okay it's totally made up but it's very logical. When you're reviewing code of your teammates don't be a friend, Be their arch enemy, don't let them make mistakes that you might have to clean someday. Cleaning other's shit will make your hand dirty. Enforce good practices in code reviews.

Must have Android Libraries

Android Debug Database

Android Debug Database allows you to view databases and shared preferences directly in your browser in a very simple way.

What can Android Debug Database do?

  • See all the databases.
  • See all the data in the shared preferences used in your application.
  • Run any sql query on the given database to update and delete your data.
  • Directly edit the database values.
  • Directly edit the shared preferences.
  • Directly add a row in the database.
  • Directly add a key-value in the shared preferences.
  • Delete database rows and shared preferences.
  • Search in your data.
  • Sort data.
  • Download database.
  • Debug Room inMemory database.

Robin

Robin is a logging library for Bundle data passed between Activities and fragments. It also provides a callback to send screen views of user visited pages to your analytics client

Android studio plugins

This is a list of all awesome and useful android studio plugins.

TODO

Anything that improves code quality, improves it performance.

Find this project useful? ❤️

  • Support it by clicking the button on the upper right of this page. ✌️

Contact - Let's connect and share knowledge

License

Copyright (C) 2017 Bal Sikandar
Copyright (C) 2011 Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].