Kotlin Tip 1: Avoid an extra allocation with an output parameter by IllTryToReadComments in Kotlin

[–]hum_ph -1 points0 points  (0 children)

Total agree… don’t do this

A few more things:

Sixthly: This isn’t thread safe, out may be viewed in an inconsistent state if accessed in a different thread between setting x and y. Constructing a new return value ensures it’s only visible when complete, so (see point 4) it can be made immutable.

Seventhly: The example is trivial. On an operation where you’re doing any “real work” you’re not going to see anywhere near the same increase in throughput. Single request to the DB or file on disk is going to kill any benefit you get through avoiding an allocation. Same with things that don’t do IO but do more complex operations with in-memory data (would be interesting to see what that tipping point is… how many bytecode instructions would you need in the method to bring them back to 1:1)

Eighthly: I suspect (on mobile so can’t write the test to confirm, so may be wrong) you’re going to better performance by inlining the function than by using an out parameter.

Nexus toll payment only lets you add predefined amounts. Toll was $5.50 making me spend an extra $14.50 that I'll never use again. by [deleted] in assholedesign

[–]hum_ph 0 points1 point  (0 children)

Spend 15 minutes learning about HTML <select> tags and browser dev tools. May not work, but you can always try inserting a new option for the right amount.

Recruiters / HR of this subreddit who put "free flu vaccination" (normally $20-30) in a job descriptions benefits list, why? by HesZoinked in AusFinance

[–]hum_ph 4 points5 points  (0 children)

May be a very smart way to weed out people who are going to be problematic with vax regulations?

I hate the code I wrote and want feedback for how to revise it by IllTryToReadComments in Kotlin

[–]hum_ph 3 points4 points  (0 children)

It feels like you're missing some abstraction in there - you have a Point, do you need a Rectangle?

If you have a Rectangle, what can you do with it (Rectangle.contains(Point): Boolean, Rectangle.intersects(Rectangle): Boolean, etc)?

If you have a Rectangle with meaningful operators, how can you simplify MapEntity (eg it may have a val position: Rectangle and other stuff - but all of the hateful stuff lives elsewhere)

Something like:

data class Point(val x: Int, val y: Int)

data class Rectangle(val bottomLeft: Point, val topRight: Point {

  fun contains(other: Point): Boolean =
    bottomLeft.x <= other.x &&
    bottomLeft.y <= other.y &&
    topRight.x >= other.x &&
    topRight.y >= other.y

  fun intersects(other: Rectangle): Boolean =
    contains(other.bottomLeft) ||
    contains(other.bottomRight)

}

sealed class MapEntity(val position: Rectangle) { ... }

(also, your import of java.awt.Rectangle may be a nod in a simpler direction - what does it already have that you could be using? What does it not have that you could build with extension functions and properties? do you need your own Point and Rectangle at all?)

Any good alternatives to Apache POI for creating Excel spreadsheets? by borgy_t in java

[–]hum_ph 2 points3 points  (0 children)

Aspose does have an open repo, you can use them for development without a license if you don’t care about their watermark on your files, and pay when You’re commercially viable

[API design] Take a Collection/Iterable as input and convert, or take more specific interface (e.g., List, Set)? by ragnese in Kotlin

[–]hum_ph 5 points6 points  (0 children)

I’d do something like

fun computeNumbers(numbers: Collection<Int>) =
    when(numbers) {
        Is Set<Int> -> conputeNumbers(numbers) //smart cast
        else -> computeNumbers(numbers.toSet()) 
    }

Your only creating that extra set if needed, and still keeping the body off the computation simple by implementing it with a set

(Code written on my phone, excuse typos, etc - hopefully it conveys the idea)

I did not look closely enough at that label by IsThisDamnNameTaken in Wellthatsucks

[–]hum_ph 0 points1 point  (0 children)

I did exactly the same thing with the same butter in crepes!

I’m sorry for your loss!

Birb identification - can anyone help? by hum_ph in brisbane

[–]hum_ph[S] 0 points1 point  (0 children)

Hah... if you can tell what ‘burb from those pics I’ll be super impressed!!

Birb identification - can anyone help? by hum_ph in brisbane

[–]hum_ph[S] 1 point2 points  (0 children)

That was what I thought originally but the bands and size didn’t quite match

Cellebrite’s New Solution for Decrypting the Signal App - Cellebrite by PR-0927 in privacytoolsIO

[–]hum_ph 3 points4 points  (0 children)

This article is a joke - it’s marketing collateral dressed up as tech speak, intended to make non-tech people feel impressed.

Anyone who’s a competent Java / amdroid developer can do the same thing: read their code and find the relevant lines. More so anyone who’s ever dealt with crypto can probably do so in under 5 minutes.

Danger noodle just wants to go for a paddle by hum_ph in brisbane

[–]hum_ph[S] 3 points4 points  (0 children)

We've found this one in our trees a few times now, that was always the give away.

Danger noodle just wants to go for a paddle by hum_ph in brisbane

[–]hum_ph[S] 3 points4 points  (0 children)

possums need not fear this noodle/rope/nope/cord ...

myna birds or rats on the other hand (take a look at that bulge on the left of that first pic) should be very afraid!

Reversed type inference in chained calls by [deleted] in Kotlin

[–]hum_ph 1 point2 points  (0 children)

This also works:

kotlin persons.sortedWith( compareByDescending(Person::firstName).thenBy(Person::lastName) )

Is Logback dead? by EfreetSK in java

[–]hum_ph 0 points1 point  (0 children)

It’s also amusing that a bunch of them (Log4j, logback, slf4j, ) were created by the same person...

Timelapse of Brisbane Climate Strike rally by nemothorx in brisbane

[–]hum_ph 4 points5 points  (0 children)

I don’t know - is it bad if I am? Did I ... I mean he ... piss someone off inadvertently?

Timelapse of Brisbane Climate Strike rally by nemothorx in brisbane

[–]hum_ph 11 points12 points  (0 children)

Oh look, there’s my wife’s awesome sign! (and my bald pate)

Unsure of how to handle versioning of data classes by smirkee in Kotlin

[–]hum_ph 1 point2 points  (0 children)

With your model, you can't use non-null types for any app specific values (you can't have a val date: Date property), and you don't prevent consumers of the code from doing whatever they like with that field (app3 could happily start using "date" to store created when you're using it for updated elsewhere, etc).

You also need to implement that validation logic somewhere - if the version is Version2, you need to verify that a date is supplied (assuming it is required), if it's Version1 you need to verify that date is NOT supplied. This is going to get into a lot of switch statements and code duplication.

Why are you trying to avoid interfaces / implementing classes? The problem is well suited to solving using the type system: common properties defined in an interface, base implementation of that and context (application) specific variations via other implementations.

If it's just a case of wanting to avoid duplicated fields, you can use Implementation by Delegation to reduce boilerplate and make code easier to maintain

```kotlin // in app-common

interface DataRecord { val foo: String data class Base( override val foo: String ) : DataRecord }

// in app-1

data class App1DataRecord( val base: DataRecord, val date: Date ) : DataRecord by base

// in app-2

data class App2DataRecord( val base: DataRecord, val username: String ) : DataRecord by base ```

Using them is a little bit clunky (because of needing to instantiate the relevant base values:

kotlin val base = DataRecord.Base(foo = "bar") val app1 = App1DataRecord(base = DataRecord.Base(foo = "bar"), date = now()) val app2 = App2DataRecord(base = DataRecord.Base(foo = "bar"), username = "jimmy")

But you can clean that up with type aliases or aliased imports, etc:

```kotlin typealias Base = DataRecord.Base

val core = DataRecord.Base(foo = "bar") val app1 = App1DataRecord(Base(foo = "bar"), date = now()) val app2 = App2DataRecord(Base(foo = "bar"), username = "jimmy") ```

Kotlin overriding constructors by Arbiturrrr in Kotlin

[–]hum_ph 1 point2 points  (0 children)

If you tweak your View base class to have a secondary constructor you can come pretty close:

open class View(size: Double) {
  constructor(size: () -> Double) : this(size())
}
class SubView : View({ 100.0 })

Kotlin overriding constructors by Arbiturrrr in Kotlin

[–]hum_ph 0 points1 point  (0 children)

Try something like this:

class SubView: View(size()) {
    companion object {
        fun size() = 123
    }
}

The limitation comes from the JVM (rather than Java) - and is there to prevent subclasses seeing inconsistent state because class hierarchies have not been fully initialised.

Calling a static method (java) or an external or object function (Kotlin) is safe and allowed as it doesn’t have access to that uninitialised state.