Design Patterns and Best code practices

Project requirements and deadlines today are often ad-hoc. Catching up with early deadlines might drastically impact code quality. A well-structured code can help reduce this impact to a great extent.

What are Design Patterns?

A Pattern is a Generic solution to a commonly occurring problem. It defines a discipline and helps in maintaining code structure and quality.

Code quality reduces with an increase in complexity.

How do we improve this graph?

I'll pen-down two of my personal favorite design patterns and the way they solve a real-world code complexity problem

1. Observer Pattern

This pattern is used when there's a one-to-many relationship between objects. If one object changes its state, the others are to be notified. The best example for Observer pattern is the Publisher-subscriber use-case.

A JavaScript Implementation for a Pub-Sub Model can be found here.

2. Facade Pattern

Often, it is required that the developers need not get into the details of how things work. Facade pattern tells us to, "Create an interface, set protocol and functions within this interface, implement the interface and add functionality". Let's see how Facade pattern can be implemented in social media use-case.

/*
  
This code will help you understand the Facade pattern.

*/

public interface Friend {
    
    // Define what are friend functions. This sets a protocol.

    void addFriend();
    void removeFriend();
    void viewPhotos();
    void viewFriendList();
}

public class SocialMedia implements Friend {
    
    // override, write a logic to these functions 
    public void addFriend() {
      System.out.println("In SocialMedia::addFriend()");
    }
    
    // override 
    public void removeFriend() {
      System.out.println("In SocialMedia::removeFriend()");
    }
    
    // override 
    public void viewPhotos() {
      System.out.println("In SocialMedia::viewPhotos()");
    }
    
    // override 
    public void viewFriendList() {
      System.out.println("In SocialMedia::viewFriendList()");
    }
}


public class CreateSocialMedia {
    
    public static void main(String args[]) throws IOException {
      
      // Create an object of class SocialMedia
      SocialMedia sm = new SocialMedia();
      
      sm.addFriend();
      sm.removeFriend();
      sm.viewPhotos();
      sm.viewFriendList();

    }
}


Following this pattern, ensures that a developer who implements an interface follows the rules set by the interface.

Best Practices:

  1. Code Indentation: This is the most important of all. Make sure to use proper tabs or spaces wherever necessary. I prefer using tabs.
  2. Comments: Comments are good to have. Keep a habit of writing inline comments for every function and for any complex functionality you develop. Write a short summary on the top of your file stating what functionality the file is going to perform. (See the code sample above)
  3. Meaningful variable names: Declaring a variable with a meaningful name helps get an idea of what the variable will store. Be specific.
// Ambiguous variable name

var n = "John Doe";

// The better way
var full_name = "John Doe";

4. Create functions to perform specific tasks: Do not write too much functionality into just one function. Make sure to branch out responsibilities. Keep your functions short and to the point.

While adopting a Design pattern and following Code practices, always remember to follow the KISS principle. Do not over-complicate your project by using too many patterns and frameworks. Reading your code should be a good read :)



To view or add a comment, sign in

Others also viewed

Explore content categories