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