Category Archives: Week8

Refactoring code!

Before our more recent classes where we learned about the Singleton and Simple Factory pattern, I knew about the concepts of refactoring, but in a different light. Rather than using these patterns and methods to make my code more efficient through a pattern of sorts, I consider refactoring as something more akin to making my code more readable and more efficient by breaking down useless portions of code. This isn’t exactly wrong, but it’s not completely right either!

Mainly, because though these approaches were important, they could be done even better! I main thing that I was missing in my own refactoring was obviously the design smells that we have recently went over now. Intuitively, I understood a few of the smells already, but now that I have a better grasp of many of them, it is clear that my older refactoring efforts were missing plenty of things!

For example, I used to clean up code and make it easier to understand by breaking them into chunks, which is good, but at the same time, I would also condense other areas since they seemed to ‘fit.’ It is clear now that I should NOT do that, but you live and you learn!

I think that the biggest resource that I found so far, is this website! Though it is basic and even a bit barebones, it gives a pretty good introduction to refactoring. It makes sure to consider things such as cleaning up dirty code from inexperience, a refactoring process, the design/code smells, and techniques to refactor as well as show off a few different design patterns!

One thing that might be helpful is that the website contains many different images to help break concepts down and even code examples! I know for me, it helps tremendously when I can look at some code and see exactly what is happening! Though I do understand the concepts a lot of the time, I find that I learn things a lot more quickly if I can look at the source and break things down myself! The website even has plenty of different coding languages too, so if say, Java isn’t your best, you can look at the different patterns in C#, Python, etc!

Overall though, I think that learning about a more refined structure to refactor code and implement these various design patterns will help me tremendously! I know that there have been times where I have gone to look at my old code and I’m sitting there lost, confused, and asking “Who did that!” Hopefully, with this in mind, I’ll have a better understanding of how to proceed with my older code past and future!

From the blog CS@Worcester – Bored Coding by iisbor and used with permission of the author. All other rights reserved by the author.

“Encapsulate what varies”

When asked the question “Which object-oriented design principle do you think is most important?” a Software Architect named Nicholas Cloud answered, “Encapsulate what varies”. I must say, I couldn’t agree more. In his blog, DeveolpIntelligence, Nicholas writes about his desire to constantly look for ways to utilize this principle to make writing expressive and maintainable code easier. He gives a lot of great examples and breaks them down, explaining each way they can be changed and why that change makes the code better.

Encapsulation means to bundle data with methods that operate on that data, or to restrict the direct access to some of an objects components. There has been some debate between programming language researches on which meaning they prefer to use. The Gang of Four suggests that we consider what should be variable in our designs. This approach is the opposite of focusing on the cause of redesign. Many design patterns use encapsulation to create layers between objects, making it easy to change things on different sides of the layers without negatively affecting the other side. There are so many advantages of encapsulation, from data hiding, to reusability, to making testing your code easier.

As one of the fundamentals of OOP, it is arguably one of the most important due to the number of advantages it brings. I found all the information I obtained from a few different blog posts and articles, all of which had almost the exact same information. Having read just the definition, understanding the true essence of encapsulation was difficult, but the code examples I found were extremely helpful and made things clearer. I thought the manner in which the information was presented was easy to follow and the use of real-life examples and comparisons kept me interested enough to stay focused on the subject. Below is an example I found easy to understand;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person {
String name;
int age;

 void talk() {
}
 
void think() {
}
 
void work() {
}
 
void play() {
}
}

The common characteristics and behaviors of a person are packaged into a single unit: the Personclass. The Person class is an encapsulation unit and the Person object exposes its attributes and behaviors to the outside world:
1
2
3
Person you = new Person();
you.name = "John";
you.work();
Here, encapsulation hides implementation details of the Person class from other objects. Likewise, creating an interface is also the process of encapsulation:
1
2
3
4
5
interface Human {
void eat();
void talk();
void think();
}
This interface groups the essential behaviors of human-being in a single unit.

The rest of the examples can be found at https://www.codejava.net/java-core/the-java-language/what-is-encapsulation-in-java-the-what-why-and-how.

I enjoyed learning about this principle and I feel more confident going forth in this field knowing yet another way to make writing code neater, more maintainable, flexible and easier to read.

Knowledge obtained for this blog post:
https://www.informit.com/articles/article.aspx?p=167890&seqNum=4
https://www.developintelligence.com/blog/2013/04/encapsulate-what-varies/
https://levelup.gitconnected.com/object-oriented-design-principles-bb6daf98b185
https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
https://www.geeksforgeeks.org/encapsulation-in-java/

From the blog cs@worcester – Coding_Kitchen by jsimolaris and used with permission of the author. All other rights reserved by the author.

Blog 8: Angular 2 Response

Hello readers, today I’m doing my response blog on a post from AshuTomar who wrote a very nice blog related to computer science and something that is actually being talked about in my computer science class right now.

The blog starts by talking about typescript, the writer says it is type safe which means it can generate errors at compile time only. It is also purely object oriented an extends object oriented javascript. Typescript also allows to write class, interface and module-statements just like in Java and lastly typescript can transpile to any version of javascript code.

Next the writer talks about the speed and performance of Angular 2 and that it supports lazy loading which means you don’t need to load the complete app at once but modules can be loaded dynamically whenever there is a requirement. Angular 2 also supports AOT compilation which allows you to compile your code ahead of time.

Skipping the uses of the valid HTML, DOM section, the writer talks about the modular design of Angular 2 and how it is easier for Java based programmers to learn a new Javascript framework. Lastly though in the blog the writer shows code how to use the bootstrapping application and talks about and shows dependency injection enhanced.

The reason I chose this blog was because it is very familiar to what we are talking about in my software class in college right now. We are trying to do typescript in webstorm for our duck simulator program that we had to write in different strategy patterns. Getting back to Angular 2 and typescript, this blog explained a little about typescript and what the writer learned from Angular 2 and how it works and what it can do. I’m not an expert on Angular yet, just learned it was a thing so getting any information from anywhere right now is always good to improve my skills. The writer says Angular 2 allows people the ability to make a single page application in a perfect viable way. Reading that makes me really want to get into Angular and learn it because going to a work place and applying to a job, if you can make them a webpage or do any computer science things that involve creating applications, it makes you more wanted than if you didn’t. That is given that if you have more impressive details on your application then you will be more wanted by a job, well with this skill in angular you could also do some freelance work, make webpages or applications for different companies for a side job, a lot of people could make a lot of money just by doing that, so I’m not shying away from learning angular and type script. In conclusion, Angular and typescript are very important for a computer scientist future, also this blog is a good read, gives some details about angular 2 and how it works. Thank you for reading my blog this week, come back next week for some more programming talk with me.

Source: https://blog.knoldus.com/2017/06/14/when-i-took-angular-2-to-build-my-application/

From the blog CS@Worcester – Benderson's Blog by bendersonsblog and used with permission of the author. All other rights reserved by the author.