Skip to main content
66 votes
Accepted

Struggling with cyclical dependencies in unit tests

You're worrying about implementation details too much. It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should ...
Philip Kendall's user avatar
29 votes
Accepted

Is it good practice to wrap a related set of properties into its own struct/class?

You will either need zero of a thing, one of a thing, or an arbitrary number of the thing. I'm shocked that your design predicts that the number of needed links will always be three and that you ...
candied_orange's user avatar
12 votes
Accepted

Why doesn't Swift allow Int String subscripting and integer ranges directly?

Swift strings are made of characters. Characters can be made of any number of Unicode code points. If you have a very long string, and you want to access the one-millionth Character, you'd have to ...
gnasher729's user avatar
  • 49.4k
12 votes
Accepted

The right place for "app logic" in MVVM context

In MVVM, the business logic is built into the Model. The ViewModel is there to bridge between the View and the Model, so it's logic only pertains to driving the display and updating the model from ...
Berin Loritsch's user avatar
9 votes

Swift functions vs computed properties

Use countOfAttendees and countOfPaidAttendees(). A computed variable is one that returns a calculated value each time it is accessed. That is, it doesn’t store a value. Internally it is implemented as ...
Jano's user avatar
  • 267
7 votes

Does chaining higher order functions mean worse performance?

Performance depends on many factors. Language. Compiler optimizations (a compiler may rewrite the first version into the second one, or the other way around, or transform both versions in the exact ...
Arseni Mourzenko's user avatar
7 votes
Accepted

Does swift value type like String, Array, Dictionary internally contain class

String, Array, and Dictionary are all structs (that get stored on the stack), but their dynamically sized "content" is stored in a buffer allocated on the stack. This buffer is part of an object, ...
Alexander's user avatar
  • 5,205
6 votes

Is it good practice to wrap a related set of properties into its own struct/class?

You are on the verge of falling into the "I see a technical possibility" trap. Just because you see a common trait among items does not mean it makes sense to apply some aggregation on them. The ...
Martin Maat's user avatar
  • 18.6k
6 votes

What is a good approach for saving a Swift model that's a struct containing structs to Core Data?

You have some options: 1 - Convert them to class. So you can inherit from NSManagedObject and do the rest like the tutorials you usually find in internet. 2 - Make a rapper around any struct you ...
Mojtaba Hosseini's user avatar
6 votes

What is a good approach to naming when modeling a sport that can be between either individuals or teams?

A player is a player, regardless of how many of them make up a team. A team is a team if there is more than one player per side. You could go with "Side" as a name, but I think you are missing an ...
Greg Burghardt's user avatar
5 votes

Struggling with cyclical dependencies in unit tests

How exactly do you get around these sorts of issues? You revise your thinking on what a "unit test" is. An object that manages a mutable data in memory is fundamentally a state machine. So any ...
VoiceOfUnreason's user avatar
5 votes

Guidelines for using extensions in Swift

The community convention is that libraries should never define conformance of a type that isn't theirs (in this case, CoreLocation.CCLLocationCoordinate2D) to a protocol that isn't theirs (in this ...
Alexander's user avatar
  • 5,205
5 votes

How do I mock API responses with security in mind?

Rather than sprinkle such conditionals throughout your program, refactor the code so that all communication goes through a single, minimal communications specialist. Then replace that specialist with ...
Kilian Foth's user avatar
4 votes
Accepted

Dependency Inversion Principle (Swift) - Applicable also without polymorphism? (Abstraction: constrained generics)

Disclaimer: I don't know Swift. With that out of the way - if Swift generics are anything like C# generics, and it looks like they are quite a bit, I wouldn't say that Policy "knows" about the ...
Filip Milovanović's user avatar
4 votes
Accepted

Localize a countdown timer

The format you have adopted is confusing: it uses the same format than HH:MM:SS but with a different meaning. The appropriate time format in many countries is ISO 8601. It foresees a decimal ...
Christophe's user avatar
  • 82.3k
4 votes
Accepted

Swift: Best way to store crypto amounts?

To add some information to Jimmy's answer. If you still think that you can do financial calculations using Float or Double, try this code snippet, that adds a couple of cents, one by one: let ...
Christophe's user avatar
  • 82.3k
4 votes
Accepted

Is it okay for an Interface Adapter / Repository / Gateway to use Entities in its implementation

The More General Part of the Answer I'm going to answer the question you stated in your closing line, but there's a caveat - see below. Is this okay? Considering the Interface-Adapter and Gateway are ...
Filip Milovanović's user avatar
4 votes
Accepted

Swift: What is the rationale behind forbidding named parameters for closures?

This was decided in SE-0111, Remove type system significance of function argument labels, which cites the following motivation: Motivation ... As currently implemented, this feature can lead to ...
Alexander's user avatar
  • 5,205
3 votes
Accepted

At what size array in Swift would you consider switching to CoreData?

I don't think your UI got laggy because the application cannot process data arrays with hundreds of entries. 1000 strings with a length of, say, 100 characters cost around 100kB memory, that's less ...
Glorfindel's user avatar
  • 3,167
3 votes
Accepted

One-field interfaces

Use A. As you say yourself it is safer and the intention of the code is clearer. I think you may have a misunderstanding regarding coupling. An Album is supposed to be coupled to an Artist. Using a ...
JacquesB's user avatar
  • 62.4k
3 votes
Accepted

How to break down complex if conditions that include optional binding?

There are a few different approaches we could take to simplify your example code. Make them not optional (aka null objects and default values) The most direct way to not worry about unwrapping an ...
cbojar's user avatar
  • 4,261
3 votes
Accepted

Struct vs class in Swift

The swift struct only needs to know if it's data is uniquely referenced or not, and not needing a full-fledged ARC. For example, on the line var p2 = p1 the compiler could theoretically just flip a ...
JSquared's user avatar
  • 221
3 votes
Accepted

Should I write out computed vars that all depend on one other var, or should I assign them as normal vars in that single var's setter?

Yes, if you move the computation to the getter of these properties, then it will be performed only when you actually read those computed properties, and not every time whether it's needed or not. ...
Kilian Foth's user avatar
3 votes

How to Replace Many if Statements for many types

A so-called "state table" or "finite-state machine (FSM)" design might be helpful here. The algorithm starts in one "state" and moves to other "states" as time goes on. When the algorithm is ...
Mike Robinson's user avatar
3 votes

Swift: Best way to store crypto amounts?

I know nearly nothing about swift but it looks like that Decimal is what you need. Neither float nor double can represent all decimal values exactly because they use binary fractions: x0.1 = 1/2, x0....
JimmyJames's user avatar
  • 31.1k
3 votes

Swift: Best way to store crypto amounts?

You should use the same unit as the underlying cryptocurrency. With dollars, we are advised to store cents because that is the actual smallest unit. Certain applications may want to store tenths of ...
Stack Exchange Broke The Law's user avatar
3 votes

Is it okay for an Interface Adapter / Repository / Gateway to use Entities in its implementation

No. What are the structures you asked about? Unless the application you're developing is a deployment tool, those are not entities because they are not part of the application domain. I would classify ...
Rad80's user avatar
  • 376
3 votes
Accepted

Design for the future or make it tightly coupled to the implementation

Dependency injection helps code reuse, and unit tests are often one kind of code reuse you need. But if you can test the real deal without needing to inject a mock, then you don't really need the DI, ...
Alexander's user avatar
  • 5,205
3 votes
Accepted

When should tuples be used as an argument instead of an array?

Consider how the calling function will hold these objects - will it need to construct an array or tuple just to call this new function, or will it already have them in an array or tuple for other ...
mmathis's user avatar
  • 5,586
3 votes

When should tuples be used as an argument instead of an array?

Does position matter or order? If you accept an array I expect the logic to gracefully handle arrays of different lengths (longer or shorter). Insisting that the array is sorted is fine. But caring ...
candied_orange's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible