Blockchain Developer | BSc Computer Science

Month: May 2021

Apex Constants Class Benefits and Best Practices

Introduction

Every software developer who has worked on a large project knows that sooner or later it is necessary to refactor the code, either due to changes in the business logic or due to optimization.

A good programmer must not only solve the problem efficiently but also provide a solution whose code is maintainable. To achieve the latter, the best recipe is always to follow good programming practices associated with the development environment.

Salesforce, of course, is not the exception, and here we will see the use of Constants to make our code clearer and more maintainable.

Apex Constants


In Apex, Constants are class members, generally static, whose values can be initialized only once.

public class someApexClass {
   static final String DEFAULT_NAME = 'Jhon Doe';
   static final String MAX_CONNECTIONS = 5;
}

The final keyword denotes this functionality, and as a convention, the name is used in capitalized snake case.

Note that the Constants, after being initialized, do not vary during a transaction, but their values ​​can be easily modified when it is necessary to refactor the logic dependent on it.

Apex Constants Class

Using Constants allows you to change a common element to several methods of a class quickly in one place, but many times we find configurations common to several classes.

A class whose functionality is to provide Constants to other ones allows to quickly change an element common to several classes.

As an example, imagine that you are a Salesforce Developer in a company that uses a status labeled “Designed” in the Opportunity sobject. This status is used in many places in the code, and one day the Scrum Product Owner requests changing the label to “Design Ready”.

Salesforce allows you to rename the status in the sobject configuration and reassign the values ​​of the existing records, but, what about the code? If the old status label is in each different class, we must change it by hand, which forces us to deploy all those classes, and perhaps to forget something in the deployment. If we previously defined a Constant and used it in all these classes, we only need to change the value in one place.

A Word About Performance

As Salesforce Developers, you know that Sharing Is Caring in the Multitenant Cloud, so we work for efficiency.

It is not the intention of this post to explain in detail what a Property is in Apex and its functionalities, but when the Apex Constants Class grows using the syntax that we presented previously (members syntax), the execution time of all the transactions that use any of the Constants of this class grows significantly.

A class with approximately 300 declared Constants can add about a second of execution time to each transaction (Relative to the hardware, but significant).

The solution: declaring Constants as properties cause them to load as they are requested, and not all at once (lazyload).

public class someApexClass {
   static final String DEFAULT_NAME { get{ return 'Jhon Doe'; }};
   static final String MAX_CONNECTIONS { get{ return 5; }};
}

However, this makes requesting the constant more than once slower than the previous way, so if it is used in a heavy cycle, the operation can be more expensive than using members.

As a general rule, it is advisable to use properties (lazyload) when we have a lot of Constants defined in our class, and members otherwise.

How To Start WordPress with Caddy using Docker Compose

Introduction

WordPress is a popular content management system (CMS). It can be used to set up blogs and websites quickly and easily, and almost all of its administration is possible through a web interface.

In most cases, WordPress is installed using a LAMP or LEMP stack (i.e. using either Apache or Nginx as a web server). In this guide, we’ll set up WordPress with Caddy instead. Caddy is a new web server quickly gaining popularity for its wide array of unique features, like HTTP/2 support and automatic TLS encryption with Let’s Encrypt, a popular free certificate provider.

In this tutorial, you will configure Docker Compose to start WordPress with Caddy v2.

Install Docker

  1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
  1. Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
  1. Use the following command to set up the stable repository
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  1. Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

  1. If you would like to use Docker as a non-root user, you should now consider adding your user to the “docker” group with something like:
    sudo usermod -aG docker your-user

Remember to log out and back in for this to take effect!

Install Docker-Compose

  1. Run this command to download the current stable release of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
  1. Test your installation:
$ docker-compose --version
docker-compose version 1.26.2, build 1110ad01

Step 1: Create docker-compose.yml

version: '3.3'
services:
  # Database
  database:
    image: mysql:latest
    container_name: database
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    env_file: 
      - .env
    environment: 
      MYSQL_DATABASE: blog_wp
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - blog-network
  # WordPress
  wordpress:
    depends_on:
      - database
    image: wordpress:php7.4-fpm-alpine
    container_name: wordpress
    restart: always
    user: "root:root"
    env_file: 
      - .env
    environment:
      - WORDPRESS_DB_HOST=database:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=blog_wp
    volumes:
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      - ./wordpress:/var/www/html
    networks:
      - blog-network
  # Webserver
  caddy:
    image: caddy:alpine
    container_name: webserver
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./wordpress:/var/www/html
      - ./caddy_data:/data
      - ./caddy_config:/config
      - ./Caddyfile:/etc/caddy/Caddyfile
    networks:
      - blog-network
networks:
  blog-network:
    driver: bridge

docker-compose.yml

The above docker-compose.yml requires .env file to contain password and others environment variables:

MYSQL_ROOT_PASSWORD=
MYSQL_USER=
MYSQL_PASSWORD=

.env

Create a php.ini file to allow big file update to your wordpress instance:

memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M

php.ini

Create folder to store caddy config, database and the wordpress code for custom development:

$ mkdir caddy_data caddy_config wordpress db

Step 2: Caddyfile

example.com {
        root * /var/www/html
        php_fastcgi wordpress:9000
        file_server
}

Caddyfile

Your folder should look like this now:

Step 3: Set up WordPress

To start, let’s run docker:

$ docker-compose up -d

Open the domain in the web browser

Powered by WordPress & Theme by Anders Norén