- Home
- >
- Software Development
- >
- How Lessons to Speed Up Swift Can Work With Other Programming Languages – InApps 2022
How Lessons to Speed Up Swift Can Work With Other Programming Languages – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn How Lessons to Speed Up Swift Can Work With Other Programming Languages – InApps in today’s post !
Read more about How Lessons to Speed Up Swift Can Work With Other Programming Languages – InApps at Wikipedia
You can find content about How Lessons to Speed Up Swift Can Work With Other Programming Languages – InApps from the Wikipedia website
The Udemy online academy recently published a Top 10 Tips to Speed Up Swift Learning, and here I’m looking in depth at five of them, but in the wider context of including other programming languages, not just Swift.
1. Use Type Inference
The better programming languages have shorter and simpler syntax that makes source code easier to read and understand. For example, Java, though popular, doesn’t have type inference. When you declare a variable you have to specify the type, even if you assign an initial value. Using type inference increases code readability.
The example below is from the AWS SDK for Java on GitHub. Languages that have type inference include ML, OCaml, F#, Haskell, Scala, D, Clean, Opa, Rust, Swift, Visual Basic (from 9.0), C# (from version 3.0) and C++11.
In Java, it’s:
AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics(); |
In C# it’s this shorter version:
var awsRequestMetrics = executionContext.getAwsRequestMetrics(); |
The IDEs need to be able to infer the type from the right hand side, and, of course, you can only use type inference when there is a value to assign. If you declare fewer uninitialized variables, you’ll have fewer bugs.
2. Value Types Are Passed by Copy
From C onward, many programming languages have passed value types by copy into functions. Value types are usually simple types, like ints and floats. The opposite types are reference types, such as strings and instances of objects. Reference types are often 32 bits long (on 32 bit architectures) and point to (or refer to) a block of memory elsewhere.
When you pass a value type variable into a function call, like in this C example below, the value in variable b is passed in to variable a by copying. Inside the function dodouble(), the passed-in value in a is multiplied by 2, so it has the value 20. The external variable b, though, remains unchanged, and still has the value 10 in the print.
#include <stdio.h>
int dodouble(int a){ a = a * 2; return a; } int main() { int b=10; printf(“b=%d b*2= %d”,b,dodouble(b)); return 0; } |
3. Make Things Generic
One of the principles of programming is D.R.Y. — Don’t Repeat Yourself. One way to do this is use generics (aka templates). Say you have a function to sort ints. Instead of writing an almost identical function to sort floats, you convert it to a generic function that works with any sortable type. Then you call the function for a particular type, say an array of floats, and it sorts it irrespective of the type, as long as it’s possible to compare two elements of the same type.
This example works with any value type T, and sorts the array arr in place using BubbleSort. The where T : IComparable restricts the use of this to a type T that implements IComparable. You can add this to any type by adding a method int CompareTo(T t) which compares t with this, and returns -1, 0 or 1, depending on whether the comparison is less than, equal to or greater than. In this example, BubbleSort is called twice, once each on an array of ints and an array of floats. These have implicit CompareTo, so you don’t need to specify it.
Normally, I wouldn’t ever use bubble sort as it’s inefficient, but it serves to illustrate the point.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class Program { static void Swap(ref T x, ref T y) { T t = y; y = x; x = t; }
public static void BubbleSort( T[] arr) where T : IComparable { for (var i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length – 1; j++) { if (arr[j].CompareTo(arr[j + 1]) > 0) Swap(ref arr[j], ref arr[j + 1]); } } }
static void Main(string[] args) { int[] vals = {98, 65, 1, 2, 8}; float[] valf = {9.8f, 2, 3f, 0.0f, 10.7f, 12.5f}; BubbleSort(vals); BubbleSort(valf); foreach ( var val in vals ) { Console.WriteLine(val); } foreach (var val in valf) { Console.WriteLine(val); } } } |
4. Representing the Lack of Value: Optionals
Swift has a very neat way of dealing with reference types that can be null, and value types that can’t be null. It lets you wrap both types with an enumerate value, called an optional. Some other programming languages support similar concepts. Java has wrapper classes for primitive types, and C# has nullable types. The next version of C# (6.0) has Swift-like checking of nullable types.
List lines = ParseSourceCodeFile(“Program.cs”); return lines?.Count ?? 0; |
This reads in the file program.cs. If this fails, perhaps because the file is locked or absent, then the variable lines will be null and the null coalesce operator ?? will return 0.
It simplifies the syntax, removing the checks for null, and makes for slightly more functional-like programming.
5. Tuples
Languages like Swift, Java, C#, Lisp and Python support tuples. A tuple is an ordered set of values of the same or different types. In C# it’s a class, but unlike a class the fields aren’t named. It’s also not like a List, as the fields aren’t the same type.
A common use is to return a 2-tuple from a function call, with one of the elements being an error code and the other a computed value. Or a tuple can hold a single data set, with the components representing individual fields. In C# there are predefined constructors for one to seven values, with an eighth parameter that can hold more tuples, making, in theory, any number of tuples possible, albeit a bit clunky.
In many languages tuples are immutable, though in Swift you have to specify it using let for read-only instead of var.
Conclusion
These programming tips all apply to Swift, but all of them were drawn from existing programming languages. It’s quite often the case that having mastered one programming language, others are easier, as you’re just filling in the gaps.
Feature image: “Star trails at Hopkins Prairie, Ocala National Forest” by Ryan McKee is licensed under CC BY-ND 2.0.
InApps is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Udemy.
Source: InApps.net
Let’s create the next big thing together!
Coming together is a beginning. Keeping together is progress. Working together is success.