Tag Archives: CS-343

Refactoring your program

Sometimes when a program undergoes consistent updates it can get messy, in cases like this it can be useful to refactor it. I’ve had a few experienced cleaning a program however I have never refactored an entire program. The developers over at refactoring guru luckily have a website dedicated to this subject.

An illustrated depiction of a programming refactoring process, highlighting the importance of clean code.

Purpose for refactoring

When you refactor a program you are fighting something they call technical debt and create clean code. With clean code comes a few benefits including:

  • Obvious for other programmers
  • Doesn’t contain duplicate code
  • Minimal number of classes and other moving parts
  • Passing of all tests
  • Easier and cheaper to maintain

What is technical debt?

“Technical debt” as a metaphor was originally suggested by Ward Cunningham using bank loans as an example.

You can make purchases faster If you get a loan from a bank however now on top of principal you have interest. and with time you can rack up so much interest that the amount of interest exceeds your total income, making full repayment impossible.

The same concept can be applied to code. Speeding up without testing new features will gradually slow your progress.

Some causes of technical debt include:

  • Business pressure
  • Lack of understanding the consequence
  • Failing to combat the strict coherence of components
  • Lack of tests, documentation, communication.
  • Long-term simultaneous development in several branches
  • Delayed refactoring
  • Incompetence

So when should one refactor?

Refactoring guru comes up with a few instances on when to refactor.

  • Rule of three:
    • When doing something for the first time, just get it done.
    • When doing something similar for the second time, cringe at having to repeat but do the same thing anyway.
    • When doing something for the third time, start refactoring.
  • Adding a feature:
    • If you have to deal with someone else’s dirty code, try refactoring it first; Easier for future features.
  • Fixing a bug:
    • Clean the code and errors will discover themselves
  • Code reviews:
    • Last chance to tidy up the code
    • Best to perform these reviews in pair with an author

We know when, but how?

Refactoring is done via a series of small changes, each making the existing code slightly better while leaving the program in working order.

Here is a checklist on refactoring done the right way:

  • The code is cleaner
  • There should not be new functionality
  • All existing tests pass

Final Thoughts:

Overall, I found this website on refactoring to be really informative and would recommend refactoring guru as a starting point. The most important thing that I got out of this is that developers should always try to write clean code or clean code as its undergoing development. Unfortunately sometimes software development can be very time containing and its not always possible which is why refactoring is important.

From the blog Petraq Mele blog posts by Petraq Mele and used with permission of the author. All other rights reserved by the author.

CS343-01: Week (Quarter) Two

Software Constr – Blog Two

In class, we learned two acronyms. DRY which was Don’t Repeat Yourself and YAGNI which meant You Ain’t Gonna Need It. So, I was curious as to what others we had and why we had them in the first place.

When researching on why acronyms like DRY and YAGNI are important, I came across that “in the ever-evolving world of software development, clean, maintainable code isn’t a luxury — it’s a survival skill. As systems grow and teams scale, codebases can quickly become tangled, brittle, and expensive to change.” The acronyms are principles that help us write code that are sustainable and that even though they’re easy to understand, they’re powerful when applied.

Alongside DRY and YAGNI, there’s also KISS (Keep It Simple, Stupid). The KISS principle “encourages developers to avoid unnecessary complexity. Whether you’re writing a function or designing an entire system, simplicity is often the best strategy.” This is important since if there’s a lot involved in the project, then there’s a lot more risk of bugs when can result in the needless complexity, rigidity, etc. that we learned in class.

This principle was actually created by a systems engineer named Kelly Johnson who was working for Lockheed Skunk Works which was the team that developed the SR-71 Blackbird (“a retired long-range, high-altitude, Mach 3+ strategic reconnaissance aircraft,” according to Wikipedia). “His idea was simple: systems should be so straightforward that even someone with basic training could repair them under stressful conditions — like in combat. This philosophy translated beautifully into software, where complexity is often the enemy of reliability.”

I learned that DRY “was introduced by Andy Hunt and Dave Thomas in their 1999 book The Pragmatic Programmer. Their definition was concise but profound: ‘Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.’” It helps reduce redundancy and speed up development in a project.

I learned that YAGNI worked as “a reminder to avoid building features or abstractions that aren’t immediately required” because of the encouragement it gives to “developers to resist the urge to ‘future-proof’ code based on assumptions about needs that may never materialize.” I also learned that YAGNI came from a thing called Extreme Programming (XP) and was popularized by Ron Jeffries, one of the original Agile Manifesto signatories. It became a core tenet of XP: ‘Always implement things when you actually need them, never when you just foresee that you need them.’” It helped prevent overthinking in engineering and keep the development focused on solving the problems of today rather than in the future.

Source: https://levelup.gitconnected.com/software-architecture-explaining-kiss-dry-yagni-with-practical-examples-in-typescript-9bf23c484816 

From the blog CS@Worcester – The Progress of Allana R by Allana Richardson and used with permission of the author. All other rights reserved by the author.

Principle of Least Knowledge (AKA Law of Demeter)

Hello everyone, today I will be talking about the Principle of Least Knowledge (AKA Law of Demeter). When first looking into this topic I was unsure of exactly what this was and how it would be applied to programming. When doing my research I found the Khouri College of Computer Sciences at North Eastern University had a page dedicated to this topic, where this law was first introduced.

General Formulation

Illustration of the Law of Demeter, highlighting the principle of limiting interactions between objects.

The LoD is essentially a simple style rule for designing object oriented systems.

“Only talk to your immediate friends” is the motto. 

Professor Leiberherr, the author, states a formulation of “Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.Its main motivation is to control information overload thus helping memory management as each item is closely related.

You can informally summarize the Law with these three formulations:

  • Each method can only send messages to a limited set of objects, namely to the argument objects and to the immediate subparts of the class to which the method is attached.
  • Each method is “dependent” on a limited set of objects (organize dependencies)
  • Each method “collaborates” with a limited set of objects (organize collaborations)

To formulate the Law we can choose from the following independent possibilities:

  • Object/Class
    • Class formulation is intended to be compile-time
    • Object formulation is intended to be followed conceptually
  • Messages/Generic functions/Methods
  • Weak/Strong
    • If we interpret it as all instance variables, including the inherited ones, we get the weak form of the Law. If we exclude the inherited instance variables, we get the strong form of the Law.

Benefits

In a paper written by Leiberherr, there are a couple facts stated for the benefits:

  • If the weak or strong LoD is followed and if class A’s protocol is renamed, then at most the preferred client methods of A and A’s subclasses require modification.
  • If the weak or strong LoD is followed and if the protocol of class A changes, then only preferred client methods of A and its subclasses need to be modified and only methods in the set of potential preferred clients of A and its subclasses need to be added
  • There’s even more benefits highlighted in the paper pertaining to limiting information overload.

Final Thoughts:

Prior to this webpage I knew nothing about this law/principle, however I now understand that it is a fairly useful rule or its respective use case. The law teaches you a way to program Classes, Inheritance, Abstraction, and a few other techniques. Infact There is so much more depth to this that I cant even fully fit it into this blog post. I would highly recommend you check out this page as It contains all the information you need along with sources to learn this.

From the blog Petraq Mele blog posts by Petraq Mele and used with permission of the author. All other rights reserved by the author.

How to become a SOLID software developer.

By Petraq Mele

Hello again to those reading this blog, this time I want to talk about an extremely topic relevant in the programming atmosphere, that being a concept known as SOLID. I managed to find a great section written by Manoj Phandis on these principles.

SOLID is an acronym of five OOP design principles designed to help make it more understandable, flexible, and maintainable.

What are the main 5 design principles?

SINGLE RESPONSIBILITY PRINCIPLE: This principle states a class should have one, and only one, reason to change. Lets take an Animal class example, as opposed to the animal class having a sound and feed parameter, separate those responsibilities into separate classes.

Some benefits include:

  • more readable, that is easier to understand
  • less error prone
  • more robust
  • better testable
  • better maintainable and extendable
  • maximizes the cohesion of classes.

OPEN CLOSED PRINCIPLE: “Open for extension” means the behavior of a module can be extended. “Closed for extension” means when we are adding/extending a modules behavior it should not result in changes to a modules source or binary code.

Demonstration of the Open/Closed Principle in object-oriented programming.

An example Manoj gives is a credit card company wanting to introduce a new preferred credit card product with double reward points. Instead of using conditionals, you create an extension via implementation inheritance or interface abstraction.

LISKOV SUBSTITUTION PRINCIPLE: LSP states functions that use references to base classes must be able to use objects of the derived class without knowing it. For LSP compliance we need to follow some rules that can be categorized into 2 groups:

  • Contract rules
    • Preconditions cannot be strengthened or weakened in a subtype
    • Invariants must be maintained
  • Variance rules
    • There must be contra-variance of the method argument in the subtype & be covariance of the return type in the subtype
    • No new exceptions can be thrown by the subtype unless they are part of the existing exception hierarchy.

INTERFACE SEGREGATION PRINCIPLE: Clients should not be forced to depend on methods they do not use. Interface segregation violations result in classes depending on things they don’t need & an increase of coupling and reduced flexibility/maintainability.

Tips to follow:

  • Prefer small, cohesive interfaces to “fat” interfaces
  • Creating smaller interfaces with just what we need
  • Have the fat interface implement your new interface.
  • Dependency of one class to another should depend on the smallest possible interface.

DEPENDENCY INVERSION PRINCIPLE: This principle has two parts. The first part says high-level modules should not depend on low-level modules. Both should depend on abstractions. The second part says Abstractions should not depend on details. Details should depend on abstractions.

Part one example:

Part two example:

Final thoughts:

Overall these principles are very useful when it comes to object-oriented software development. I learned quite a good amount and I want to thank Manoj Phandis for their amazing outline of the SOLID principles, I would advise you to check them out in his website incase you’re interested in learning more.

From the blog Petraq Mele blog posts by Petraq Mele and used with permission of the author. All other rights reserved by the author.

CS343-01: Week One Blog

Software Constr – Week One

For week one’s blog, I read the article I first found, Software Architecture Recommendations By Mark Richards. I wanted to start off with something I’ve found first before potentially either diving into more research for videos and articles or maybe even going through what my classmates have found for inspiration.

In Software Architecture Recommendations By Mark Richards, it features a software architect named Mark Richards who covers his definition of software architecture along with the key soft skills and responsibilities of an architect. There are two aspects of software architecture in his vision, as structure and as process. “Within the structural aspect of software architecture there are 4 dimensions: architecture characteristics, architecture components, architecture styles, and architecture decisions.” He mentions how there are soft skills and techniques which include negotiation, facilitation, and leadership when it comes to the process aspect he sees in software architecture.

He says that negotiation is a required skill since “almost every decision you make as an architect will be challenged. Your decisions will be challenged by other architects who think they have a better approach than you do; your decisions will be challenged by business stakeholders because they think your decision takes too long to implement or is too expensive or is not aligned with the business goals; and finally, your decisions will be challenged by development teams who believe they have a better solution.” Which makes sense since if you want to do your job, people will think what they have to say is better, more efficient, etc. So you do have to understand the political climate– as the article says– and how to navigate it to get the views and decisions you made both approved and accepted.

Facilitation is another “soft skill” that Mark Richards thought of where the architects don’t only collaborate with development teams. He says that they should also collaborate with various business stakeholders to understand a number of things such as “business drivers, explain important architecture characteristics, describe architectural solutions, and so on.” Upon further research, I found that facilitation is a shift in the architect’s role from a central decision-maker to a facilitator who empowers the development team to make architectural decisions collaboratively, so it’s a tool to have to keep things on track.

And then there’s leadership. The architect in Mark Richards’ mind is responsible for leading and guiding the development team through implementations. “They are there as a guide, mentor, coach, and facilitator to make sure the team is on track and is running as smooth as a well-oiled machine, and to be there when the team needs clarification or has questions and concerns about the architecture.”

Reading this from his perspective and his opinions are an interesting way of learning his job as a software architect since he has the experience and he has learned from the mistakes he’s made. It’s an important thing to know that even though it’s his job, he’s always learning about ways to improve and for him to share this knowledge is something that’s valuable since you can take the advice and knowledge given and apply it to your own job.

Source: https://apiumhub.com/tech-blog-barcelona/software-architecture-recommendations-mark-richards/

From the blog CS@Worcester – The Progress of Allana R by Allana Richardson and used with permission of the author. All other rights reserved by the author.

CS343-01: Software Constr, Des & Archit

Software Constr – First Steps

This is the beginning of my documentation in my class CS-343 and phase two, part two, I suppose, for CS@Worcester with the work that I was able to accomplish, had trouble with, and solutions I found.It’s kind of like my last and now current blog, except new class equals new puzzles I’ll experience! I really hope to do well, even with quizzes or exams as a sort of weak spot for me.But hopefully, with enough reading and practicing, I’ll do well. I want to do well. I’m aiming to do well.

From the blog CS@Worcester – The Progress of Allana R by Allana Richardson and used with permission of the author. All other rights reserved by the author.

Intro: Software Construction, Design and Architecture

This post serves as the starting point of a series of blog post that will be used to dive deeper into the topics of Software Construction, Design, and Architecture.

-EA

From the blog CS@Worcester – CurrentlyCompiling by currentlycompiling and used with permission of the author. All other rights reserved by the author.

Introduction- Brandon Njuguna

His this is my blog called Computer Science From a Basketball Fan. Im excited to Publish on this new blog and learn from other bloggers. This blog will be primarily used for CS-343 and CS-348 as of right now. Hope for the best!

From the blog Computer Science From a Basketball Fan by Brandon Njuguna and used with permission of the author. All other rights reserved by the author.

UML Class Diagram Arrows

Seeing how one of my CS classes just recently introduced me to UML class diagrams, I wanted to conduct some research on the formation and comprehension of the diagrams. I looked for blog posts that showed examples of the diagrams and how they would be written, since I like reading written explanations and visual representations.

I stumbled across a blog covering the different arrows used in the diagrams, as well as when and how they can be used. That blog can be accessed at https://www.gleek.io/blog/class-diagram-arrows.html.

The blog presents to us six different types of class diagram arrows: directed association, inheritance, composition, realization/implementation, aggregation, and dependency. I had already seen directed association, inheritance, and implementation arrows, but I had not yet familiarized myself with the others. I learned the following from the blog:

A composition arrow in a UML diagram does not have an actual arrowhead, but instead looks like a solid diamond at the end of a solid line. The solid diamond is at the sub-object end of the solid line, and indicates that the sub-object cannot exist without the container class. It can be shown using -<*>.

The aggregation arrow looks just like the composition arrow, except that the diamond is hollow/white. The aggregation arrow is used to show association between two classes, but the subclass can still exist without the super class. It is shown with -<>.

Dependency arrows have a thin arrowhead and a dashed line. They show that two elements depend on each other but the dependency is weaker than standard association. Making changes to the parent class will have an impact on the child class. It is shown with -.->.

This blog was a great source to understand the usage of different arrows for UML diagrams. It provided great examples of when to use aggregation and composition, and I now know to take those into account for when I will need to make my own UML class diagrams in the future. It was easy to understand how the relationship between a library class and a book class can use the aggregation arrow because books can still exist after they are borrowed from the library. It was also easy to see how the relationship between a shirt class and pocket class can be composition association because a shirt pocket would not exist without the shirt. I also think it was a nice touch for the blog to include a video on the page where it explains the arrows and shows how the examples would be typed up on gleek.io.

From the blog CS@Worcester – CS With Sarah by Sarah T and used with permission of the author. All other rights reserved by the author.

Introduction

Hello everyone, welcome to my blog! I’m a senior computer science major at Worcester State University. I will be using this blog to document my journey in CS-343.

From the blog CS@Worcester – Fadi Akram by Fadi Akram and used with permission of the author. All other rights reserved by the author.