Code Tips #11: Ruby – Provide ways to iterate over collections

Sometimes we have classes that represent collections, for example a class Group can represent a collection of members

class Group
  def initialize(members)
    @members = members.dup
  end
end

When we want to iterate over the collection’s members we have to expose them through attr_reader

class Group
  attr_reader :members

  def initialize(members)
    @members = members.dup
  end
end

group1 = Group.new(["Nick", "Ana", "Petros"])

group1.members.each { |x| p |x| }

The problem with this approach is that the consumers of our collection have to know that we named our variable “members” and use it to iterate. If we ever rename this variable, the consumers of our collection have to rename their call as well. Moreover, we can’t control the order of the enumeration. What can we do to fix that? Continue reading Code Tips #11: Ruby – Provide ways to iterate over collections

Code tips #5: React – How to avoid extra control property

In React many times we have components that have some slight variations and we want to control how these components will be rendered. For example, we have a mobile and a desktop version of a component. Usually React developers tend to add an extra property in the component based on which they control the version of the component that will be rendered, either through if or switch statements.

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ProductCardSmall from './ProductCardLarge';
import ProductCardMedium from './ProductCardMedium';
import ProductCardLarge from './IPBadgeLarge';

export default class ProductCard extends Component {
  static propTypes = {
    cardType: PropTypes.string,
  };

  render() {
    const { cardType } = this.props;

    switch (cardType) {
      default:
      case 'small':
        return <ProductCardSmall {...this.props} />;
      case 'medium':
        return <ProductCardMedium {...this.props} />;
      case 'large':
        return <ProductCardLarge {...this.props} />;
    }
  }
}

And then use the component like:

<ProductCard cardType="medium" />

But there is a way to simplify the code by Continue reading Code tips #5: React – How to avoid extra control property

Book Review: The DevOps Handbook: How to Create World-Class Agility, Reliability, and Security in Technology Organizations

(5 / 5)

Easy read, with many real-world examples from companies like Linkedin, Twitter, Etsy. Provides myriad of best practices. I highly recommend this book to anyone that is involved in the development of any type of software, from POs to developers, and from QA engineers to Managers, this book has to offer practical advice to everyone.