Better HN
topnewbestaskshowjobs
Log inSign up

owensd

8 karma · joined Sep 26, 2014 (12y ago) · 7 submissions

http://owensd.io
Submissions

comment 10y ago · link · parent

Yeah, we do. However, since our primary audience of people we are talking to are on a Mac/iOS device, it doesn't really matter. And when we are on a non-Mac, we know what the square box symbol is supposed to be.

comment 11y ago · link · parent

First off, a struct is not an object. Structs in Swift, like many languages, are value types. They have the same packing structure in Swift as in C, especially in the example that I gave.

You can read more about them here: https://developer.apple.com/library/ios/documentation/Swift/...

I can write the same problem with components of the array as bytes if you prefer, but then performance will be even worse. The root cause of the performance issues is the array access and set. The more the do, the worse the performance.

So what you are saying, is that no person would ever create an array of data and expect to do anything with that data? And they would never do that in a processing loop? Ok...

If you actually read the blog post, you can see that I already gave an example using raw buffers (the UnsafeMutablePointer<Pixel>). Which means that every index into the pointer moves 4 bytes. Again, using UnsafeMutablePointer<Byte> is not doing anything other than creating a harder API surface to work with.

Also, the UnsafePointer types exist because without them, there would be no way to interact with C code. And if I need to use the UnsafePointer types everywhere I have arrays so that I can have reasonable performance in debug and release builds, then there is a bigger problem that needs to be addressed.

comment 11y ago · link · parent

Yes, the var should have been inout. It had an affect on the release build performance (twice as fast), but not the debug build. I've updated the blog post.

The performance problem is regarding all retain/releases though; not the copy of the data. That's why the debug builds are not affected.

comment 12y ago · link · parent

> I can promise you my function doesn't write to disk, doesn't output to the screen, etc. You cannot promise the same with your function.

WHAT?!

Your type signatures have absolutely no assurances with regards to side effects. They cannot even make a claim that the function is thread safe, let alone that it doesn't write to disk our output to the screen.

I'm baffled at why you think that is true:

    int foo(int bar):
      // network call here
      // write a log to disk here
      // change a global value here

      return happy_int
And you are woefully mistaken about about this claim as well: "it must work with the list I passed it, because it doesn't know anything else."

Many languages that actually have good generic type systems allow for type specialization. That means that I can provide different implementations for different types. So in the contrived example of you doing something completely different with my list of ints in the dynamic version is completely possible in many statically typed languages too.

comment 12y ago · link · parent

> Please note I didn't throw out add(3,4)==7, but instead pointed out it's terribly inadequate as a test. Additional testing tools must be employed; unit testing alone of this kind is not enough.

I don't follow what you are saying here at all... It's an API test with no external integration points, what other kind of tests besides unit tests would you have? Your `add(x, y) = add(y, x)` are still unit tests.

Also, you said that you would "rule out testing specific values such as 3+4=7". You have to test specific values; the contract of the function is:

    f(x, y) = z
Where z is the mathematical sum of x and y.

Specific value testing is the only way you can verify that claim for a given set of inputs.

>> The only thing that static typing provides, in regards to test cases, is this [example]

> This assertion is wrong. Static typing done well provides a lot of things "for free", such as restricting incorrect behavior.

This was taken out of context; this was in reference your to "generator" of test values for X and Y. The type signature alone is inadequate to generate test values.

Regardless,

    f :: [a] -> a
Is no harder to test in a dynamic language.

    let r = f([...])
    assert(r, correct_value)
    assert(r.type, correct_type)
It's up to the contract of the function to determine what, if any, validation needs to be done on the input. This is true regardless of type system. The only question is this: do you also check the type.

Again, type is only _one_ of the constraints that get applied to parameters. In the add function example, the other constraints are:

    1. x <= TYPE_MAX (for free in a statically typed system)
    2. x <= TYPE_MAX - y
    3. y <= TYPE_MAX (for free in a statically typed system)
    4. y <= TYPE_MAX - x
Plus the similar for TYPE_MIN. In the `addbase2` example, additional constraints are:

    1. x power of 2
    2. y power of 2
In this example, we still need to add verification for two-thirds of the constraints.

On the flip-side, with generics, especially with the type of generics we see in Swift (using the `f :: [a] -> a` example), you'll probably need to model constraints of the collection, the element type of the collection, and the type of indexer that is being used if you wish to make your function actually work.

And then your implementation only works for those that rigidly adhere to the type conformance, where as the dynamic one can work for any type that conforms to the protocol, whether loosely or explicitly.

This flexibility is very powerful, is not hard to code safely around, and requires significant less code gymnastics before you can even get your code compiling.

comment 12y ago · link · parent

There are actually many poor claims you made in your posts about "good tests".

> I'm aware that addition is a toy example, but suppose we want to test our implementation: > Except for very simple verification, to exclude obviously broken implementations, I'd rule out testing specific values such as 3+4=7. And, like you said, performing an exhaustive exploration of all values is out of the question. > So I'd try property testing instead. Relevant properties in this case are associativity, commutativity, etc. > As an example, I'd try writing properties such as: > for all X, Y: add(X, Y) = add(Y, X)

These properties can also be satisfied by implementations of add() that: - return a constant value - return the smallest number of (x, y) - return the largest number of (x, y)

The cases you threw out as an "obviously broken implementation" are required to actually validate that functionality of the method. The functionality of the method is also one of the properties of the method.

You can write it in a more generic way than simply: assert(7, add(3, 4)). However, those tests are _also_ required. Without them, you never actually test that the `add()` function does what it's supposed to: add two numbers together.

Regardless of type system, you also have to worry about underflows and overflows - another property of the functionality of the method.

> You are right, without additional information property testing would be less useful. Which is yet another reason to favor static typing in my opinion.

Static typing doesn't help you constrain sets of inputs; it may not be valid that your method accepts all ranges of integers. You could have a method `addbase2(int x, int y)` that is to be used only when x and y are powers of two because of an optimization you perform in that method. Static typing doesn't help you generate the correct input set for x and y.

The only thing that static typing provides, in regards to test cases, is this:

    def add(x, y)
      assert x is int
      assert y in int

      return x + y

    // test cases
    assertIsThrown(add("foo", "bar"))
That was the test case you had.

Regardless, the point of the article was not about static typing being bad. There is value it. However, there is also value in not being so rigid in your type system that things don't work well.

    add((short)0, (long)1)   // compiler error if you have an extremely rigid type system
Generic systems typically swing the pendulum far to the right requiring an extremely rigid type system. That always causes pain. The question you have to ask, is the ROI worth it. For some, it is. For others, it's not.

comment 12y ago · link · parent

I'm just going to reply to one part with an example of the problem.

> I think this is somewhat incorrect. You should never just be type-casting your inputs. (In fact, I think it should ideally be impossible to do so without the compiler generating really big flashing warnings saying "THIS IS DANGEROUS!"). The static verification here will prevent you from doing silly things, and should ideally force you to do input validation at the location of input, instead of blindly casting things to the type it needs.

I did not say anything about type-casting inputs. I said coercing values into a given type. The naïve approach can be to type-cast, the other way is to write the code for the coercion process.

    // some collection we are holding the values in for some reason
    NSMutableArray *inputValues = [[NSMutableArray alloc] init];

    NSString *someInputValue = // probably read from user input or a file
    NSInteger value = [someInputValue integerValue];

    BOOL validInput = YES;
    if (value == 0) { // we need to check that there really is a value of 0...
        NSString *trimmed = [someInputValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
        NSString *trimmed0 = [trimmed stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]];
        if (trimmed0.length != 0) {
            // oops, actually had an error... handle it
            validInput = NO;
        }
    }
    
    if (validInput) {
        [inputValues addObject:@(value)];
    }
Of all of the places where could have had errors along the way, the last `[inputValues addObject:@(value)];` doesn't really concern me that much.

Also, the compiler doesn't help me get things correct... the only thing it would have helped me do is make sure I put an integer into the array, not that I had the right values in the array. Had I not known that `integerValue` returns `0` in its error cases, I would have not known that I need to write some additional code to verify the string value was indeed a zero.

And generics only helps you when you have collections of identical types. Storing arrays of plist entries, for instance, requires you to revert back to `AnyObject` (or similar).

Generics can be helpful, if it's done well. However, even in .NET's generic system, with all the limitations and constraints it put in, there are many times where it still gets in the way.

I'm tired of fighting with my tools just to get the job done. Currently, Swift makes me fight a heck of a lot more then I want to or need to just to make the compiler happy. The end code I write is the same both ways, but the Swift code has a lot more annotations and is a lot less flexible.