DEV Community

Barrington Hebert
Barrington Hebert

Posted on • Edited on

How To Code Faster: Coding Strategies, GRASP, Shortcuts

Sometimes coding can be a swift and easy experience, and other times it may seem to be a slow and never-ending process filled with continuously going back to fix untimely mistakes and errors. In this blog, we will explore methods in which we can code faster and quickly reach the deadlines given to us by our employers.
First off, the most important element of coding, and possibly the most overlooked by newer programmers, is the importance of the GRASP principles. GRASP, which stands for General Responsibility Assignment Software Patterns, is a concept described by Craig Larman which lays out a set of principles and guidelines for designing object-oriented software systems. GRASP allows us to coders to make more informed decisions during the object-oriented design process, and through understanding the GRASP principles, we can tackle complex coding projects with ease.
One can discuss the GRASP phase in depth, however I will bless you with a short rundown of the principles outlined in GRASP for your enjoyment, whilst highlighting a few of my personal favorite principles in GRASP.
There are 9 principles to GRASP, the first principle is the creator principle; which provides us the task of asking ourselves; where exactly should we create our Objects in our code? How can we minimize the usage of memory on our systems in a way that we can find what we want in our code easily without searching through multiple lines of code for a single variable? The general answer to such a question can be found in the implementation and usage of constructor functions, or class constructors; which minimize the recreation of objects multiple times. Consider the following example in which you are designing a game with multiple countries:

//this constructor is where all countries created in our game will be 
//passed in.
class Country {
  constructor(name, population,wealth){
    this.name = name;
    this.population = population;
    this.wealth = wealth
  }
}

//This named country, will have a unique bonus, and will inherit the properties of the Country constructor
class Jamaica extends Country {
  //the unique bonus or trait that comes with this country will stay assigned to this country. Perhaps not all countries will need this trait or method. Allowing for easier access, fewer lines of code will be needed
  constructor(name, job, income, somethingUniqueToJamaica){
    super(name, job, income)
    this.somethingUniqueToJamaica =  somethingUniqueToJamaica;
  }
}
Enter fullscreen mode Exit fullscreen mode

The second principle of GRASP dictates where you should assign different responsibilities in your code. Which states that in your code; you should try to keep the responsibilities closest to the code block that is related to that responsibility. If you were computing sales of rugs for example, you would keep the expression or function that computes the total sales closest to the function that generates the list of items you are calculating the total for. There is also the controller principle; that says it makes sense to have an object inside of your user interface that handles the events that relate to the user interface. It helps your code look neat if you place methods that handles events (click, mouseenter, etc) all inside of one object; saving you time from scrolling around searching for where you put that .onclick for a particular button.

Image description

The next principle is called Protected Variation. It is essentially a reinforcement of the fundamental concept of Abstraction; which is the removal of unnecessary details from code or programs to make them easier to use and understand [https://stackify.com/oop-concept-abstraction/#:~:text=Abstraction%20is%20one%20of%20the,about%20all%20the%20hidden%20complexity]. Creating an object that helps translate, or format other objects into a certain way, will help make code easier to read or pass in to other code blocks; a good example of Protected Variation would be something similar to JSON.parse. Indirection and Low-Coupling, are two more principles in GRASP that highly relate to the implementation of this concept.
The Indirection principle involves introducing an intermediary object or layer to mediate interactions between classes . Low-coupling asks that you keep your code broken up into smaller pieces for ease of reusability, and High Cohesion, asks for us to ensure our code blocks are each individually highly focused on accomplishing one particular task. GRASP also emphasizes Pure Fabrication and Polymorphism, where polymorphism emphasizes the importance of using object inheritance, and pure fabrication is a principle that states if we don't know where exactly to put something, we may be better off creating a new object or class for it.
If you wish to explore more into the GRASP phase, the youtube channel ArjanCodes does an excellent job explaining all the principles of GRASP in this video [https://www.youtube.com/watch?v=fGNF6wuD-fg].

Image description

Now that you understand the GRASP principles, it should become clear to you how important it is to make a plan before you begin coding. Planning out what it is you want to accomplish before coding is the arguably the most important part of creating code and can drastically reduce the time spent coding a project. Dedicating an extra 15 minutes of time on the drawing board ensuring everyone is on the same page, can understand the layout of a project, whilst also reinforcing the importance of implementing the GRASP principles, can result in hours saved in the long run. When it comes time to actually begin coding, it is also important to utilize all the tools made available to us as programmers to speed up our time coding exponentially.
We as coders are given a toolbox of methods we can use to help speed up the time we spend coding, three of our most important tools is the debugger, our ability to write tests, and shortcut commands. These three tools work together, as they can help us identify problems and fix them much faster than without. Becoming comfortable with using the debugger can help us identify issues in our code, and writing automated tests will help issues reveal themselves earlier in our code than without. Writing our own tests is a good way to ensure we don't ruin other sections of code with the added bonus of the fact that as one continues to code; the tests will always be there for us to reference.

Image description

The debugger, as well as tests, are amazing tools we can use to step through our code one line at a time to find errors and fix them quickly. Once these errors are found, we can go back and correct our code. If we do not make use of the shortcut commands available to us in coding; this can drastically increase the time spent coding. Some ideas that would easily fix a problem are often overlooked due to the fact that the fix may take "too long" to go back and due, but with implementing proper shortcuts, these tasks become easy, and we are given more freedom to begin experimenting with ideas that may yield the results we wish to acheive.

Image description

Some strategies I enjoy implementing is using ctrl+f to find if an item is in my code; although at times it will not answer the question as to whether it has been assigned value, or describe all the contents of the item within the code. This is where we can make use of logging values to a console.

//checking the value of an object
console.log("myObj is " + myObj);

console.log(`myObj is ${myObj} `);

console.log(myObj, 'myObj')
Enter fullscreen mode Exit fullscreen mode

Above, all 3 of these give the same result. But it is much faster to list the arguments to pass in console.log by separating them with a comma as seen on the third line. This is also much faster than chaining if/else statements as seen below in our next example.

//takes too many lines of code
if(myObj){
console.log('myObj', myObj)
} else {
console.log('myObj', myObj)
}

//cut out the middle man

console.log('myObj', myObj)
Enter fullscreen mode Exit fullscreen mode

I know it seems ridiculous but you will be surprised how many people want to use that if statement to check their code. Just type it in and hop into the debugger.

Consider this; If you do something that takes 15 seconds, it may not seem like a huge deal, but if you do that 1000 times, you just wasted 2 hours, cutting it down to 2 seconds will make it roughly 15 minutes. By shortening the amount of code or keys we need to push, we can drastically reduce the time spent coding. Some shortcuts are better than others at reducing these 15 second experiences down to a few seconds.

We can type out the abbreviation for a file and hit enter; and then voila, our css is there. One example of this is in css code, instead of trying to type out something crazy like 'grid-template-columns', you can just type "g" "t" and "c" (the abbreviation for it) then push enter, and the entire line will pop up!
Ever had to rename every single variable in your code? Instead of manually going through, you can highlight the variable in question, then press ctrl+shift+l, and it will select all matching variables in your code, as you type out one, you will be replacing them all. You can also go two at a time with ctrl+d.

There are countless shortcuts and tools we can use to shorten our code, and continuously learning these shortcuts from other programmers is something I continually experience on my coding journey. So be open-minded and willing to continuously learn. However shortcuts and using the debugger alone are nowhere near as powerful as ensuring you have a clear-cut plan and goal laid out before you code. Ensuring that plan implements the principles of GRASP will exponentially decrease the time you spend coding, as well as ensuring everyone involved in your project is on the same page. Hopefully you found this blog helpful and I look forward to sharing more things I have learned in my coding journey with you. Cheers!

Top comments (0)