In software development, we optimize for readability since code is read more than its written. Named parameters are one way to improve code readability. Let's say we have a function that calculates the distance between two points. Let's take two-point Point
class instances representing the start and end positions. When calling the functions, using positional arguments it's not very clear what is being referred to.
In Python:
In Dart:
In Swift:
On the other hand, using named parameters makes function calls more expressive, it's clear to the reader what the arguments represent. This becomes helpful particularly as the number of arguments grows. Let's add one more argument. In the examples below when calling the functions it's not very clear what the third argument is when it's being read.
Using named parameters is really helpful in such a situation. In the three languages, it's possible to specify that the parameters should be passed as named parameters. In Python, we can add a *,
before the parameters, in dart we can add curly braces around the parameters, and in swift, we can remove the underscore before the parameter names.
Top comments (0)