Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I get overloading is a popular way of writing DRY code but I’ve never been a fan of overloading either. You might dislike getSomethingBySomething style function names but it’s no different to the examples you’ve given in terms of readability but with the bonus of having fewer surprises.


Should I look for the function called: `getStudentByClassRoomFirstNameLastName` or is it `getStudentByClassRoomLastNameFirstName`, or `getStudentByClassRoomFullName` since you sort that way. They you have to deal with optional parameters, do you have an extra argument for that or is there a `getStudentByClassRoomFirstNameLastNameMiddleName` function out there? Now you are scanning the auto-complete code trying to figure out which function you need to call.

If you have named parameters, this just works:

  `getStudent(classRoom: 15, firstName: "Mary", lastName: "Jane")`
  `getStudent(classRoom: 15, firstName: "Mary", lastName: "Jane", middle: "Anne")`
It can call the same function with optional parameters, or 2 different functions and you don't care from the calling side because the syntax is the same and always makes sense.

(slight tangent)

Far better with overloading in general is being able to have 2 methods with the same name which return the same thing but have different types of inputs. Like in Elixir, you can have an API callback be `handle(apiResult)`, then have 2 functions, one which handles the error and one which handles the success. Zero logic written to filter out bad-calls, it's just the same method with different types (the Error result type and the Success result type). Vastly simplifies and cleans up that kind of code.


Even better is Elixir's Ecto or Linq

    Students |> Students.Repo.get_by(first_name: "Ryan")

    from s in Students where firstName = "X" select s
    
    Students.AsEnumerable().Select(s => s).Where(s => s.Name == "X")
etc.


Elixir is what I was thinking of for much of the above, unfortunately, I only used it fairly briefly and so I was going largely on memory and some of that is likely more Swift than Elixir which shares some of that.

I really loved using Elixir, very frustrating going back to TypeScript after that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: