-: We're gonna talk about indentation in this video,
but firstly, let's recap and go into
a bit more detail about code blocks.
So a code block in Java,
and actually most programming languages,
is one or more statements within left
and right curly braces.
All the code within the curly brace is a part
of a logical unit.
So when we define a class like this,
and you've seen this before in the course,
that's a code block.
The code block is the part in between
the left and right curly braces.
And code blocks can be nested within
other code blocks, as well.
So in this example here, we can see
that we've got our public static void main declaration
in the left and right curly braces.
Well that's a method,
and that also is a separate logical unit
or a code block in its own right, as well.
And taking that to the next level, after the declaration
of our int variable salary,
we've got an if statement there,
and that if statement is also got its own code block
as well with the left and right curly braces.
So this simplistic little programme has three code blocks.
The first one's here,
the second code block is here,
and the third one is here.
So code block one, this one, contains code block two
and code block three.
Code block two contains code block three.
So we say that code block three is a nested code block
of code block two, and code block two
and code block three are nested code blocks
of code block one.
So this will become important later
when we discuss variable scope,
but for now just keep in mind that each
of these code blocks are logical units in their own right.
All right, so I mentioned indentation.
So let's discuss that
and then see what that's got to do with code blocks.
So in computer programming,
indentation is a convention of adding spaces
to typed code to convey programme structure.
So here's our code block sample code again.
So notice how there are spaces
to the left of the word public,
in the main method declaration.
Note also that there's spaces to the left
of the declaration of our salary int variable.
And there's also spaces to the if statement
and then finally there's spaces to the left
of the system.out.println statement.
So this is indentation.
So if we contrast that to the same code,
without any indentation,
as you can see here on the screen,
this code works identically to the indented code
but Java ignores indentation when it scans your programme.
So just like white space, indentation is for humans only.
It makes code easier to read,
and for you to see at a glance which code blocks
specific code is part of.
So looking at both samples
of code at the same time,
to left we've got the indented code
and to the right the code that's not indented.
One's a lot easier to read than the other.
I think you'd agree
that the indented code's a lot easier to read.
So in terms of conveying programme structure
we can see at a glance because we're indenting
that the public static void main block is part
of the main block,
and we can see that within the public static void main
code block, there's a declaration of a variable there
and also the if statement.
It's very easy to see
because we're using indentation
that that is part of the main method part
of that code block in other words.
So that's what we're trying to do is
whilst you don't have to indent code,
certainly from Java's perspective it'll work
quite happily without it,
it's much easier to write code for humans,
and they can see at a glance which particular code
is part of which code block.
And that becomes important later
when we start talking about variable scope
and methods, and so on.
So again, as a recap, looking at both slides, again,
the code works identically in both cases.
Java ignores indentation when it scans your programme
and we're only indenting code using white space in general
to make it easier for humans to read.
All right, so to find out more information about
block indentation code conventions,
I'll go back to the browser here.
This is the link we've been using for our code conventions.
And you can see down in section four down here
there's an option for formatting.
And if we click into there,
there's a bit of a discussion there on block indentation
and using spaces.
And their example is using two spaces.
That's one way of doing things.
Another example is four spaces.
So there's multiple code conventions out there.
The point is that there is a convention here
telling you what you can do.
So let's swing back to IntelliJ now
and look at how indentation works in practise.
All right, so as you can see
I've created a new project called indentation.
Now this time I chose not to use the template
to create our class and method automatically
so that we can type some stuff in manually
to see how IntelliJ adds indentation for us automatically.
So if you're following along using another IDE
or a text editor, most will do a similar thing
to what you see here.
So what I'm gonna do first is
right click the src folder there.
I'm gonna click on new, select class, java class,
and I'm gonna go with the same boring name we've been using
throughout the course so far.
Main, a capital M,
making sure class is select there.
We can press enter,
and you can see automatically
that IntelliJ has created that class for us now.
I'm gonna close down that project pane
to give us a bit more space.
So we've got an initial code block as you can see there.
The lines one and two,
or basically the code in between the left
and right curly braces
of which there isn't any code at the moment.
And at the moment, we can't actually see any example
of indentation,
so let's actually add our main method
so that we can execute a programme.
And we've seen that and talked about that previously.
I'm gonna come down to that end of line one
and press Enter.
Now notice what happened when I pressed Enter there.
IntelliJ's automatically indented
but it hasn't indented to two characters,
as the code convention stated, it's indented by four.
And we'll talk more about that a little bit later.
The point is though it's done the indentation
for you automatically.
And you'll find that most IDEs will do this for you
which is a good thing, saving you having
to remember to do it yourself.
All right, so what I'm gonna do now is type in
the IntelliJ shortcut which we've seen before, psvm,
and press Tab,
and that just types in the whole line for us.
And the point of that is that it's created
the method declaration for main,
and it's added another code block for us,
and note that it's automatically indented again
by yet another four spaces.
And being pedantic like I am,
I'm just gonna press enter there
just to give it a bit more space.
Now I can press tab to go over
to the position I was in before.
All right, so let's type in now our declaration statement
for our variable salary that we saw on the slides.
I'm gonna press Enter there again
and notice when I press Enter,
that the indentation level is kept automatically by IntelliJ
so it hasn't gone back to the start of the like like that,
but rather it knows, okay,
he wants to separate a line,
but he also wants to keep it at the same indentation level.
So basically IntelliJ is knowing
and it's been configured to work to use indentation.
So I'm gonna type int salary
equal zero, sorry equals 60
semicolon to end the line.
So I'm pressing Enter,
you can see that IntelliJ's maintained
the indentation level again,
and if we type our if statement from the slides,
if parentheses salary
is greater than 60
this time we're gonna add a code block,
we add our left curly brace,
press Enter,
you can see that IntelliJ has added the right curly brace
on line eight, and automatically indented again
ready for you to type some statement within that code block.
And I'll quickly type in the statement
sytem.out.println
You are rich exclamation mark.
All right, so that's indentation at work.
And you can see that that code is much easier to read,
and another way of looking at it would be,
if I come down here
and I remove the indentation,
I'm holding Shift and pressing Tab,
now if you look at that same code without indentation,
yes it still will work,
and if we run this, we see that this will still work for us.
In this case we've got no output
because I've used if salary greater than 60,
I'll change that to 50 which it should have been.
Run it again.
So I can see that's working okay
and if I code down the code window
and actually undo the changes again,
go back to where we were.
Like so, and I'll just change that back to 50 again.
That's the original code as I typed it
and obviously that'll still work as well.
You can see that one version of the code is a lot easier
to read than the other.
What I wanna do now is just show you some of the settings
within IntelliJ, so if you're using another IDE
you'll probably have to find the equivalent settings
in that IDE or text editor.
I'm on a Windows machine at the moment
so I'm gonna go into file
and select settings,
on a Mac it'll be preferences,
go into preferences and click on preferences
and get into there that way.
Next I'm gonna go into editor.
And when I come down to code style
I'm gonna close down that.
So I've got editor, code style down here.
And I'm gonna select Java.
And that should be the same on all three operating systems.
You'll really use preferences on a Mac
or settings for Windows or a Linux machine.
Then go down to editor, code style, and Java.
So there's a few things to note here.
So note that the use tab character checkbox isn't checked.
And that's a good thing,
and what that means is when you press Tab
which is the indent key in most text editors
it doesn't add a Tab character
but instead it adds a number of spaces.
And the number of spaces that it adds is based
on the contents of the tab size
which you can see is currently set to four in IntelliJ.
So in general it's a good idea not
to use any special characters in a raw text file
and a raw text file is a Java source file
that we've been using so far in the course.
The general recommendation is certainly to use spaces
instead of tab characters
and that's why this is the default option
that's turned off.
So that's what that option does
and as I mentioned, tab size indicates the number
of spaces that are gonna be used when you press tab
but also have the indent
and that's the automatic indent that IntelliJ's doing
for us which you've seen in this video.
That's set to four as well
and that can be changed.
Now I'm not gonna go over all these other settings,
other than to say that IntelliJ allows you
to do a lot of customization here
to work with different styles of code conventions.
So if you end up working for an organisation
that needs and has a different policy,
then you can actually change the sizes here
and basically work within their conventions.
All right, so I'm gonna close that down for now.
And we'll go back and remove the indentation again.
Cause I wanna show you another way
of just clearing up code if you get into
a situation where your code's messy.
I'm just gonna kill some of these spaces.
I'm gonna do the same thing I did before.
Select all and Shift + Tab
and remove the indentation again like so.
Again, I'll just run it again
and it should work as it worked before.
No dramas there, but another way,
instead of undoing which may not be
an option for you,
you can reformat your code at any time
taking into account any changes you've made
in the editor settings that I've just shown you,
by coming up to code and clicking on reformat code.
And there's a shortcut there
you can see it's Control + Alt + L on a Windows machine,
so you can just go into the relevant menu
for your other operating system if you're on Mac
or Linux and see what the shortcut is for you.
If you click on that,
that will then reformat your code based on the settings.
So it's taking into account the indentation settings
that were in that pane for you automatically.
That's pretty neat.
Anytime you get into trouble
or your code's starting to look messy
that's a good way to quickly clean it.
All right, so one last thing that I want to show you now
before we finish the video.
IntelliJ helpfully shows you code blocks
the start and where they end.
And that can be useful if you miss a right curly brace.
So if you look carefully you can see there's a line
to the left of the second keyword public on line three.
There's a line going down
to the right curly brace on line nine.
And what that's telling you is that the left curly brace
on line three,
that the start of the code block ends,
or is part of the closing code block on line nine.
So basically at a glance it's confirming which is which
and you can see that there's also another one
showing for line six through line eight.
So that's the other code block.
It's not showing the very first one,
the outer most code block on line on to eleven
because there's really no need to do that
because that's the outer most code block.
So this is kinda useful if you've missed
a right curly brace somewhere
you can just check those lines
and see whether what you thinks is actually lined up
with the right thing and a combination of you doing
that and actually looking at reformatting your code can be a
good way to actually fix general bugs when you're first
starting out learning to programme.
All right, so this video's gone on well
and truly long enough,
so I'm gonna end the video here
and see you on the next one.