This may sound offensive around here which is not intentional, but I must be blunt - I absolutely hate dynamically typed languages. I love the structure and security of a proper IDE supported language like C++ or C#.
Maybe I am doing something wrong?
I am constantly afraid if I change something like a variable name, it will start triggering endless weird errors that become hard to chase down. I have had this experience with Javascript where i mistype something and then spend 20 min trying to find out why it won’t run. It is always quite annoying. Would never happen in a proper IDE like Visual Studio with C++/C#.
I am programming in VS Code for Elixir (or Javascript).
Take for example where here I am trying to implement @behaviour WebSock
:
#termination reasons here: https://hexdocs.pm/websock/0.5.3/WebSock.html#c:terminate/2
def terminate(reason, state) do
case reason do
# The local end shut down the connection normally, by returning a {:stop, :normal, state()} tuple from one of the WebSock.handle_* callbacks
:normal ->
IO.puts("TERMINATED: NORMAL");
#The remote end shut down the connection
:remote ->
IO.puts("TERMINATED: REMOTE");
# The local server is being shut down
:shutdown ->
IO.puts("TERMINATED: SHUT DOWN");
#No data has been sent or received for more than the configured timeout duration
:timeout ->
IO.puts("TERMINATED: TIMEOUT");
#An error occurred. This may be the result of error handling in the local server, or the result of a WebSock.handle_* callback returning a {:stop, reason, state} tuple where reason is any value other than :normal
{:error, reason}->
IO.puts("TERMINATED: ERROR" <> to_string(reason));
end
end
Is this a reasonable structure for this based on the documentation of the list of possible reasons here?
Currently this gives me on build:
warning: variable “state” is unused (if the variable is not meant to be used, prefix it with an underscore)
│
37 │ def terminate(reason, state) do
Which I understand. I am not using state
in the above. Sure. But what do I want to do about this to silence the warning which is meaningless to me? I must match the pattern. I don’t know if I’ll use state
in the future.
Option 1 - rename state
to _state
as they say. It works. Is this a good idea? Perhaps not. If I start using state
later, I have to rename it again. Then if I stop using state
(comment out that code) I’m back to the warning popping up again. And what? I rename it again to _state
? Seems insane.
If this was a statically typed language with a proper IDE to immediately tell me if I do something wrong, I would more readily just rename variables on ongoing basis back and forth. Maybe I just need to suck it up and deal with it.
Option 2 - create some dummy usage of state
in this function that costs nothing or near nothing to run. Anything come to mind? Something like if (state === true) do end
or state = state
. Kind of wasteful probably no matter what as every operation adds up. I don’t know.
One idea I just thought of would be to wrap it in a case that will never be met like
:never_gonna_happen ->
IO.puts(to_string(state));
This does seem to suppress it with negligible code bloat/expense on run. Might be best way to me.
Option 3 - ignore the warnings or disable the warnings altogether somehow - I am already accumulating these warnings in just minimal projects. Pollutes the screen when running. But I don’t want to disable them globally as I might actually get some important ones at some stage.
What do you do about this?
In general, does it not drive you crazy coding in general and especially naming and renaming variables when there is no IDE to help you? And you are just working in a glorified text editor? How do you cope with working like this? Is there something I’m missing in terms of how I’m approaching it?
Thanks for any thoughts or advice.