0:00
hey everyone I'm Asho to Singh tech lead at CTStech.com uh I have a 9 year of
0:06
experience and yes I'm the person in my team who always uh consider to just uh
0:12
debug the code and uh uh enjoy that uh doing that legacy refactoring code so uh
0:19
today uh what we are going to learn about that we have that kind of things like such as pleasure to uh let's be
0:27
today what we going to do it uh we will going to learn that some of the key point which we are going to understand
0:33
like how we writing the code how we going to be write a good code uh and what good code means all about so the
0:40
agenda it's it's like that if we say this way like to be honest everyone can
0:46
write a code good code but the code can be work but when it comes in a terms like where the code is perfect where the
0:53
course is efficient and clean and elegant to uh write it's matter a lot so
1:00
uh the agenda today we going to learn is how best practice to impact the readability and maintainability of the
1:06
performance real world strategy and write the code or function behavior second uh thing that we're going to
1:13
learn here my slide is not moving I think
1:22
mhm is there any problem my slide is not moving i see you are on the why clean
1:29
and efficient code matters i see you on that slide i can we can see your slides
1:42
okay yeah now it's now it's reflecting yeah so the first questions comes in your mind uh when we see that why clean
1:49
code efficient code matter so you know uh that feeling when you are returning
1:54
from your six month back from your projects and you see some of your own written code and you feel who wrote this
2:00
kind of garbage it's it's nothing else written it's you so this is that why we have uh
2:08
that good quality code and efficient code matters to write that good quality means of maintainability performance and
2:15
robust code okay so the next uh there are some
2:22
some kind of solid principle we have uh when we comes to the understand the writing the good quality code there are
2:28
kind of some principle we have which help you to out to write a good quality code and that principle help a lot while
2:36
you are writing a code it's tell you that what point what kind of things you have to do to achieve these goal so
2:44
first things in that matters comes called solid principle and each and every word of the solid stand for one
2:52
single things so when we say s S for the first thing is called single responsibility principle single
2:58
responsibility principle that means if you're writing something if you're creating a class if you're creating any
3:04
method that class and method basically it's have only one purpose to do when I
3:11
say only one purpose to do that means of if you are creating a class for saving anything if you're creating a class to
3:18
update anything it has only one goal to do nothing else so if you were comparing
3:24
with the real world example that means of like we have a uh in our company we have a different different department
3:29
I'm a IT guy and we daily we interact with that HR guy so uh it guy only write
3:36
a code i cannot run that uh uh pay roles which uh help to just generate our
3:42
salary and other things but but same way if you think in different way the HR guy
3:47
cannot run that uh write a good quality code if you force them to write a code
3:53
they can achieve the writing the code but and it might be it will work but doesn't mean that that code is having a
3:59
good quality the second things we have when we writing a code uh we have to measure
4:05
things like we have to follow that open close principle which we say that software should always open to extend
4:12
but all but close the modification that means of when we when you writing uh
4:17
your code should be open for the extension but closed for the text modification that mean the feature you
4:23
are adding or modifying it will not going to impact that existing feature
4:28
okay that means of the the way we have a discount like uh every day that most of the services uh clients website are
4:36
throwing the discount maybe in a different different way like we had a Christmas sales offer
4:43
we got Diwali sales offer so one discount type we are giving that means
4:49
not we are not at same time we are not going to give any other discount so we are adding the feature we are not
4:54
modifying that uh we are not modifying the ejecting feature so the third we have that list substitution it's kind of
5:01
like you should always replace the parent object with the child object child object that means like
5:08
sometime what happening that we have some some uh things that we have the similar behavior but the work culture is
5:15
different like we have if you going to create a one class which is a one class
5:21
which is going to be a your bird class bird class that means always think that blood bird can be a fly But there are
5:28
some bird we have which they cannot fly like a penguin so if you are going to implement forcefully the fly method for
5:36
that in a uh for that penguin it will break your applications so always try to
5:43
segregate your uh bird bird it mean the flying and non-flying in between the non
5:50
as for the behavior wise instead of creating one interface create a different two different interface which will help to write and segregate both
5:57
between a flying bird and non-flying bird third when fourth we are saying that interface segregation interface
6:05
segregation is very important for writing a good quality code it it helps a lot it's it's not only helping to just
6:13
uh making that entity different to different like integrating that one entity from another entity and breaking
6:19
down your application in small small unit it's also help to decoupling your
6:25
code so interface segregation stand for like like whenever you creating any
6:30
application when you create writing any uh interface you need to first think and
6:37
come to the conclusion what what kind of things like what what exactly you want to create before writing a interface
6:46
to come for the push create a small small small interface for one single
6:51
responsibility and just go for that like helper uh as Earlier I told like in uh
6:57
some if you take the example we have a I savable and I readable that means if uh
7:04
if I am creating a interface for uh I savable that means it's have only one job to save the data nothing uh do
7:12
nothing related to that update it will not touch that update not not going to do the job for the deleting or anything
7:18
else third we have a fourth we have a dependency inversion highle module should not be depend on the low-level
7:24
module that means instead of creating a one interface which will going to be
7:30
depend on another interface just create a using a interface to decouple the
7:35
classes decouple the classes that means of if you same way if you're going to take the example of listq there is a one
7:41
bird class we have which can be fly one some bird cannot be fly instead of creating one interface bird interface
7:48
create two different interface called uh flying interface and non non-fly bird
7:54
interfaces moving to the next uh the earlier we have seen those are the
8:00
principle that we have followed to when we thought about the writing a good and clean code that's a very important thing
8:07
while you comes to write a good quality code but apart from that there are lots of thing that we have to consider to
8:14
write a good quality code and when we comes to this point there are multiple things we have to follow even these uh
8:21
these thing are smaller but it's create a very very well impact on your code quality so first thing we have a
8:28
meaningful name meaningful name is very important when you're writing a code and
8:35
I feel uh in my career I have around 5 years uh 9 years of experience and I met
8:40
around 100 of uh more than 100 developers and I work for more than 15
8:46
15 to 20 uh uh pro teams so when I meet
8:52
the developer I I got only one problem with them when they try to write a uh
8:58
method name class name or variable name then they not give the proper time they're not going to ask that why I'm
9:04
writing the class why I'm creating this method if you are if you question yourself like why you going to write
9:10
this why this method required and why this variable required you will get that
9:16
answer of uh that question and that will be going to be a name of your
9:21
variable and everyone just writing the code like naming can anything like someone write that ABC get data and data
9:29
that will not going to help because what going to be happen in future if someone comes after four to five month and if
9:36
you try to read your code you then you will realize that you need to debug your entire code because your code is not
9:43
speaking on behalf of yours so whenever you're creating a variable give a clear
9:49
descriptive name of variable function classes and avoid sort confusing name
9:54
that make the code harder to understand second we have a sort functions some of
10:00
the time when when we see the code you often going to see we have a one sort
10:07
one function which have around more than more than 100 line of code and that's
10:13
not going to be good even if you're trying to write a unit test cases for those function it will not help you a
10:18
lot so try to create a small function small function and if you feel that the
10:24
function have a much much line of code just create a one more function and call
10:29
that function from the existing functions so create a sortter function write a function that do one thing at a
10:36
one time and do it well then consistency consistency style that means of whatever
10:44
that uh coding style or naming style you are following in that project follow for
10:49
entire project not keep for one uh class and uh another class will going to be
10:54
different coding style so it keep your code clean uniform and easier to
11:00
maintain avoid deep nesting when I say deep nesting that means of some of the
11:05
time what we have seen that like you are going to write a uh if condition within a if condition write a loop within a
11:12
loop it it is it uh create unnecessary looping and keep your code uh not clean
11:18
it will dependent on one if to another if so try to keep code by reducing keeps
11:24
nesting use early whenever you try to just filter out some data you try to
11:29
just if data going to be nulls what I have to return use always try to uh write a code which early
11:36
returns commenting code commenting code nowadays that most of developer follows
11:41
but some of the developer not following because commenting code will help a lot when you come and or someone uh after
11:47
you going to uh check your code or trying to modify something the comment that you have written on the top of the
11:53
class or on top of the method it help a lot it will get the clear picture for what reason they have right you have
12:00
created this class or method proper documentation nowadays very less people
12:05
are following because I have seen couple of companies where we got the project we
12:10
when we try to understand what what the project all about and why we have created these things we don't have any
12:18
documentary even we don't have the document to build that project so always try to create a proper document there
12:25
uh files in a do in your visual studio you can create a dot called readmi.mmd
12:32
file it's well known proper docu for the documentation purpose next slide when we say uh comes
12:39
on that we have seen the principles we have seen that how to create the variables functions and other things
12:45
there comes one more things that each and every developer mostly the early developer that beginners are do these
12:52
kind of mistake it's called uh efficient memory measure object object so uh when
12:57
we creating a object always try to avoid creating object unnecessary especially
13:02
inside the loop so if you see my code here we have uh what what you can see
13:08
that that user tried to just uh just run this loop at a thousand time
13:15
and what they are doing that they just appending the text into the builder string builder but the string builder
13:21
they have created into inside that for each uh for for loop so what going to be
13:27
happen every every iteration it going to be create a new object that is a bad
13:32
habit because what what it be have is doing here it's why why they have
13:38
creating here because of that cleaning the method before adding appending the
13:44
index here it need to be clear you can achieve that by this way here if you see
13:49
I have created this string builder at the top of your uh for loop and then I
13:55
have writed a report that before adding appending that index I'm clearing the clearing my uh uh string builder object
14:03
so this is how what going to be happen there is a one copy of object getting created and here it will creating more
14:09
than a thousand object around thousand object so avoid these things second
14:16
whatever the object that you are creating try to dispose that object so how you can uh dispose there are two way
14:23
you can dispose the object either you can using uh try to use using block
14:28
which is look like this whatever that operation you're going to do try to use for the using block so the life cycle of
14:35
your object will remains within that within that uh uh using block and second
14:42
you can also use that I disposable interface to dispose your object
14:50
uh string builder string builder that string builder is connection uh concenate for that using so I have we
14:56
have not not me only that even that we are on that early stage uh I also used
15:02
to follow whenever it comes to concentrate the string I used to follow only one thing I create the string
15:09
variable and then I start with the same way the result plus equal to the data so
15:15
this is the bad practice to do instead of that you can create a string builder string builder does the
15:22
same thing and it will create a less memory uses so it will make your
15:28
application faster third thing the collections
15:34
always use the proper collection for the proper region if you want to create a uh
15:41
if you want to create a order item access by the index go for your list of
15:47
creation if you have a dictionary fast key based lookup go for your dictionary
15:53
object if if you feel there are something uh you going to have where
15:59
that ensure that unique item go for the hash set so see uh here if you see that
16:05
we what I have done that I have created a new uh list object where I put that
16:10
number of numbers 1 2 3 and you if you want to retrieve that you can go with
16:16
that indexing by and you get the number back same way you can create that dictionary where going to be a key pair
16:22
combinations key will be a your id based on that you can get the data back same
16:28
uh in h has set you can you can keep a you can uh store a unique value if
16:33
you're going to try to store a duplicate value it will not allow you so the next part we comes to
16:41
unsynchronous programming so uh when we talk about that clean code and everything we know there are multiple
16:48
component that we need to follow first we need to know that exact principle which allow you the decoupling coding
16:55
which allow you to what need to be do at what level then we understand like uh
17:00
how we going to write a code what naming convention we have to follow how we going to what kind of function that we
17:06
have to create and everything and the apart from this there are a couple of thing that we need to follow which will
17:13
help you to improve your application performance which will help you to u kind of uh make your code readable so
17:22
that's come unsynchronous programming async awaited when you using async
17:27
awaited uh async aavitted remember to use runto toss it it uh why we use the
17:34
run toss CPU for the CPU utilization it not going to block your main thread so
17:41
many time what we are does that we are just adding the for the hu work we do one thing we just write a async weight
17:47
and we feel that this will this will solve our problems But not it will block
17:53
your main thread it will wait to your task getting over so until you are just writing a single wait in and sit back
18:00
write a run the task so it your task going to be run in a different thread and it will not your uh block your main
18:08
thread this way uh so uh so how you going to write it so you can write this
18:13
way like uh await task run and again we have a avoid deadlock
18:19
it's important to avoid deadlock in asynchronous programming by following that appropriate practice use
18:26
asynchronated the the way uh the thing that we have discussed to avoid that main thread blocking always avoid to use
18:35
await and dot result in async calls if you write a dot aait or dotreel your
18:41
thread going to be a block until that you your main method got that result
18:47
back the third we have run heavy task in a background use the run task.r run
18:52
method always if you have a heavy task like a some of the time when we have the refresh kind of page where we on the
18:59
click on the reference the heavy of the data get pull out from the DB so if
19:04
you're not uh going to write a code that such a way like a task dot run in
19:10
different thread so what going to be happen if you click on the refresh button that your entire page going to be
19:16
inresponsive the user cannot click anywhere that means once the page got
19:21
inresponsive you losing your customer they will just close your window and go away so instead of that you can write a
19:29
code such way like it will run the task in a background once the data gets ready it will refresh for a second and load
19:36
the entire data third the configuration things whatever the in configuration you have
19:42
you call this uh get async configuration file to load your configurations
19:48
data after that there are a couple of thing that we write we always nowaday that everyone prefer to write the link u
19:55
query but in link query there are more of the thing that we need to follow and we need to avoid so always first thing
20:03
use always use in memory loops so always inable which will help you to keep the
20:08
memory low second avoid multiple enumeration select whatever you need so
20:15
some of the time what happening that user we don't uh uh write a code such way even though we need that username we
20:21
write all the we are returning everything that is not correct use select whatever you want be
20:30
a spec specific while you're selecting and fourth we have any when we say any
20:37
that means of like whenever we writing anything when we are creating anything and and some of the time what we feel we
20:44
have to check like uh we check that if something is null or not instead of that
20:49
or we are checking if that list have any data or not if you're checking that list have any data or not instead of that
20:56
check any list dot any that that will give you exact answer and be very
21:02
quicker experience uh so after that there are come kind of
21:08
things we have followed to make your application performance faster it's uh
21:14
called caching and performance boosting caching comes in a picture long
21:19
back how earlier we don't have such more things like a distributed caching so we
21:25
used to follow that in-memory caching inmemory caching that means how we store that local application cache in the
21:32
local applications which will only accessible for one application label that other application cannot access
21:38
that so uh in memory caching we have a local application fast access and use
21:43
single server caching other we have a distributed caching distributed caching there is a so many cache provided
21:50
available the most famous is radius so uh instead of storing the value into
21:56
your cache you can store the value in your radius cache and from there many
22:02
many application if you have a three application or three microservices the each one can directly link with that
22:09
cache and get the data back so it will it will allow it will help to not pull
22:15
or ping your database every time instead of making a request to the your database
22:22
to get the data back it will allow to get data from your cache if data is not
22:28
available in cach then it will redirect your request to get the data from database and insert it to the cache and
22:33
then return back to your controller so cach will really help to make your uh
22:38
application faster and this is a real way that I have followed that because uh most of that API when we are loading
22:45
that API getting the same data for same way like we have a master data which
22:50
going to be applicable for all you all user so instead of instead of just
22:57
calling that API and getting the data from back from that uh database every
23:03
time what we have done that we have using that uh you are storing those uh response into that cache so if some
23:10
users comes it not going to be hit my database it get the data from the cache and it increase improve that uh API
23:17
response time very very less like within the second within a
23:24
minute fourth like when we have discussed everything there is a one more
23:30
thing which comes in a picture called login and monitoring so even though you are done each and everything you
23:35
followed everything all the scenario all the principle all the naming convention you followed the garbage discretion you
23:42
followed that how you going to write a code how you going to write your link you you've done all the thing there will
23:48
be something which you need to add in your application for that monitoring purpose you need to know that like what
23:55
level your customer is reaching at what level your application getting break and you also got to know that like at what
24:02
function level at what class level your application is taking too much time to
24:07
give that response so here's the logging comes in a picture the structural
24:13
logging using the tool called silly log n logger these tool are very famous nowadays and it's having the JSON format
24:20
application for this uh uh analysis so these JSON file you can inte integrate
24:25
with so many uh uh application inside like if you using the Siri log you can
24:31
integrate those log u response with data log and there are so many uh things you
24:37
can do with that and you get the nice clean um UI where you can monitor each
24:43
and everything in a one window so always follow to log that data whatever data
24:49
you have try to keep it in there logging in
24:55
there we have uh session earlier that where we uh got the uh insight of how to
25:01
write that unit test cases and code review uh unit test cases is very important because whatever the changes
25:08
you have done whatever the code you have written this is the point where you can test it out this help you to test the
25:15
test your function what the small you know small code method you have created
25:21
so always try to write a good quality code which will which will help you to
25:26
uh test your code and ensure that whatever the code that you have written it's working fine it's returning the
25:32
variables whatever you expected from that method or classes and test ensure
25:37
readability code review catchb and improved stage so uh some of the
25:43
developers saying that code review while we doing the code review there are lots of comment coming it's because of that
25:50
uh the way you are writing a code if you write following the best practice to write a code that the reviewer can
25:56
understand your code code and it will approve your code so and while you're
26:03
reviewing the code it it give that sense if you're missing something reviewer can early catch that no this will going to
26:09
be happen this will create a problem in a future so always review a code
26:17
second thing uh the tool there are multiple tool that you each developer net developer should know it's called uh
26:24
first is dress use of dot find what should so
26:30
slow down your application so uh we have a so many times we write a code and we then later on we realize that our
26:37
application or function is not working as expected it's work returning the data
26:42
but it's taking too much time and uh when we when we uh comes like we we got
26:49
to ask like can you just decouple this and you let me know that at what function at what level uh it's getting
26:56
slow so uh we used to write a kind of like a stopwatch and other things which
27:03
help you to just uh give a um timing like start timing and end timing of your
27:09
method that is old things now we have a dot application uh tools which you can
27:15
add it to your application and it will give you a clean clear picture at what
27:21
label method and which method which function is taking how much time to get it executed properly it also help you to
27:28
check CPU uses and memory leakage you get to know that uh your application is
27:34
taking how much time to uh taking your CPU utilization and any memory leak is
27:41
happening or not pro provide clear graphs to see what slowing down your app
27:47
okay third second thing that we have second tool that we call the benchmark.net tool use of benchmark.net
27:54
Net tool to test how fast your code running and it's give the accurate
27:59
result to compare different method and it's very easy to use you just need to annotate your method with the benchmark
28:06
annotation and done and run your applications it will give you the pictures and it's also give the clear
28:12
and graphics picture which will help you to understand that what inside going on your
28:20
applications apart from this the we have a design patterns which help you to
28:25
write a decoupling code which help you to uh understand like what exactly your
28:31
requirement need so patterns pattern going to be decided based on your
28:36
application's need so we have a factory pattern also known as a smart object
28:41
creation we have a repository pattern that called clean data access and we have a clean architecture uh which
28:48
called layered scale design that we in today's session uh we earlier we uh uh
28:54
one of the we got that uh deep insight of clean architecture so I feel everyone
28:59
already aware of that so factory patterns it's called factory pattern is useful to creating object without
29:06
specifying that exact class if I say the real real world example it's kind of
29:12
like the way if you order something like uh if you think this way like Amazon
29:18
Amazon it's a factory okay we are placing the order i need a I need a one
29:25
uh toy so it it going to be what it going to do it will help you to get the
29:31
toy from that company so same way if you uh talk about the coding language if you
29:36
have a uh if you if you create a application where your application going to connect with a two different uh two
29:43
different cloud like uh uh some of the functions working b based on your azure
29:49
function azure cloud and some of the function depending on your AWS cloud so
29:55
instead of you creating a different different object for writing a code for the different different uh uh making a
30:01
connection connection with different different cloud you can create a factory pass a object and just tell that to your
30:08
master uh master class I need this I need this uh uh object of uh Azure cloud
30:15
it will make a connection with the Azure and give you object back and you do your job okay and it prompts loose coupling
30:22
to simplify object creation pattern implementing the pattern can provide a flexibility and change creation of logic
30:29
without modifying the client code also uh the second we have a
30:34
repository pattern that called clean database keep data access separate from the business lane this is a very
30:40
important and I feel now that every every developer following this logic okay so it it's it's basically doing the
30:48
things like you can decouple your uh data access and business logics differently it prompts your reusability
30:56
so this allow you to reuse your code in many way it's not depending you write to
31:01
again and write a code again and again and clean architecture layer it's called like a centralized communication between
31:08
the component reduce tight coupling and messing dependency improve flexibility to make the system easier to scale it's
31:16
a nowadays a very famous architecture that we follows clean architecture and
31:21
is going to be widely used sooner so that's it from my side thank