CODEX

Learn eight useful tricks to write professional-looking code.

Original Image: Ales Nesetril

I’ve collected a list of useful tips and “tricks” you can use to write professional Swift code.

1. Shorter If…Else Statements with Ternary Operators

Ternary operators allow us to make if...else statements shorter.

condition ? true : false

For example, let’s compress this if-else expression:

let money = 100if money > 0 {
print("Some money")
} else {
print("No money")
}

This can be written as a one-liner:

money > 0 ? print("Some money") : print("No money")

2. Destructuring Tuples

Let’s implement a function that returns a tuple that contains a name and an email address:

func getInfo() -> (name: String…


Step by step guide

An animated gradient on an iOS device
An animated gradient on an iOS device

Here is the link to the complete code for the animated gradient ViewController.swift. To learn how it’s done, stick around.

Set Up the Gradient

Choose the colors

A gradient is a powerful effect. Choosing colors that look good together is essential to make it stand out. You may find color samples in uiGradients useful.

Define the Colors in Your Project

I use these three RGB colors in my gradient:

let color1: CGColor = UIColor(red: 209/255, green: 107/255, blue: 165/255, alpha: 1).cgColorlet color2: CGColor = UIColor(red: 134/255, green: 168/255, blue: 231/255, alpha: 1).cgColorlet color3: CGColor = UIColor(red: 95/255, green: 251/255, blue: 241/255, alpha: 1).cgColor

Create a Still Simple Gradient First

Let’s initialize an empty gradient layer to the…


CODEX

What are they? What are the differences? Which should I use?

Photo by Markus Winkler on Unsplash

Structures and classes are important building blocks of your code. But what are the differences? Which one should I prefer?

The Short Answer

Structs and classes support properties, methods, subscripts, and initializers. They can be extended and both can conform to protocols.

Reference Types

In Swift, classes are reference types…


Learn these elegant tricks and shorthands for writing professional JavaScript code.

Photo by Martin Shreder on Unsplash

I’ve collected a list of useful JavaScript tricks for you to make your code look more elegant and professional. I hope you enjoy it!

1. One-Liner If-Else

In JavaScript, you can use a ternary conditional operator to compress your if-else statements.

Remember, this shorthand is meant to make the code cleaner. Use it only when it clearly makes the expression more understandable.

2. Null Coalescing

The null coalescing operator ?? returns the right-hand side if the left-hand side is null. Otherwise, it returns the value on the left. This is useful because it allows you to omit lengthy if-checks.


Learn the everyday language of web developers and web designers

Learn the language of web development and web design that startups commonly use.

Agile Web Development

Agile is a common buzzword in the tech industry.

Algorithm

An algorithm is a set of instructions for a computer to perform a task. …


Start using yield in JavaScript

Hot-air balloons
Hot-air balloons
Photo by Ricardo Rocha on Unsplash.

In JavaScript, yield is used to pause the execution of a function. When the function is invoked again, the execution continues from the last yield statement.


Learn all the basics of working with lists in Python.

Photo by Chris Ried on Unsplash

A Python list is a common data type. It’s used to store elements, such as integers or strings. A list is a mutable collection. You can add, remove, and modify the elements of a list.

numbers = [1,2,3,4,5]

How to Create a List in Python

In Python, you can create a list by comma separating elements inside square brackets.

num_list = [1,2,3]word_list = ["cat", "car", "dog"]mixed_list = ["cat", 1, 2, "three"]

Notice how you can insert different types of items into the same list. …


Learn what associations colors have and how businesses benefit from them.

Photo by Pawel Czerwinski on Unsplash

Colors are everywhere. They can affect your mood. Green calms you down, and red fires you up.

What Is Color Psychology

Color psychology studies how colors affect human behavior.


Did you know JavaScript keeps your function scope alive after execution?

Photo by Juanjo Jaramillo on Unsplash

Closures in JavaScript mean that an inner function has access to variables of the outer function. This also applies when the execution of the outer function has finished.

How Regular Functions Work in JavaScript

Let’s briefly see how a regular function works in JavaScript.

function hello() {
const word = "Hello";
console.log(word);
}
hello();

Output:

Hello

When hello() is called, the code in between the curly braces runs. During that time, the function hello() can access and use its variable word.


What makes a color neutral? Why should you use neutral colors?

Photo by Amy Humphries on Unsplash

A neutral color is that perfect background color that isn’t loud. It highlights the rest of the colors and does not combat for space.

Artturi Jalli

Tech Expert, Entrepreneur, Software Engineer from Finland. Writing about programming and science. Check out my blog at https://www.codingem.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store