📺 Develpreneur YouTube Episode

Video + transcript

The Value of Clean Code Part 4

2023-01-12 •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]
because I've actually seen applications
where this makes sense to pull this out
and do this but this code itself was
embedded inside of another method or
another class and if you refactor things
cleanly it's reusable the whole point of
object-oriented programming is to write
clean
reusable functional code so that you
don't reinvent the wheel every time you
do a new method new class new program
and if you break it down like this it's
very easy to understand what's going on
and able to follow the balancing ball
okay gotcha okay
this is uh as Michael had mentioned as
he started getting into this this
actually you could do a lot of
discussion around
documentation and that the approach that
he he's lined out is is one approach
um and it varies from organization
organization and even language language
because some kind of you know some
places it's
um it's things like it's entirely write
your code without comment so that there
are no comments it should be fairly it
should be readable based on
uh like function names and class names
and variable names and things like that
and some of it is
um even without having things quite as
as complicated like the way hit this
one's set up for example you know the
subtract two numbers and return the
result if you look at the the variable
names it's it's obvious that's what it
is I mean it's like you basically just
take left number minus right number and
it returns result so the code itself is
is very readable even with a very simple
um name
now some cases the names you know there
are some languages limit the names
um and some of them uh some
organizations are going to say hey we
want to keep it you know shorter rather
than longer code completion makes it so
that it doesn't that is less of an issue
uh like personally I prefer to have
stuff within the comments as opposed to
you know having the name basically
describe what it does it can be like a
summary but
um usually have more in the comments
versus uh you know having real long uh
variable names and things like that but
the key to the point for all that is
consistency and so he he mentioned
several times you know programming
standards development standards that is
probably the most important thing is
that whatever organization you're in is
that everybody does it consistently
which is where things like code reviews
are huge so for example if I if if this
is the the standard
and I go in and write it and I don't
follow the standard and I you know I've
got some x's and y's and some things
like that and some really short you know
cryptic names then in the code review
would be something where Mike would be
like hey you know according to code
standards this is how it should look so
you know your code doesn't fall as
standard let's get that to standard so
that when because that's the most
frustrating thing is when somebody comes
in and looks at the code and depending
on what class or what file or what
function even
it's completely different uh how it's
documented and of course the other thing
you have to watch out it for is just
make sure that it stays current you know
if you come in and you start changing
something around then you may need to
you know update the comments you may
need to make sure that you've got your
you know maybe you adjust a couple of
the uh the names
um so that's important and then one
other thing I want to point out that's
that sort of comes up as Michael was
going through that where he was looking
at
you know like where he's building out
like what this does and what it returns
uh particularly like the the divide by
zero that's something where it's
important for both testing and as you're
developing and so now it says hey you
know we can't divide by zero
so you should either you know have some
sort of exception that it throws or have
something that says okay if I get a zero
I'm going to send this message or return
this value and then make sure that your
code does that because from a testing
point of view that means
you know they're gonna they're gonna
come through and they should be able to
see uh from your comments from your
documentation how that's supposed to
work and then build that test and then
it better match results so if you say
it's going to return uh not a number
then when they come in if they do a zero
then it should come back not a number
otherwise you're gonna say hey this
failed testing so documentation not is
is a great way for you to be more
thorough in your development that's why
in some cases there's there's some
approaches to coding where you start by
creating comments you pseudocode
everything
and sort of write out the story of what
your class or your function is supposed
to do and then as you build in the code
you've sort of got like an outline that
says hey I got to do this I've got to do
that I've got to check for these things
and handle these exceptions I'm going to
return these kinds of messages so
there's
there's a lot of benefit that comes from
proper documentation that is this sort
of hidden you know there's a couple
things Michael highlighted but there's
there's some things that were just sort
of like
uh you know a part of that that when you
think about it like oh wow this is
really going to help us become better
developers doing it uh oh one last thing
I want to mention is that while this was
focused on Java and javadoc
uh just like unit testing the junit
we've mentioned in the past
those tools exist in some way form or
fashion in almost every language and if
you if you're not sure what it is like
you know if you're getting into
something new then if you just do a
Google or other search based on you know
hey what is the javadoc
equivalent in PHP
um or c-sharp or whatever it is then
you're gonna you're probably gonna get
some good results and yeah the
um the way that you do comments and
maybe the way that you uh take advantage
of that tool are going to vary but uh
you know those those tools pretty much
exist everywhere and it's worth doing a
little bit of upfront research to make
sure that whatever it is you're you're
working in that it um
that you take advantage of it and it I
guess the follow-up to that is things
like uh Minify and things like that and
some of the code compilation types of
things that work for for example with
JavaScript which allow you to have
really big complex names and all of that
and not be hurt by uh when you get to
run time because it basically goes
through and says I'm going to take all
these big complex very human readable
names I'm going to turn on something
that is just you know the machine can
read it a human's not going to get much
use out of it but then it's it's going
to run faster so
um you know there's some tools like that
whichever environment you're in it's
worth it to understand what tools are
there and as an organization or even as
an independent is to
I have a standard and stick to it and
hold yourself and your teammates to it
I'll step off my soapbox but there's
just like I said this is one of those
it's actually it's a very big uh topic
and although Michael did a great job and
you know in one session sort of like
saying hey here's here's some big
differences from where he started and
then where he ended up
um there's so many rabbit holes we could
go down talking about that
yep and I have planned on possibly a
future topics and discussions on that as
well but it good addition Rob uh also
you know you mentioned code review well
right here this is a good example of
what should be caught in a code review
if you document it so here I'm saying
what this method should be doing and I
say oh I'm done I commit the code under
code review it should fail because I did
not follow this condition I did not do
the divide by zero rules so this would
fail this should come back and then what
should have happened should have been
something more along the lines of okay
so if
right number
equals zero
it's either return
a number
um
or like throw
or
format exception or
null number or whatever but basically
you have to do something like this to
handle that uh like Rob was saying uh
the other thing that uh Rob mentioned
that I didn't touch on too deeply but if
you actually were to like I said remove
these comments you understand what the
code's doing but if you I haven't
written this and you're just writing it
for the beginning this makes sense to
just come in and write something write
the requirements for the method right
like pseudocode the method and then even
potentially uh after this so you do
something like this
and actually let's just uh do another
example real quick
um so if we were to do something like
this
um
simulate
the factorial on a calculator
and this is to uh
calculate the factorial
value of a number
thank you
now
you get a number so we could say float
we could say factorial
and we'll say float
and number
that's it all right so we don't have to
go too far into the depths except we
know we need to return something
so we know we need to return
a float
uh oh uh sorry wrong
all right so float
float
and we'll just do return
all right so this is just meeting the
bare minimum requirements for the method
and then in here we could essentially
kind of do some pseudo code or you could
kind of write your testing so you would
say something like for testing
you could do something inside of here
even but for like a unit test and this
kind of pushes a different topic but
this is kind of where you would write
your test driven code so you would write
the test cases first and then come back
and make sure that the code worked but
to test this it would be factorial
of say zero
would equal zero
um
Victoria one
two equal one
factorial two
would equal two
three
is going to be six
and then
for testing
basically factorial and factorial is
going to equal to n times n minus 1.
and so what you end up getting into is
oh well all right here is how we're
going to test this this is how the
function should work so you would
actually write this test case to expect
these values and then you would come
back in here and you would write the
code to do this so actually what you're
going to be doing here is if
number
equals zero
return
one
else
return
this one
zero
so now what we've essentially done is to
meet this criteria we are checking the
number to see if it's zero if it's zero
we return one and then so if we start
out with zero uh oops uh factorial zero
should be
if one return one
um
I'll change the code here just for
Simplicity for example so if we're one
we return one so one one yeah no sir was
right
so if we're one one minus one is zero
zero comes in for zero we return one
uh if we're two two minus one okay
um
what am I doing wrong here something
number times the factorial thank you
or
well yeah yeah
right
something like that but anyway so this
is how you would write recursion but
here's your test case you would actually
write this test and then the test would
exist so you would expect one two three
and so on and then you would come in
here and write your code so if your code
worked
you would run the test and you should
get these these test cases should pass
so that's just kind of pseudocoding and
expanding out on that but if I didn't do
that
or like this division by zero here like
this rule
code review should fail things like that
so this just is like Rob said one
example of how you can do readable code
uh I just like this particular approach
because with the comments it's very
clear
without the comments it's still pretty
clear as to what's going on and this can
be applied to pretty much any language
out there not just Java
any other questions
okay
so today we talked about writing
readable code we talked about ways we
could uh
cleanly write code why it's beneficial
what can happen if we don't write
readable code and just some other rabbit
holes we could go down if we expand on
this topic further
I'd like to thank everyone for your time
today we appreciate your time we'd love
if you'd love to discuss any of these
topics further with us you can reach out
with some questions or comments to us at
infoletdeveloper.com
developmenter.com contact us we're on
Twitter at developreneur we're also on
facebook.com at developer and you can
find us on YouTube by searching
developreneur our goal is making every
developer better
thank you have a wonderful day
thank you
Transcript Segments
10.7

thank you

18.89

[Music]

27.119

because I've actually seen applications

29.34

where this makes sense to pull this out

31.74

and do this but this code itself was

34.32

embedded inside of another method or

37.079

another class and if you refactor things

40.079

cleanly it's reusable the whole point of

42.899

object-oriented programming is to write

45.18

clean

46.5

reusable functional code so that you

49.92

don't reinvent the wheel every time you

51.66

do a new method new class new program

54.32

and if you break it down like this it's

57

very easy to understand what's going on

59.16

and able to follow the balancing ball

63.359

okay gotcha okay

67.82

this is uh as Michael had mentioned as

72.36

he started getting into this this

73.439

actually you could do a lot of

76.1

discussion around

78.08

documentation and that the approach that

80.64

he he's lined out is is one approach

84.659

um and it varies from organization

87.54

organization and even language language

90.42

because some kind of you know some

91.979

places it's

93.9

um it's things like it's entirely write

96.78

your code without comment so that there

99

are no comments it should be fairly it

101.22

should be readable based on

104.4

uh like function names and class names

107.04

and variable names and things like that

108.9

and some of it is

111.36

um even without having things quite as

114.18

as complicated like the way hit this

115.86

one's set up for example you know the

117.24

subtract two numbers and return the

118.56

result if you look at the the variable

120.659

names it's it's obvious that's what it

123.299

is I mean it's like you basically just

125.04

take left number minus right number and

126.78

it returns result so the code itself is

130.08

is very readable even with a very simple

133.92

um name

135.42

now some cases the names you know there

137.819

are some languages limit the names

140.22

um and some of them uh some

142.2

organizations are going to say hey we

143.459

want to keep it you know shorter rather

145.14

than longer code completion makes it so

147.78

that it doesn't that is less of an issue

150.12

uh like personally I prefer to have

153.78

stuff within the comments as opposed to

155.72

you know having the name basically

157.8

describe what it does it can be like a

160.08

summary but

161.819

um usually have more in the comments

164.099

versus uh you know having real long uh

167.879

variable names and things like that but

170.04

the key to the point for all that is

173.76

consistency and so he he mentioned

176.04

several times you know programming

177.54

standards development standards that is

180.36

probably the most important thing is

182.459

that whatever organization you're in is

184.2

that everybody does it consistently

186.72

which is where things like code reviews

188.22

are huge so for example if I if if this

192.06

is the the standard

194.34

and I go in and write it and I don't

196.68

follow the standard and I you know I've

198.36

got some x's and y's and some things

200.34

like that and some really short you know

202.26

cryptic names then in the code review

204.599

would be something where Mike would be

205.98

like hey you know according to code

208.2

standards this is how it should look so

210.84

you know your code doesn't fall as

212.459

standard let's get that to standard so

214.68

that when because that's the most

216

frustrating thing is when somebody comes

217.68

in and looks at the code and depending

221.04

on what class or what file or what

223.2

function even

224.7

it's completely different uh how it's

227.879

documented and of course the other thing

229.62

you have to watch out it for is just

231.12

make sure that it stays current you know

232.56

if you come in and you start changing

233.94

something around then you may need to

236.76

you know update the comments you may

238.44

need to make sure that you've got your

239.94

you know maybe you adjust a couple of

241.68

the uh the names

244.739

um so that's important and then one

246.36

other thing I want to point out that's

248.159

that sort of comes up as Michael was

250.379

going through that where he was looking

251.58

at

252.84

you know like where he's building out

254.22

like what this does and what it returns

256.019

uh particularly like the the divide by

258.479

zero that's something where it's

262.139

important for both testing and as you're

264.479

developing and so now it says hey you

266.639

know we can't divide by zero

268.919

so you should either you know have some

271.139

sort of exception that it throws or have

273.54

something that says okay if I get a zero

275.58

I'm going to send this message or return

278.58

this value and then make sure that your

280.86

code does that because from a testing

282.84

point of view that means

284.88

you know they're gonna they're gonna

286.32

come through and they should be able to

287.58

see uh from your comments from your

290.16

documentation how that's supposed to

292.62

work and then build that test and then

294.96

it better match results so if you say

297

it's going to return uh not a number

299.28

then when they come in if they do a zero

301.62

then it should come back not a number

304.08

otherwise you're gonna say hey this

305.82

failed testing so documentation not is

309.3

is a great way for you to be more

313.199

thorough in your development that's why

314.88

in some cases there's there's some

316.56

approaches to coding where you start by

319.199

creating comments you pseudocode

321.479

everything

322.52

and sort of write out the story of what

325.62

your class or your function is supposed

327.06

to do and then as you build in the code

329.639

you've sort of got like an outline that

330.96

says hey I got to do this I've got to do

332.58

that I've got to check for these things

334.56

and handle these exceptions I'm going to

336.479

return these kinds of messages so

338.639

there's

339.78

there's a lot of benefit that comes from

342.78

proper documentation that is this sort

345.6

of hidden you know there's a couple

347.46

things Michael highlighted but there's

348.9

there's some things that were just sort

350.34

of like

351.18

uh you know a part of that that when you

354

think about it like oh wow this is

355.44

really going to help us become better

357.36

developers doing it uh oh one last thing

359.94

I want to mention is that while this was

361.8

focused on Java and javadoc

364.5

uh just like unit testing the junit

367.38

we've mentioned in the past

369.3

those tools exist in some way form or

372.3

fashion in almost every language and if

374.82

you if you're not sure what it is like

376.38

you know if you're getting into

377.1

something new then if you just do a

379.259

Google or other search based on you know

382.5

hey what is the javadoc

385.46

equivalent in PHP

389.22

um or c-sharp or whatever it is then

391.74

you're gonna you're probably gonna get

393.06

some good results and yeah the

396.12

um the way that you do comments and

397.919

maybe the way that you uh take advantage

400.319

of that tool are going to vary but uh

403.319

you know those those tools pretty much

404.94

exist everywhere and it's worth doing a

407.46

little bit of upfront research to make

409.02

sure that whatever it is you're you're

410.88

working in that it um

413.94

that you take advantage of it and it I

416.699

guess the follow-up to that is things

418.199

like uh Minify and things like that and

421.08

some of the code compilation types of

423.419

things that work for for example with

426

JavaScript which allow you to have

428.46

really big complex names and all of that

431.46

and not be hurt by uh when you get to

435.12

run time because it basically goes

436.919

through and says I'm going to take all

437.94

these big complex very human readable

440.46

names I'm going to turn on something

442.319

that is just you know the machine can

443.819

read it a human's not going to get much

445.86

use out of it but then it's it's going

448.199

to run faster so

450.66

um you know there's some tools like that

452.099

whichever environment you're in it's

454.62

worth it to understand what tools are

456.539

there and as an organization or even as

459.18

an independent is to

461.28

I have a standard and stick to it and

463.8

hold yourself and your teammates to it

466.319

I'll step off my soapbox but there's

468.3

just like I said this is one of those

469.68

it's actually it's a very big uh topic

472.8

and although Michael did a great job and

474.419

you know in one session sort of like

476.099

saying hey here's here's some big

478.319

differences from where he started and

479.94

then where he ended up

482.039

um there's so many rabbit holes we could

483.66

go down talking about that

486.599

yep and I have planned on possibly a

489.18

future topics and discussions on that as

491.52

well but it good addition Rob uh also

495.24

you know you mentioned code review well

497.099

right here this is a good example of

498.84

what should be caught in a code review

500.28

if you document it so here I'm saying

502.8

what this method should be doing and I

505.379

say oh I'm done I commit the code under

507.84

code review it should fail because I did

510.479

not follow this condition I did not do

512.76

the divide by zero rules so this would

516.36

fail this should come back and then what

519.36

should have happened should have been

521.159

something more along the lines of okay

522.839

so if

524.339

right number

528

equals zero

532.32

it's either return

536.399

a number

539.519

um

540.839

or like throw

550.08

or

555.839

format exception or

559.019

null number or whatever but basically

561.6

you have to do something like this to

563.16

handle that uh like Rob was saying uh

566.22

the other thing that uh Rob mentioned

567.959

that I didn't touch on too deeply but if

571.56

you actually were to like I said remove

574.8

these comments you understand what the

577.38

code's doing but if you I haven't

579.48

written this and you're just writing it

581.339

for the beginning this makes sense to

583.38

just come in and write something write

586.14

the requirements for the method right

588.779

like pseudocode the method and then even

591.24

potentially uh after this so you do

593.399

something like this

595.2

and actually let's just uh do another

597.72

example real quick

599.94

um so if we were to do something like

601.8

this

603.779

um

607.08

simulate

608.64

the factorial on a calculator

615.98

and this is to uh

620.1

calculate the factorial

623.88

value of a number

626.88

thank you

627.839

now

630.839

you get a number so we could say float

633.68

we could say factorial

640.399

and we'll say float

643.26

and number

645.36

that's it all right so we don't have to

648.24

go too far into the depths except we

651.06

know we need to return something

653.399

so we know we need to return

656.339

a float

661.74

uh oh uh sorry wrong

665.1

all right so float

667.56

float

668.76

and we'll just do return

675

all right so this is just meeting the

676.8

bare minimum requirements for the method

679.98

and then in here we could essentially

683.04

kind of do some pseudo code or you could

685.92

kind of write your testing so you would

688.019

say something like for testing

691.32

you could do something inside of here

693.06

even but for like a unit test and this

695.64

kind of pushes a different topic but

697.5

this is kind of where you would write

698.82

your test driven code so you would write

700.92

the test cases first and then come back

702.66

and make sure that the code worked but

704.82

to test this it would be factorial

711.12

of say zero

715.68

would equal zero

718.14

um

722.94

Victoria one

725.399

two equal one

727.44

factorial two

729.839

would equal two

733.56

three

735.24

is going to be six

737.82

and then

739.14

for testing

741.839

basically factorial and factorial is

745.68

going to equal to n times n minus 1.

756.6

and so what you end up getting into is

759.72

oh well all right here is how we're

762.54

going to test this this is how the

765.18

function should work so you would

766.8

actually write this test case to expect

768.959

these values and then you would come

770.82

back in here and you would write the

772.74

code to do this so actually what you're

774.48

going to be doing here is if

778.62

number

780.12

equals zero

783.899

return

786.899

one

789.24

else

790.8

return

795.54

this one

805.92

zero

809.16

so now what we've essentially done is to

812.1

meet this criteria we are checking the

815.88

number to see if it's zero if it's zero

817.86

we return one and then so if we start

820.98

out with zero uh oops uh factorial zero

824.459

should be

826.32

if one return one

829.32

um

834.779

I'll change the code here just for

837.139

Simplicity for example so if we're one

840.6

we return one so one one yeah no sir was

844.74

right

849.98

so if we're one one minus one is zero

853.5

zero comes in for zero we return one

857.1

uh if we're two two minus one okay

861.959

um

870.48

what am I doing wrong here something

872.1

number times the factorial thank you

881.16

or

889.98

well yeah yeah

898.98

right

902.82

something like that but anyway so this

906.36

is how you would write recursion but

908.459

here's your test case you would actually

910.019

write this test and then the test would

912.899

exist so you would expect one two three

914.94

and so on and then you would come in

917.459

here and write your code so if your code

919.62

worked

920.94

you would run the test and you should

922.8

get these these test cases should pass

925.22

so that's just kind of pseudocoding and

928.199

expanding out on that but if I didn't do

930.839

that

931.8

or like this division by zero here like

934.019

this rule

935.16

code review should fail things like that

937.199

so this just is like Rob said one

940.26

example of how you can do readable code

943.68

uh I just like this particular approach

946.019

because with the comments it's very

948.24

clear

949.199

without the comments it's still pretty

951.54

clear as to what's going on and this can

953.699

be applied to pretty much any language

955.86

out there not just Java

961.56

any other questions

968.519

okay

973.32

so today we talked about writing

975.899

readable code we talked about ways we

978.42

could uh

980.16

cleanly write code why it's beneficial

982.8

what can happen if we don't write

984.54

readable code and just some other rabbit

988.079

holes we could go down if we expand on

990.839

this topic further

993.779

I'd like to thank everyone for your time

995.76

today we appreciate your time we'd love

998.639

if you'd love to discuss any of these

1000.32

topics further with us you can reach out

1002.54

with some questions or comments to us at

1005.36

infoletdeveloper.com

1007.339

developmenter.com contact us we're on

1010.339

Twitter at developreneur we're also on

1012.74

facebook.com at developer and you can

1015.68

find us on YouTube by searching

1017.36

developreneur our goal is making every

1019.94

developer better

1021.38

thank you have a wonderful day

1036.64

thank you