New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Good line length? #56
Comments
I think long function definitions might be a code smell, but if you have to have them I'd much rather have them on one single line. I think line length limits are generally a mistake because they cause more readablity problems than they solve. Having short lines as a goal is good, doing stupid coding tricks just to get a line under X chars is not good. |
@bbense so don't do complex pattern matching in the function definition? |
Elixir is prone to long lines I think because of what @hunterboerner mentioned: big function heads. This is an example from marco_polo: defp parse_live_query_content(<<op_type, token :: int, record_type, version :: int, cluster_id :: short, position :: long, rest :: binary>>, schema) do
I can't really make it shorter without losing either clarity or performance. Another example is some stdlib functions (Erlang in this case but you get the point) that can make a line long without making it complex at all. This is a real-world line from marco_polo as well: days = date_to_gregorian_days(date.year, date.month, date.day) - date_to_gregorian_days(1970, 1, 1) All of this just to say that yes, probably "enforcing" a line length limit in the style guide may be too much? |
No, don't limit yourself to 80 chars if you need to do complex pattern matching in function definitions. If that's what you really need to do, then do it and don't worry about some silly line length guideline. If you think before you break the rule, then breaking the rule is fine. That's what I mean by code smell, not that you shouldn't do it, but that you should think if that's really the right thing before doing it. To be honest, there is much in this style guide that I think borrows too much from rules of thumb in Ruby. Elixir is still too young to know what the rules of thumb will be that lead to maintainable code. |
Btw. when it comes to numbers I would say that Besides this I would say you should find your own limit. And this number will be perhaps a very personal one. Ask yourself: How much information (here characters per line) I am capable to grasp? Furthermore this might be also influenced by your editor/IDE setup you use; how much information regularly fits on your screen. Unless you develop on really low resolution tiny screens and/or side-by-side split windows I'm pretty sure most of the time you have a lot of whitespace to the right. Considering both statements it's hard to tell where you should wrap your code. It might be 80, but could also easily be more or even less. Therefore I think a style guide can't really help here. And as the Ruby style guide was mentioned here: in general I think a lot of their recommendations are pretty good, but some like "80 characters per line" are always things I override in every project. Update My personal rule of thumb is: If I have to scroll horizontally too often then it seems to be time to think about how much stuff I've put into the lines. |
Guess the consensus is that we can close this one? |
Maybe instead of explicit line length - we should advise on line break techniques? For a long def put_new_lazy(keywords, key, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
case :lists.keyfind(key, 1, keywords) do
{^key, _} -> keywords
false -> [{key, fun.()}|keywords]
end
end |
+1 for not setting a specific line length. Most styleguides seem to suggest 80 yet I've never participated in a project that enforced it. Why have styles in a guide that you intend to ignore? I personally try to keep my lines under 120 but in reality I just keep things clean and do whatever works best. |
This would be nice. |
Should we mention that no line length is defined by this guide. I will propose to include this in the tools section, as it is appropriate to set up the space configuration and line break |
This is something that I've struggled with as well and That being said, +1 on the line break techniques. Many times I have gone to a new-line and asked myself, "is this correct? Should I be moving this to a new line?" |
👍 on the including a line breaking technique |
I try to keep the line length under 130 characters - the width of GitHub's file viewer, so code reviewers don't need to scroll horizontally. |
The erlang guidelines I found state 100 columns per file. Erlang Coding Standards & Guidelines
|
This is something that the new Elixir code formatter will take care of, so I think this issue can be closed. |
@whatyouhide I noticed that the code formatter defaults to a line length of 98 characters. Do you have any insight on why this exact length was chosen? My naive guess is that if you comment out a line of code it will still not be longer than 100 characters. :) |
@christopheradams just a good looking line length, I don't remember any particular reason to make it 98. It's customizable anyways :) |
Great that it is customizable :) The most beautiful lines of code are less than 70 characters. In computer science as I remember it was always less than 80 characters. Just longer lines are harder to focus on them read them and you need more time to process them by brain. For example: In computer science if there is a need to write line more than 80 characters it's probably good place for refactoring code or sometimes refactoring language. |
I just asked @josevalim this question. He chose 98 so that it wouldn't take sides for any default preference. If it were 100 then someone would argue for 80. This way no one is happy. 😁 |
Microbikeshedding: why not 97 or 99 instead? |
Should there be a line length limit? I have my editor set up to make text after 80 chars a vivid red. I think there also needs to be guidelines for wrapping function definitions:
The text was updated successfully, but these errors were encountered: