So we looked at naming conventions next just part of coding conventions let's learn about how to structure 2
our programs so we'll see some valuable recommendations and we will also look at one effective July 3
item in this lesson. 4
Let's begin with class organization. 5
And here is the order in which we need to place a Red Cross members disorder was prescribed by Sun Microsystems 6
on most of the standard Java classes do seem to follow this pattern. 7
Added up the variables with static variables being placed before instance variables we didn't have all 8
the static stuff there is static initializers followed by something called static nested classes which 9
would be followed by static methods static master classes will be discussed towards the later part of 10
the course. 11
Next we have the instant stuff that is instance initializers followed by constructors which are followed 12
by instance nested classes are in done followed by the instance methods. 13
So that's a long list but if it helps just memorize or display at the top we always have variables after 14
which we have the static stuff which is then followed by the instant stuff on Witan the static an instance 15
actions we have initializers followed by nested classes followed by methods. 16
Next is related to class size. 17
The most important thing about designing a class is that it should represent single responsibility. 18
And this is a very important design principle and the galiot as the single responsibility principle 19
. 20
So it shouldn't have more than one responsibility when designing your classes. 21
If you think about restricting them to single responsibility then it could help in creating better abstractions 22
. 23
That is you would very likely have several loosely coupled classes and such loosley couple classes promote 24
reusability. 25
There isn't a strict consensus but usually you don't want your class to exceed 2000 lines probably even 26
when it gets two thousand lines or so. 27
You may want to rethink about the classes responsibility and see if there is a need for further speedup 28
of the class. 29
Next let's look at methods. 30
It's very important to keep our methods very small unfocused. 31
This should do only one thing or no more. 32
There is a very popular book called clean code and it actually recommends that method should hardly 33
ever be 20 lines long motile methods that are small unfocussed would help in picking a good descriptive 34
method names. 35
If a method is long. 36
See if you can refactor it into smaller matters and enroll them from this method. 37
It promotes software use as despoiler amateurs might be useful elsewhere. 38
Mono the origin or longer method would be a lot cleaner and also readable. 39
Will you give this example when introducing methods on the left we have this search method. 40
This is just a search for a given item in a given and added. 41
The method has several lines of code representing two main steps in that to come. 42
First up would solve the array. 43
The second step would perform a binary search on the socket at it instead of having this and dialogic 44
in the single method. 45
We can move the sorting and the binary search code into their own methods and invoke them as shown here 46
. 47
The search matter is no much cleaner and with a single glance one may easily understand how the search 48
algorithm works. 49
It is also recommended to group methods that perform similar actions. 50
For example if you look at source code of string class you will notice that all the substring methods 51
are grouped together as they perform similar kind of action. 52
Similarly an index of methods are also grouped together. 53
Next is about local variables. 54
Under advice comes from a set of Djala I can Forty-Five which says minimize the scope of local variables 55
. 56
The items is that the most powerful technique is to declare a local variable when it is fussiest. 57
And here is an example. 58
Here the method go has around 50 lines of code following with the method freezing work. 59
An argument. 60
The variable D is declared right about the invocation statement. 61
However had we declared the variable early in the method it could affect readability. 62
And here is the same example. 63
But the local variable b is not declared at the beginning of the matter. 64
Now someone is looking at this code. 65
By the time they get to the statement that's invoking method foo. 66
He or she may not recall the variable by Autard initial value. 67
Apart from readability minimizing variable scope is also useful in preventing bugs. 68
For example for loop automatically minimizes the scope of variables and dupa this. 69
It is preferable to use for loops instead of by laws. 70
Let's understand just one example where the for loop preventible. 71
Let's consider this code fragment. 72
Here we are just creating an array by invoking get random perimeter between it and a fixed array with 73
random numbers are in size as indicated in the method argument. 74
Next we simply read the array and pass each R element to the matter. 75
Note that index is incremented in the matter in location statement itself. 76
Let's assume that the user didn't copy and paste this code somewhere below in the same method and renames 77
the added to the list too and also renames inexpressible idealogy. 78
Now the user makes a copy and paste error as he or she forgets to rename the variable i in the right 79
state mental condition expression and the result of this mistake is that the loop never gets executed 80
. 81
That's because I got incremented to three. 82
Jupiter Do I look under that is the condition expression in the second by look first right. 83
So there is no completion error and it has potential to go undetected even during runtime which could 84
be dangerous in real world system. 85
No the same code was rewritten using for each statement. 86
Then we wouldn't have this bug here the second for loop also has a copy and paste error where I was 87
again not renamed Pejic. 88
So G was never declared and so compiler would indicate this right away. 89
Even if the user did not want to rename I do J. 90
In the second for loop then everything would still work fine as variable i.e. scope is localized to 91
both the frontal lobes model within the while loop example. 92
The ice core is not ending with the why loop and it may be accidentally used somewhere down in the same 93
method and the result of this could be disastrous. 94
So variable scope is one reason to prefer four loops over why lobes but readability is also another 95
reason. 96
As you can see that 4 is much more cleaner looking good and that's about it with regards to how we can 97
structural classes and methods. 98
Next we will look at conventions that are more focused on stylistic aspects like indentation. 99
Thank you