CODEX

Learn eight useful tricks to write professional-looking code.

Original Image:

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

I hope you enjoy it.

1. Shorter If…Else Statements with Ternary Operators

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

The syntax is:

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

Here is the link to the complete code for the animated gradient . 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 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…


How do computers know the time? What is the year 2038 problem?

Photo by on

The Epoch time or the Unix time is the number of seconds since January 1, 1970.

At the time of writing, the Epoch time is 1632221080 and counting.

The Epoch time is the same regardless of where you are on the globe.

Epoch time is used by computers as a “starting point” of time.

Why Epoch Time Is Necessary?

To track the time, the computer needs to know from where to start.

The Epoch time is a popular convention for computers to track the time. The Epoch time starts at 00:00:00 Thu 01 Jan 1970 UTC and is measured in seconds. …


Python range function returns a sequence of values you can loop over.

For example, let’s print values from 0 to 5 using the range() function in a .

for number in range(0,6):
print(number)

Output:

0
1
2
3
4
5

You can also take bigger steps with the range() function. To do this, provide the range() function with a third argument that is the stepsize.

For example, let’s print values from 0 to 50 by taking steps of 10:

for number in range(0, 60, 10):
print(number)

Output:

0
10
20
30
40
50

To create a range in descending…


Understand different types of betting odds

Photo by from

Betting odds tell you how much you can win from a bet. You can also use them to spot underdogs and favorites.

There are three main formats for showing odds:

  1. American Odds
  2. Decimal Odds
  3. Fractional Odds

Today, you are going to learn what each of these odd types means and how to spot underdogs and favorites in them.

1. Understand the American Odds


Learn how to replace if statements with a guard statement.

Photo by on

Swift’s guard keyword can replace if statements and improve your code quality. The guard prevents your app from crashing due to invalid data.

The guard statement works by transferring the program control out of scope when one or more conditions are not met.

An example of using a guard in Swift. Here the guard makes sure no numbers are printed out.

The guard statement is similar to the if statement. But there’s a difference: The if statement runs when a condition is met. The guard statement runs when a condition is not met.

Swift Guard Syntax

The syntax of the guard statement…


Give another meaning to a common operator in a different context

Desktop screen: “Do what is great.”
Photo by on

Operator overloading in Python means giving another meaning to a common operator in a different context.

For example, you can sum up two integers with the addition operator +:

1 + 2

But you cannot sum up instances of a custom class by default:

apple + banana

But you can change this. To use the addition operator + for custom objects, you need to overload the operator +.

Today you learn how to overload operators to make Python operations possible on your objects.

Special Methods in Python

Before jumping into operator overloading in Python, it’s important you understand the concept of special methods (also…


Learn what is the difference between the string representation methods in Python

Photo by on

Both str() and repr() methods are used for the string representation of an object. The difference between str() and repr() is that str() returns a user-friendly version of an object. The repr() method returns a developer-friendly string representation of an object.

Example

Let’s create a date object for today, and inspect its string representations:

import datetime
today = datetime.datetime.now()
print(str(today))
print(repr(today))

Output:

2021-08-14 08:18:25.138663
datetime.datetime(2021, 8, 14, 8, 18, 25, 138663)
  • str() displays today’s date so that an end-user could understand it.
  • repr() prints the developer-friendly representation of the date object. …


Start growing your audience the right way

Photo by on

According to Medium Partner Program , an author needs to have 100+ followers (and at least one published story) to be eligible to become a partner. And only partners can earn on Medium

So, if you’re new to the platform, you want to reach the 100 followers mark to start getting compensated for your work.

Today you are going to learn what it takes to get the first 100 followers and how to become a top writer on Medium.

The Truth Is Hard Work

Instead of using “growth hacks” or any other shortcuts for success, I recommend you start doing it the right way from…


JSON or JavaScript Object Notation is a widely used lightweight data format.

Photo by on

JavaScript Object Notation (JSON) is a text-based data format for representing structured data. It is commonly used for transmitting data in web applications.

For example, JSON can be used to send data from a server to the client to show a web page.

Here is how JSON data might look like:

{
"name": Jack,
"age": 30,
"married": false,
"hobbies": ["Jogging", "Tennis", "Gym"]
}

Today, you are going to learn what JSON is, why it is so popular, how to produce JSON, and how to work with it in JavaScript.

What Makes JSON So Popular?

JSON is widely used because it is

  • Lightweight—JSON is nothing more…

Artturi Jalli

I’m a tech expert, entrepreneur, and software engineer from Finland. I write about programming, tech, and science. Visit my blog at .

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