Giter Site home page Giter Site logo

be-dev-angular-bad-practices's Introduction

be-dev-angular-bad-practices

mistakes made by backend dev writing angular/frontend code

Serialize/Deseralize

Bad

export default class Product implements Serializable<Product> {
  prices: Array<Prices> = new Array<Prices>();
  name: string;
  description: string
  
  deserialize(input: any): Product {
    input.prices.forEach(price => this.prices.push(new Price().deserialize(price)));
    this.name = input.name
    this.description = input.description
    
    return this;
  }
  
  serialize(): Object {
    return {
      prices: this.prices.map(price => price.serialize),
      name: this.name,
      description: this.description
    }
  } 
}

good

interface Product {
  prices: Prices[];
  name: string;
  description: string
}

Op vs functional

Bad

export default class Product implements Serializable<Product> {
  prices: Array<Prices> = new Array<Prices>();
  name: string;
  description: string
  constructor(props: Object) {
    Object.assign(this, props);
  }
  
  public getSpecialPrice(): Price | undefined {
    return this.prices.find((price) => price.type === 'special')
  }
}

good

getSpecialPrice(prices: Price[]): Price | undefined {
   return prices.find((price) => price.type === 'special');
}

Classes are not bad, but it should be used when it makes sense.

Fat arrow is overuse

Bad

export const getSpecialPrice = (prices: Price[]): Price | undefined  => {
   return prices.find((price) => price.type === 'special');
}

good

export function getSpecialPrice(prices: Price[]): Price | undefined {
   return prices.find((price) => price.type === 'special');
}

Avoid subscribe, use async

Function params objects are passed by pointer

bad

export function getPoroductWithoutSpecialPrice(product: Product): Product {
    const index = product.prices.findIndex((price) => price.type === 'special');
    if (index > -1) {
      poduct.prices.splice(index, 1)
    }
    return product;
}


const changedProduct = getPoroductWithoutSpecialPrice(initialProdct);

console.log('same prices?', changedProduct.length === initialProdct.length); // -> true

this becouse any object sent as a param to a function its passed by pointer, changing any value of the keys will change the initial object as well

good

export function getPoroductWithoutSpecialPrice(product: Product): Product {
    return {
      ...product,
      prices: product.prices.filter((price) => price.type === 'special')
    };   
}

const changedProduct = getPoroductWithoutSpecialPrice(initialProdct);

console.log('same prices?', changedProduct.length === initialProdct.length); // -> false

this works becouse the good getPoroductWithoutSpecialPrice generates a new object, this is expecially important when you use Redux/NgRx as it will only consider a change if the object's reference changes

NgRx reducers are tricky

Rxjs switchmap flatmap concat, merge, zip

function createObserver(id: number): EventEmitter {

}

be-dev-angular-bad-practices's People

Contributors

costicapuntaru avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.