In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. In this example we will create a rooted binary tree witch has a root node and every node has at most two children, if the node has only one child it is a leaf.
interface TreeNode { Boolean isRoot() Boolean isLeaf() List<TreeNode> getChildren() void setParent(TreeNode node) void addChild(TreeNode node) } The inteface helps to define our methods in terms of functionality description
[Read More]
Fastman
Write a program that breaks up a string of words with no spaces into a string with appropiate spaces.
Consider an example that’s rich enough but no tedious: “fastman” –> “fast man” Consider an empty string or null The words come from custom dictionary Solution
class Fastman { def dictionary = ["fast","fat","man","strong"] private String find(String string){ String result = '' if(string?.length() > 0){ for(int i=0;i<string.length()-1;i++){ for(int j=string.length();j!=i;j--){ if(dictionary.contains(string.substring(i,j))){ result += "${string.
[Read More]
Replace conditional with polymorphism
The essence of polymorphism is that it allows you to avoid writing an explicit conditional when you have objects whose behavior varies depending on their types. Polymorphism gives you many advantages. The biggest gain occurs when this same set of conditions appears in many places in the program. if you want to add a new type, you have to find and update all the conditionals, but with subclasses you just create a new subclass and provide the appropriate methods.
[Read More]
Replace Error Code with Exception
A method returns special code to indicate a wrong path. Throw an exception instead.
In computers, as in life, things go wrong occasionally, when things go wrong, you need to do something about it. In the simplest case, you can stop the program with an error code. When a routine finds an error, it needs to let its caller know, and the caller may pass the error up to the chain.
[Read More]
Introduce Parameter Object
You have a group of parameters that naturally go together, replace them with an object.
Often you see a particular group of parameters that tend to be passed together. Several methods may use this group, either on one class or in several classes. Such a group of classes is a data clump and can be replaced with an object that carries all of this data. This refactoring is useful because it reduces the size of parameter list, and long parameter lists are hard to understand.
[Read More]
Replace Temp With Query
You are using temporary variable to hold the result of an expression. Extract the expression into a method. Replace all the references to the temp with the new method. The new method can then be used then in other methods.
The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in wich they are used, temps tend to encourage longer methods, because that’s the only way you can reach the temp.
[Read More]
Duplicated code
Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.
When coding I focus mostly on duplication. When the same thing is done over and over, its a sign that there is an idea in our mind that is no well represented in the code.
[Read More]
Refactoring
What is refactor?
Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introduce bugs. In essence when you refactor you are improving the design of the code after it has been written. Why do I need refactoring?
[Read More]
Playwright Reports Deployment
Playwright reports are essential in a project since they provide a quick analysis about pass/fail ratio, scenarios, browsers behaviour, logs, etc. In this technical post, we will go over two approaches to exporting reports: static HTML pages and NGINX server. NOTE: If you need to know how to setup Playwright, please refer my previous post: Playwright Getting Started. Then, let’s create a file named static.yml in your ${PROJECT_HOME}/.github/workflows directory with this content:
[Read More]
Playwright Applitools Integration
In the same way, we open our eyes and analyze if an image looks good; Applitools uses Visual AI to detect changes in our website so we can identify UI defects or validate expected new functionality. In this technical post, we will go over Applitools technology and how it can save us a ton of time in visual component validations. NOTE: If you need to know how to setup Playwright, please refer my previous post: Playwright Getting Started.
[Read More]