Hey Guys, How’re you? Today I am gonna write on most important topic Best Coding Practices
I’ll Share 10 Most Important Points of Best Coding Practices which I learned during my coding journey till today and will be happy if you all can contribute to this article and can give valuable insights
Writing merely the Code and Writing it in a Efficient way, Both Are Different
Let’s start Point by Point from very basic and Believe me every points makes a difference
1. Choose Naming Convention Wisely
The idea is that “code should explain itself”
Naming Convention is the first thing comes into my mind when creating any file and also plays an important role in long term maintenance of a project, So if used properly it reduce the effort needed to read and understand source code.
Naming conventions result in improvements in terms of “four Cs”: communication, code integration, consistency and clarity
Always Remember that “There are only two hard things in Computer Science: cache invalidation and naming things.”
Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:
- To reduce the effort needed to read and understand source code
- To enable code reviews to focus on more important issues than arguing over syntax and naming standards
Example:- In Resources, IDs should be prefixed with the name of the element in lowercase underscore like for a header TextView naming should be tv_header, for image it should be image_
For Android Naming Convention You Can Refer these resources:-
2. Choose your App Architecture Wisely / Or Use Recommended Architecture
Currently It has become a hype to use latest architecture or whatever is in trend without choosing the architecture as per your project need!
I won’t say you use an old school But yes atleast give some time to think which architecture suits well to your project need
Now Why a Recommended architecture is required? Cause after adding so many features a point comes when you start facing some of these problems:
- Scalability i.e. Difficulties in the addition of new features
- Introduction of more bugs.
- Unit Testing becomes harder
We have many architectures like MVC, MVP, MVVM, MVI, Clean Architecture. and these above problems can be avoided by following the coding standard and following some well suited architecture in your Android project and if you are following the standard coding guidelines and keeping your code clean, no architecture is bad
3. Use Kotlin Scope Functions wisely
I’ve seen many devs just use scope functions (let, run, apply, also, with) cause it has become kotlin style But Beware! Don’t Rush, Use your tools wisely
Some of the Points to Consider:-
-> don’t forget to make it descriptive
-> Nesting scoped function is considered bad practice even Kotlin style guide point this out, use if for null check and then you’re good to use scope inside it
4. Use Kotlin lateinit wisely
`lateinit` modifier plays a very important role, we use this to initialize global variables to tell the compiler that this variable would be initalized late.
Also we usually use kotlin reflection operators :: to check if lateinit variable is initialized or not before accessing it and using reflection is a BIG NO!
Reflection is considered as bad practice and hack from starting
And since people have access to `lateinit` modifier, I must say don’t use it extensively cause they are more like a cache at the scope of activity/fragment which can be destroyed anytime, and keeping them cached can cause unnecessary memory leaks + other scope related issues which will create problem for your code
Hence use it as less as possible
5. Avoid Nesting of Layouts
Layouts are basically blueprints, we need to position the views in some particular fashion on the screen
Using nested instances of layout can lead to an excessively deep view hierarchy and this will have performance issues
Better Use Constraint Layout especially if you’re going to design complex deep hierarchy layout as it has its own benefits but when it comes to complex, dynamic and responsive views
Constraint Layout comes with some powerful tools with the help of which you can define complex layouts without having deep nesting. So it flattens the view hierarchy and gives better performance.
For more reference, use this — https://developer.android.com/training/improving-layouts/optimizing-layout
6. Get rid of memory leaks with Leak Canary
We’ve all been bitten by memory leaks that cause OutOfMemoryError crashes in our apps at some point, sometimes even in production.
Square’s Pierre-Yves Ricau has solved this problem by building LeakCanary, a tool to detect and help fix memory leaks before they ship.
7. Consider using SVGs or WebPs for your image drawables
WebP is an image format that is developed by Google, focuses mainly on optimization and quality.
WebP is lossless and lossy; means it supports both types of compression, unlike PNGs and JPGs.
Also Including multiple images for different resolutions increases the project size So Better is to use Vector Graphics such as SVG images or WebP which can help reducing app size
8. Use Proguard
ProGuard is a free java tool in Android which will remove all unused code, hence helps in reducing APK size.
It not only gives benefit in code shrinking but also provides code Obfuscation shortens the name of classes and members which makes the application difficult to reverse engineer
Study about Proguard here — https://developer.android.com/studio/build/shrink-code.html
9. Consider your build speed
Long build times slow down your development process
Follow these things atleast:-
-> Never use dynamic dependency like:-
Cause it takes Gradle to check for the new version in every 24 hours
-> Disable Crashlytics in development build as it makes no sense using it in development phase, it will also improve the build time
-> Learn how to use Build Types, Product Flavors, and Build Variants and use it for faster and easier development
For more information Refer this — https://developer.android.com/studio/build/optimize-your-build
Now Last And My Most Favourite, It make my coding more efficient 🤩
10. Use SonarLint in Project
Like a spell checker, SonarLint highlights coding issues on the fly, It provide feedback to developer about the code.
It identify the new bugs and the quality issues in your code
You can Install SonarLint plugin directly from your Android Studio
Open Android studio
File -> Settings -> Plugins -> then type sonarqube and click on Browse repositories at the bottom
It plays a good role in Improving Coding of Developer as well as helps all developers write better and safer code
There are lot more things which could be covered But for now I’ve given these Most important 10 things which you must do while coding
Best Rule To Apply While Coding
Never write code like you’re the Only Developer in Project, Write like there could be hundreds or thousands
“Any fool can write code a computer can understand, good programmer write code that human understand”
Whenever anyone see your code it should be self explanatory
and Most Importantly
Don’t miss Code Reviews!
Be your teammates arch enemy and put your efforts, Review is properly or Else You will be clearing it someday. Bring inner techie in yourself! Good Coding Practice is the Key
That’s all from my side!!
Also I must say you should not miss learning How to write JUnit Test cases in your android project, I’ve covered that here:- JUnit Testing in Android with Kotlin
Share this blog to spread the knowledge | Do Follow and CLAP if you Liked it! Happy Coding