Share
Explore

Daily Hustle :Daily Hustle :

I am starting to write this blog to track my progress of learning and documenting for future reference.
To be a better developer we should not only write code for sake of it but it should be easy to understand, scalable, reliable. For that, I will be summarizing the content of each chapter of the Clean Code Book.

Day 1 :

What I have learned today in brief: Naming conventions from Clean Code
The name should tell the intent
Use Searchable Names
Use Pronounceable Name
Don’t encode type in names Ex : phoneString, paymentInt
Avoid Prefixes to Names Ex: IshapeFactory
Don’t use the same name to mean two different things
Use Domain-Specific Name Ex: JobQueue, nameBuilder
Avoid Too Long Names

Day 2:

What I have learned today in brief: Heap Data Structure
a Heap is a special type of binary tree. A heap is a binary tree that meets the following criteria:
Is a complete binary tree;
The value of each node must be no greater than (or no less than) the value of its child nodes.

Classification of Heap

There are two kinds of heaps: Max Heap and Min Heap.
Max Heap: Each node in the Heap has a value no less than its child nodes. Therefore, the top element (root node) has the largest value in the Heap.
Min Heap: Each node in the Heap has a value no larger than its child nodes. Therefore, the top element (root node) has the smallest value in the Heap.
A Heap has the following properties:
Insertion of an element into the Heap has a time complexity of O(\log N)O(logN);
Deletion of an element from the Heap has a time complexity of O(\log N)O(logN);
The maximum/minimum value in the Heap can be obtained with O(1)O(1) time complexity.

First, we will cover Insertion in Max Heap and Min Heap:
Max Heap Insertion :
Suppose the Heap is a Max-Heap as:
10
/ \
5 3
/ \
2 4

The new element to be inserted is 15.

Process:
Step 1: Insert the new element at the end.
10
/ \
5 3
/ \ /
2 4 15

Step 2: Heapify the new element following bottom-up
approach.
-> 15 is more than its parent 3, swap them.
10
/ \
5 15
/ \ /
2 4 3

-> 15 is again more than its parent 10, swap them.
15
/ \
5 10
/ \ /
2 4 3

Therefore, the final heap after insertion is:
15
/ \
5 10
/ \ /
2 4 3
Min Heap Insertion
Suppose the Heap is a Min-Heap as:
2
/ \
3 4
/ \
5 6

The new element to be inserted is 1.

Process:
Step 1: Insert the new element at the end.
2
/ \
3 4
/ \. /
5 6. 1

Step 2: Heapify the new element following bottom-up
approach.
-> 1 is less than its parent 4, swap them.
2
/ \
3 1
/ \. /
5 6. 4

-> 1 is again more than its parent 2, swap them.
2
/ \
3 1
/ \. /
5 6. 4

Therefore, the final heap after insertion is:
1
/ \
3 2
/ \. /
5 6. 4

In case of any query please put ur comments. See you soon :)

Day 3:

Today I have contributed to open source for the first time, basically put some comments on open PR and wrote down a few good coding practices. I think we learn also a lot by reading someone else’s code.
1) Junit test cases how we can better use never() method.
2) The usage of @Nullable is to let a user and the compiler know it is fine to allow a null value in as that parameter.
I believe it is a more straight-forward way of letting a user looking at your API to know that passing null will not create an NPE or undefined behaviour.
For a setter, if that field is going to be used in many places, it would be nice to look at its setter and discover null is handled.
That being said I think it should only be used for API's or libraries, using them in your own code would create too much useless code.
3) Using Jdoc it's better to see what parameters it accepts and what its return value is. It gives a clear cut idea about its acceptance of input and return of output.
Example:
/**
* Delegates the call to the {@code createContext(sessionMode)},
* using the {@link JMSContext#AUTO_ACKNOWLEDGE} as a value (JMS specification compliant).
* @return the JMSContext
*/
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
return this.createContext(JMSContext.AUTO_ACKNOWLEDGE);
}

Apart from that, I have done a very simple DS question:
class Solution {
public int countOdds(int low, int high) {
if(low%2==0 && high%2==0) return (high-low)/2;
else return (high-low)/2 + 1;

}
}
Apart from that, I am summarizing the second chapter (Function) summary:
The function should do only one task at a time
Do only one thing in a function
Command and query separations
Use minimum arguments (max 2)
Prefer exceptions over error code

Day 4:

Learnt a few things in different spaces. First will cover comments sections of clean code:
Explain why the programs work, how this code will tell
Difficult to understand code can use comments
Reveal Intent
When should you not use comments:
Comments should not explain the code, code should
Too many details should be avoided
Also, read about how to optimize Java performance, a very good one:
Now comes the DS part:
Done few questions of hackerrank:

public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
ArrayList<Integer> c=new ArrayList<>();
for(int i=0;i<a.size();i++)
{
if(a.get(i)<b.get(i))
c.set(1,c.get(1)+1);
else if(a.get(i)>b.get(i))
c.set(0,c.get(0)+1);
}
return c;
}

}
public static long aVeryBigSum(List<Long> ar) {
long[] res = new long[ar.size()];
int i= 0;
for(Long a: ar)
{
res[i] = a;
i++;

}
return LongStream.of(res).sum();

}

public static void plusMinus(List<Integer> arr) {
double positives=0, negatives =0, zeroes =0;
for(int i=0 ; i<arr.size(); i++)
{
if(arr.get(i) > 0)
positives++;
else if(arr.get(i) < 0)
negatives++;
else
zeroes++;
}
int size = arr.size();
System.out.println(positives / size);
System.out.println(negatives / size);
System.out.println(zeroes / size);
}
public static int diagonalDifference(List<List<Integer>> arr) {
// i=0, j=0; i=1, j=1, i= 2,j=2
// i=0, j=2, i=1, j=1 , i=2 j=0
int leftSum = 0;
int rightSum = 0;

for (int i = 0; i < arr.size(); i++) {
leftSum += arr.get(i).get(i);
rightSum += arr.get(i).get(arr.size() - (1 + i));
}
return Math.abs(leftSum - rightSum);

}

Day 5:

Errors: not recoverable, caused by System environment, runtime E.g Linkage Error, annotation error and virtual memory