📺 Develpreneur YouTube Episode

Video + transcript

The Value of Clean Code Part 3

2023-01-10 •Youtube

Detailed Notes

What is readable code?

Readable code is simply code that communicates its intent to the reader. It means that when you pull up some code or any type of software, (be it HTML, Java, C, .Net, whatever) that anyone can look at the code and you understand what the code is doing.

Benefits of using readable code

Easier for us to understand what the code is doing. The intent is for any developer to come in, open the code, look at the code and understand what’s going on. Clean and easy to read it make it easier to understand how the code works. When code is written cleanly, it beings to self-document the requirements. If done right anyone can come behind the coder, and easily make a code modification or fix a problem. Makes it easier for us to write testable code, like unit testing. Because if the code is written cleanly then the functionality of our functions, methods, etc… are easier to understand. Such that a tester should be able to identify how the code works, making it easier to test them. Additionally, some other benefits are taking existing documentation such as software documentation and requirement documentation, and taking pieces of this documentation and putting it in the code where it belongs. So, we know that this code goes with this functionality or this requirement.

What happens when we don’t write readable code?

Hard to understand. Complex code can be hard to read. Hard for other developers to follow the developer’s intent because it is not clear. No idea or understanding of how the code works. Typically, when we don’t write readable code, you’ll hear developers say, oh, the code’s unmaintainable it’s spaghetti code. We don’t know what’s going on. Those are signs that your code is not readable. It was not written well. It’s also very hard to make code modifications or fix a problem because the code is unreadable or unmanageable. You don’t know what’s going on or where to begin. Simply put, if you don’t know what’s going on with the code and you can’t read it, you can get stuck.

The same thing applies to testing. If the code is unreadable, you don’t know where to test it. You don’t know how to test or where to test a feature in the software.

There are essentially four ways you can write readable code, but really there are five.

Documentation

Begin with some clear type of documentation. These can be either software requirements, specification documents, some type of test plan, or some other type of business requirements. Essentially, you want some type of documentation around the software that you are going to be writing or changing. So, documentation is really key for writing well-documented readable code.

Coding Standards

Coding standards define how we are going to write our code. Such as, what are the different requirements based on things like our language, environment, architecture, or other significant coding considerations that we should follow. Especially, when we bring in a new developer, making it easier for the new developer to jump in knowing how to write the code, and what to expect when they’re looking at the code. Because everyone is following the same template as they’re writing their code.

Style Guides

Style guides are the way you should organize your code, name variables, name methods, and define classes. Anything that pertains to your language’s basic coding standards, basic style guides, and file naming conventions. For example, in Java, there are specific industry standards around how you name your classes, the way you name your methods or your functions, or the way you name your CSS files.

Code reviews

Code reviews, help in a couple of different ways. One, it helps maintain readability because if someone else reviews it and they can’t understand it, or they can’t read it or they can’t follow it, then it probably needs to go back and be refactored and cleaned up so that it can be understandable.

This also helps flush out any potential logical problems or potential bugs that could be introduced into an application. Something that maybe the tester or the developer forgot to test, or they didn’t think about a particular edge case. These are things that you can look at with or catch what code reviews and code reviews are also a good place to help make ensure that you’re following those coding standards and style guides.

Practice

Finally, we have practice. Unfortunately, this is not something that can be easily done overnight. It takes a lot of time and a lot of practice to get good at writing readable code. Typically, a lot of students come out of college following the examples and derivatives that they were taught in school. They will have one mindset of what coding standards or coding styles are based on whoever taught them.

Transcript Text
thank you
[Music]
and then if we wanted to we could add
some additional syntax
to make it easier
we could say this
there's a couple different ways you
could do this readability wise it's
easier to do sys out on multiple lines
sometimes it's easier to do line breaks
again this just depends on your coding
standards
calculator application
of number
operation
right number
an if float number
for decimal value
operation
does it needs math
operator
a value
of Plus
minus divide
or multiply
and right number
an eight float number or decimal
so just starting there
I have one other thing I have to do so
if we were to run this instead of
getting index out of bounds we would
actually get this message written now
now in order for me to test that I do
need to update one thing here so we did
rename the application from example one
so we need to change this to be our
calculator
[Music]
I need to name it the name of our class
so calculator location
you know calculator application
we need to make our variable a little
more uh readable uh ex makes no sense so
we know we want to reference the methods
in calculator application so really we
should just call it calculator
application uh calculate or application
so we know the method or we know the
class that we're calling
we know argument zero and argument two
are going to be left and right numbers
so we'll say 11 number
right number
uh argument one so one of the things we
can do is we can also do this so we can
pick in the values we can say spring
operator
to be clear
so we'll take operator
and we will set it equal to
args one
so now as we do our case statement we do
our switch on operator so if operator is
plus minus divide or multiplication we
know what methods we need to call so
then we're going to call calculator
application and instead of method one
we're going to do add two numbers
together
and instead of art zero and one we will
do the left number
and right now
now the beauty of this is if you've
named your application and your methods
clearly
especially your parameters if I were to
just
Say Hey I want to do uh this is where
the code completion comes in handy so I
can take calculator application add to
numbers and it will automatically pull
in the syntax from our methods so we
have the variable name left number and
right number so if we actually name
things the same name our our local local
variables the same everything translates
together everything is readable it flows
so we have this
so we have calculate application again
dot this is going to be a subtraction
calculator method
Dot slide
and then calculator method
or calculator application sorry multiply
and then last but not least so not a
valid
operator
so let me maximize this for readability
so we'll come back through here so we
have our document description for the
application it's going to be a
calculator application it's going to
have functions to simulate buttons on
the calculator right application was
started written by me uh started today
version one
our application name our method names
are very clear so we simulate Plus on a
calculator add two methods return the
results
our description add two numbers together
so we get two numbers we add them
together and we return the results of
those two numbers
same for mult uh for minus
divide and multiply now we'll get to the
rules in a minute but just follow me so
far
So based on how we've renamed this
everything is readable so even if we
were to delete our javadocs
the code is pretty straightforward it's
pretty readable pretty understandable so
if you come down here to the main method
now so even if we didn't have the
javadoc here just because we added this
method here or this is out this is
essentially that document
so if I try to run this as is
so now
we get our application is going to
accept three values simulate two numbers
and a math symbol being entered into a
calculator command calculate application
left number operator right number and it
tells you what needs to be passed in
because we didn't it through the
exception now we can handle that
exception
by simply returning
adding a return here
return
system
exit
zero
actually just do system.exit zero
system.exit zero exits the application
cleanly so now if we run this
we get our
message and we don't get the exception
but
um what we need to say is
um
I'm able to run command
syntax
and you can name this however you want
but just
um
you want to make it as descriptive as as
possible so applications kind of takes
up three values simulate two numbers
unable to run commands syntax should be
uh calculated application left operator
right number and then here are our
values
all right so we have our method we can
run it so if we actually get Pat get the
right values we should calculate the
operations
all right so to do that we need to go
into our run configuration
and set up some arguments so let's add
one plus one
and run
we get 2.0
if we run again
one minus one
we get zero
if we run again
1 divided by one
we get one and so on so as you can see
we can now run the application now to
thoroughly test the application we
should add some unit testing or other
test classes essentially that do what
simulate what I just did
um but other than that this is pretty
much how we self-document our code so
now if I look at here I can look at the
outline so I have my calculator
application I have my main I have add
two numbers divide two numbers multiply
subtract
uh if I were to
uh
way I can build the
Java Docs
there we are generate Java Docs
destination yep
Ducks finish yes to all
so it just generated the Java docs so if
I come out here
here's my documentation so if I open
with
web browser
so we have our clean Java docs or
whatever tool you use to generate docs
we have our package name we have our
project so we have our class calculator
application
brief description very clean easy to
understand go into the calculator
application so we calculate applications
uh have function to simulate buttons on
the calculator
since version author
here's our Constructors
we have our methods so main our
application is going to accept three
values
methods inherent from class
we have our Constructor
and we have our methods
oh that's what I
samples
when you generate Java docs you need to
include
there we go
there we go
so when you generate your javadocs you
can generate the ones for package
private static public because I did not
actually declare these as public they
were a class or package level
so when I regenerate it now we get our
additional method so there's add two
numbers divide two numbers multiply and
then here's our documentation
so to write readable code you can as you
can see basically it just means writing
code in plain English uh or whatever
language you're in you basically take
the what
the application or the program is
supposed to do and break it down into
its individual parts and then document
it accordingly or write your methods uh
variable names or even the calculations
in such a way that if you just read the
code you understand what that particular
piece of code is doing otherwise if you
start out with the initial example I had
it looks like spaghetti code very hard
to follow not real easy to test and as
you saw we really didn't even know what
the application was doing just by trying
to run it foreign
so let's go back over to the slides
so does anyone have any questions
is this how a developers when they bring
in a developer and they tell him that
hey please clean up this code is this
what they'll be doing like pretty much
going through each and every method to
see how can they make it simpler maybe
for the next developer or somebody
uh depending upon what they're actually
asking them to do yes
um it could be twofold one cleanup the
code could mean uh that you have very
complex methods uh where the
functionality of the method uh or the
program itself could be doing multiple
things so for instance uh say instead of
doing all the individual methods for uh
add divide multiply subtract maybe I had
a method that just said
um calculate numbers
and you passed in uh all three variables
uh float string float
but you really didn't know what
functionality really happened inside
that method but inside that method you
basically had all of this info uh all
this complex calculation so you had uh
oh okay so if it's left number do this
if it's or plus do this minus do this so
like this could have like all this code
could have been in one method so instead
of the system.outs where I call the
method all that functionality could be
in one method or program
and a lot of times when you're asked to
clean up code or refactor code what
you're doing is you're taking all these
complex uh
basically pieces of code which are
actually smaller pieces of code and you
break them down into their smaller uh
components like multiply divide
[Music]
Transcript Segments
10.7

thank you

18.89

[Music]

28.619

and then if we wanted to we could add

31.199

some additional syntax

34.92

to make it easier

44.34

we could say this

55.68

there's a couple different ways you

57.18

could do this readability wise it's

59.039

easier to do sys out on multiple lines

62.219

sometimes it's easier to do line breaks

66.14

again this just depends on your coding

68.939

standards

99.54

calculator application

106.92

of number

108.54

operation

111.24

right number

125.579

an if float number

128.58

for decimal value

139.68

operation

142.02

does it needs math

144.42

operator

151.98

a value

154.319

of Plus

156.84

minus divide

159.84

or multiply

162.3

and right number

165.42

an eight float number or decimal

168.48

so just starting there

171.66

I have one other thing I have to do so

173.819

if we were to run this instead of

175.739

getting index out of bounds we would

177.9

actually get this message written now

181.26

now in order for me to test that I do

184.019

need to update one thing here so we did

186.36

rename the application from example one

188.34

so we need to change this to be our

191.099

calculator

197.99

[Music]

205.739

I need to name it the name of our class

207.659

so calculator location

211.68

you know calculator application

215.34

we need to make our variable a little

218.459

more uh readable uh ex makes no sense so

222.9

we know we want to reference the methods

224.94

in calculator application so really we

227.76

should just call it calculator

230.099

application uh calculate or application

234.42

so we know the method or we know the

237.239

class that we're calling

239.159

we know argument zero and argument two

242.4

are going to be left and right numbers

244.98

so we'll say 11 number

248.64

right number

252.42

uh argument one so one of the things we

254.939

can do is we can also do this so we can

257.04

pick in the values we can say spring

264.78

operator

280.38

to be clear

282.6

so we'll take operator

285

and we will set it equal to

287.78

args one

291.3

so now as we do our case statement we do

294.419

our switch on operator so if operator is

297.54

plus minus divide or multiplication we

300.9

know what methods we need to call so

303.18

then we're going to call calculator

304.56

application and instead of method one

307.62

we're going to do add two numbers

310.199

together

311.94

and instead of art zero and one we will

314.34

do the left number

317.04

and right now

320.58

now the beauty of this is if you've

323.52

named your application and your methods

326.16

clearly

327.6

especially your parameters if I were to

330.3

just

331.44

Say Hey I want to do uh this is where

334.86

the code completion comes in handy so I

336.84

can take calculator application add to

339.419

numbers and it will automatically pull

342.3

in the syntax from our methods so we

345.78

have the variable name left number and

347.82

right number so if we actually name

350.1

things the same name our our local local

354.36

variables the same everything translates

357.36

together everything is readable it flows

360.66

so we have this

362.46

so we have calculate application again

367.8

dot this is going to be a subtraction

377.82

calculator method

380.94

Dot slide

387.479

and then calculator method

390.3

or calculator application sorry multiply

396.24

and then last but not least so not a

399.36

valid

400.38

operator

405

so let me maximize this for readability

409.8

so we'll come back through here so we

411.96

have our document description for the

414.24

application it's going to be a

415.62

calculator application it's going to

417.84

have functions to simulate buttons on

419.34

the calculator right application was

421.8

started written by me uh started today

424.56

version one

426.6

our application name our method names

429.86

are very clear so we simulate Plus on a

433.56

calculator add two methods return the

435.66

results

436.699

our description add two numbers together

438.96

so we get two numbers we add them

441.18

together and we return the results of

443.819

those two numbers

445.199

same for mult uh for minus

447.599

divide and multiply now we'll get to the

450.96

rules in a minute but just follow me so

453.78

far

454.74

So based on how we've renamed this

456.9

everything is readable so even if we

459.419

were to delete our javadocs

462

the code is pretty straightforward it's

463.86

pretty readable pretty understandable so

466.259

if you come down here to the main method

467.94

now so even if we didn't have the

471.36

javadoc here just because we added this

474.72

method here or this is out this is

477.78

essentially that document

480.96

so if I try to run this as is

486.66

so now

488.94

we get our application is going to

490.919

accept three values simulate two numbers

493.08

and a math symbol being entered into a

495.78

calculator command calculate application

498.78

left number operator right number and it

501.3

tells you what needs to be passed in

502.919

because we didn't it through the

505.44

exception now we can handle that

508.199

exception

509.28

by simply returning

512.219

adding a return here

515.399

return

520.74

system

522.479

exit

524.58

zero

526.2

actually just do system.exit zero

530.66

system.exit zero exits the application

533.04

cleanly so now if we run this

535.68

we get our

537.48

message and we don't get the exception

541.08

but

542.339

um what we need to say is

544.62

um

549.6

I'm able to run command

565.14

syntax

570.42

and you can name this however you want

571.86

but just

573.3

um

575.339

you want to make it as descriptive as as

577.44

possible so applications kind of takes

580.26

up three values simulate two numbers

582.12

unable to run commands syntax should be

584.959

uh calculated application left operator

588.24

right number and then here are our

591

values

607.26

all right so we have our method we can

609.839

run it so if we actually get Pat get the

613.26

right values we should calculate the

615.6

operations

616.92

all right so to do that we need to go

619.32

into our run configuration

622.8

and set up some arguments so let's add

625.2

one plus one

628.68

and run

630.24

we get 2.0

632.279

if we run again

637.8

one minus one

642.54

we get zero

644.279

if we run again

652.5

1 divided by one

656.16

we get one and so on so as you can see

659.94

we can now run the application now to

662.94

thoroughly test the application we

665.16

should add some unit testing or other

667.8

test classes essentially that do what

671.04

simulate what I just did

673.62

um but other than that this is pretty

676.079

much how we self-document our code so

678.42

now if I look at here I can look at the

681.24

outline so I have my calculator

682.8

application I have my main I have add

686.1

two numbers divide two numbers multiply

688.32

subtract

689.579

uh if I were to

693.42

uh

695.279

way I can build the

699

Java Docs

713.22

there we are generate Java Docs

718.079

destination yep

722.399

Ducks finish yes to all

728.399

so it just generated the Java docs so if

731.399

I come out here

732.779

here's my documentation so if I open

735.72

with

736.92

web browser

738.839

so we have our clean Java docs or

741.6

whatever tool you use to generate docs

744.14

we have our package name we have our

748.74

project so we have our class calculator

750.959

application

752.579

brief description very clean easy to

755.399

understand go into the calculator

757.32

application so we calculate applications

760.7

uh have function to simulate buttons on

763.32

the calculator

765.779

since version author

769.2

here's our Constructors

771.72

we have our methods so main our

774.62

application is going to accept three

777.36

values

780.6

methods inherent from class

783.48

we have our Constructor

786.42

and we have our methods

797.279

oh that's what I

809.1

samples

811.86

when you generate Java docs you need to

814.62

include

845.579

there we go

856.44

there we go

858.899

so when you generate your javadocs you

861.12

can generate the ones for package

862.68

private static public because I did not

866.519

actually declare these as public they

868.68

were a class or package level

871.92

so when I regenerate it now we get our

874.019

additional method so there's add two

875.639

numbers divide two numbers multiply and

878.16

then here's our documentation

879.959

so to write readable code you can as you

884.399

can see basically it just means writing

886.26

code in plain English uh or whatever

888.72

language you're in you basically take

891.3

the what

893.399

the application or the program is

895.32

supposed to do and break it down into

897.66

its individual parts and then document

900.959

it accordingly or write your methods uh

905.04

variable names or even the calculations

908.339

in such a way that if you just read the

910.8

code you understand what that particular

913.62

piece of code is doing otherwise if you

916.38

start out with the initial example I had

918.48

it looks like spaghetti code very hard

920.76

to follow not real easy to test and as

924.48

you saw we really didn't even know what

925.86

the application was doing just by trying

927.42

to run it foreign

930.06

so let's go back over to the slides

936.06

so does anyone have any questions

942.72

is this how a developers when they bring

945.66

in a developer and they tell him that

947.04

hey please clean up this code is this

948.72

what they'll be doing like pretty much

950.699

going through each and every method to

953.16

see how can they make it simpler maybe

955.44

for the next developer or somebody

957.839

uh depending upon what they're actually

960.36

asking them to do yes

963.6

um it could be twofold one cleanup the

965.94

code could mean uh that you have very

968.579

complex methods uh where the

971.16

functionality of the method uh or the

973.68

program itself could be doing multiple

976.139

things so for instance uh say instead of

980.76

doing all the individual methods for uh

985.199

add divide multiply subtract maybe I had

987.839

a method that just said

990.06

um calculate numbers

992.579

and you passed in uh all three variables

996.959

uh float string float

1000.74

but you really didn't know what

1002.42

functionality really happened inside

1003.98

that method but inside that method you

1006.56

basically had all of this info uh all

1009.199

this complex calculation so you had uh

1012.019

oh okay so if it's left number do this

1014.18

if it's or plus do this minus do this so

1018.019

like this could have like all this code

1020.66

could have been in one method so instead

1023.42

of the system.outs where I call the

1025.28

method all that functionality could be

1027.439

in one method or program

1030.26

and a lot of times when you're asked to

1032.66

clean up code or refactor code what

1034.64

you're doing is you're taking all these

1036.679

complex uh

1039.26

basically pieces of code which are

1041.36

actually smaller pieces of code and you

1043.52

break them down into their smaller uh

1045.62

components like multiply divide

1048.48

[Music]