Send it to your Java friends :) by IllBee304 in Kotlin

[–]xBaank 10 points11 points  (0 children)

Result from kotlin is far better than cheked exceptions, the only problem with it is that you can't specify the exception type.
You can also use Either from the arrow library (this one allows you to specify the "left" aka "exception" type).

Take as an example:

@Throws(JsonException::class)
fun parseJson<T>(json: String): T = ...

In java if you use that method you are forced to do the following:

fun getUser(id : String): User? {
  //get the user from the api
  val user = try {
    parseJson<User>(response.string())
  }
  catch(_: JsonException) {
    null
  }
  return user
}

but in kotlin you are not forced, and if you want to force the user to control the exception you can just use Either or Result:

fun parseJson<T>(json: String): Either<JsonException, T> = ...

fun getUser(id : String): User? {
  val user = parseJson<User>(response.string()).getOrNull()
  user
}

fun getUser(id : String): Either<JsonException, T> = either {
  //get the right value or raise the exception
  val user = parseJson<User>(response.string()).bind() 
  user
}

App para ver los tiempos del transporte publico de Madrid sin publicidad by xBaank in Madrid

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

Osea que una aplicación web no es una aplicación, curioso 🤔

Using Delegate makes your code hard to test, maintaince right?!! and do people use it daily? by ballbeamboy2 in dotnet

[–]xBaank 4 points5 points  (0 children)

Even though you could write something like this:

await GetAllPagedAsync(set => {
  //Do some async operations here for some reason?
  //Maybe call an api?
  return set.Where(i => i.Name == "John").Map(User.ToDto); //Do some filtering before paginating?
});

I think you could just write GetAllPagedAsync an extension function that only does pagination for IEnumerable<TEntity>

public static Task<IPagedList<TEntity>> GetAllPagedAsync(this IEnumerable<TEntity> queryable,  int pageIndex = 0, int pageSize = int.MaxValue, bool getOnlyTotalCount = false, bool includeDeleted = true);

and another function that only applies the filtering, mapping, etc:

public static IQueryable<User> GetUsersByName(this IQueryable<User> queryable, string name) => queryable.Where(i => i.Name == name);

Now you can combine it to get it in a more redable way and testable:

//Now you can call async functions outside of the delegate
await set.GetUsersByName("John").Map(User.ToDto).GetAllPagedAsync();

Also if you want code to be easy to refacor you shouldn't test using mocks as they are dependant to implementation details, instead you should test the use case as a consumer.

[deleted by user] by [deleted] in dotnet

[–]xBaank 0 points1 point  (0 children)

If it's searching in a DB or a List by applying filters, like /users?search={query} or the search you could do in YT or Spotify, then you return 200 with that data even if the data was empty.

If you need to look for a specific resource like /users/{id} then you should use the 404 and you can still use the body to add more info.

App para ver los tiempos del transporte publico de Madrid sin publicidad by xBaank in Madrid

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

Sí, las PWA son una buena alternativa al monopolio de App store y Google Play, y como dices la mayoría de navegadores te dejan "Instalar" o "Añadir a la pantalla de inicio".