Feature #12637
closed![Assignee: core](/images/group.png?1722288379)
Unified and consistent method naming for safe and dangerous methods
Description
Hello,
As a multi-paradign language allows programmers to code from imperative to functional style. This gives Ruby to be modern and wide used language in these days.
Ruby is built on idea to be as fun, understandable and focused for people.
But i think it carry "complicated behaviour" in some method naming.
In this documentation is written: (http://docs.ruby-lang.org/en/trunk/syntax/methods_rdoc.html)
The bang methods (! at the end of method name) are called and executed just like any other method. However, by convention, a method with an exclamation point or bang is considered dangerous. In ruby core library the dangerous method implies that when a method ends with a bang (!), it indicates that unlike its non-bang equivalent, permanently modifies its receiver. Almost always, ruby core library will have a non-bang counterpart (method name which does NOT end with !) of every bang method (method name which does end with !) that does not modify the receiver. This convention is typically true for ruby core library but may or may not hold true for other ruby libraries.
Hightlited part of citation talks about that method with exclamation mark modifies its receiver. What is good because i could choose between imperative habit and modify receiver, but i can also create new data and code in functional habit.
Eg: i can create new array with map method, but always i can modify existed object with map!. What is incredible and easy to understand.
Or i want to get unique values from array by uniq assign into new variable. Still it is able to modify origin array with uniq!
Second highlighted part is about confused part. It tells to programmer be careful. Not all methods follows this great design.
In Ruby are methods which are "inconsistent" in naming.
Eg. Many built-in Array methods like
- clear
- concat
- delete
- delete_at
- delete_if
- fill
- insert
- keep_if
- pop
- push
- replace
- shift
- unshift
are dangerous because modify its receiver. What is confusing for many programmers and it brings unexceptional behaviour without study documentation deeper.
I think Ruby is popular for people because it communicate with programmer like in human language.
Nowadays are getting functional programming languages high popularity and trend increase more and more.
It could be great to remove that one word ("Almost") and have Ruby as language more clear, unambiguous and brings more functional principles into language what we like and love.
Updated by r.smitala (Radovan Smitala) about 8 years ago
- Description updated (diff)
Updated by r.smitala (Radovan Smitala) about 8 years ago
- Subject changed from Unificate and consistent method naming for safe and dangerous methods to Unified and consistent method naming for safe and dangerous methods
- Description updated (diff)
Fixed typo
Updated by shyouhei (Shyouhei Urabe) about 8 years ago
Array#clear being destructive is the nature of that method. It makes no sense to have its bang variant.
I think you missed the point that a bang method is a variant. No core methods I remember appear in bang without its bang-less counterpart. The use of bang is consistent in that way.
And honestly I don't like the idea of marking every methods with "this is functional" flag. That is a twisted Hungarian notation.
Updated by r.smitala (Radovan Smitala) about 8 years ago
Shyouhei Urabe wrote:
Array#clear being destructive is the nature of that method. It makes no sense to have its bang variant.
I think you missed the point that a bang method is a variant. No core methods I remember appear in bang without its bang-less counterpart. The use of bang is consistent in that way.
And honestly I don't like the idea of marking every methods with "this is functional" flag. That is a twisted Hungarian notation.
I understand that bang (dangerous) methods are variation to normal methods. But in some circumstances it is not clear if method modify receiver or not.
I dont want to just rename methods to variant with exclamation mark. My issue is about redefine methods and also create new variations.
Eg: fill which return new array, and fill! which modify receiver.
Good example should be this:
[1] pry(main)> ary = [1, 2, 3, 4]
=> [1, 2, 3, 4]
[2] pry(main)> new_ary = ary.fill { |i| i*i }.select { |num| num.even? }
=> [0, 4]
[3] pry(main)> ary
=> [0, 1, 4, 9]
Original object is modified. Fill method should just return new one. It think it is little bit confuse. Because there is select also with bang.
Code written in idea manner ( http://docs.ruby-lang.org/en/trunk/syntax/methods_rdoc.html ) may looks like:
# do not modify original variable
new_ary = ary.fill { |i| i*i }.select { |num| num.even? }
#modify
ary.fill! { |i| i*i }.select! { |num| num.even? }
Anyway it could not be apply to every method. Just for that methods which changing state of receiver.
This is not compatible change. I know it so i opened this topic as discussion.
Updated by shyouhei (Shyouhei Urabe) about 8 years ago
Radovan Smitala wrote:
Eg: fill which return new array, and fill! which modify receiver.
I think this specific method (nondestructive fill variant) is worth considering. Maybe another name (with aliasing foo! to current fill) could be acceptable?
Updated by r.smitala (Radovan Smitala) about 8 years ago
Shyouhei Urabe wrote:
Radovan Smitala wrote:
Eg: fill which return new array, and fill! which modify receiver.
I think this specific method (nondestructive fill variant) is worth considering. Maybe another name (with aliasing foo! to current fill) could be acceptable?
It is not about fill. New method name with same function just fragmentize language.
It is about manner which is used in Ruby language. Idea about "safe" and "dangerous" methods is very clear and understandable.
Safe methods do not change receiver, dangerous do. Easy and solid behaviour. When i want to modify data, but not variable, use safe. When i want to modify variable itself, use dangerous.
But currently names are mixed with their behaviour. It looks like safe method, but bam, modify receiver itself.
Class String http://docs.ruby-lang.org/en/2.3.0/String.html is good example. Almost every modifier method has safe and dangerous counterpart (instead some methods like insert, prepend, replace, concat).
This was i think good predisposition to implement immutable strings. Because there exists methods which modify data and returns new one. Or when i don't want to use immutable strings, there are still dangerous methods.
I think this ideology how safe and dangerous methods works could be use wide across all language operations. Next major version is still so far away.
Updated by trans (Thomas Sawyer) about 8 years ago
It would more interesting if there were a way to declare an object mutable or not, then when immutable destructive methods would not be available.
Updated by phluid61 (Matthew Kerwin) about 8 years ago
Thomas Sawyer wrote:
It would more interesting if there were a way to declare an object mutable or not, then when immutable destructive methods would not be available.
Do you mean #freeze ?
Updated by matz (Yukihiro Matsumoto) about 8 years ago
- Status changed from Open to Rejected
Ruby is not Scheme.
The rule is simple: every bang method should have its non-bang version, which does not modify the receiver.
That does not mean every non-bang method keeps its receiver unchanged.
Matz.
Updated by r.smitala (Radovan Smitala) almost 7 years ago
matz (Yukihiro Matsumoto) wrote:
Ruby is not Scheme.
The rule is simple: every bang method should have its non-bang version, which does not modify the receiver.
That does not mean every non-bang method keeps its receiver unchanged.Matz.
I know my reply could be annoying. I am apologise before.
After a year of using Ruby in mid-sized project we had some unlikely issues with mutating receiver. Because we are influenced by functional principle immutability of object we fixing interesting behaviour of our app caused by our infallibility that code we used doesn't change the state. As we figuring out we have forgot that some methods changing the receiver.
By our experience it is easier to remember uniform behaviour (immutable/mutable) for method calling than thinking about different behaviour for some methods without bang alternative.
we had no problem with remembering that Integer, Float, Nil, and Boolean is passed by value and everything else with reference. But method behaviour because we can't be sure that method without exclamation mark should return new value.
I also think migration to new method naming is really easy with simple regex replacing.
Updated by duerst (Martin Dürst) almost 7 years ago
r.smitala (Radovan Smitala) wrote:
I also think migration to new method naming is really easy with simple regex replacing.
It's not so easy. There may be programs with variable names such as fill
, user written classes with methods that have the same names as the ones you want to change, and so on.
And the more heavy cost is to change all the knowledge that all the current Ruby programmers already have.
Updated by r.smitala (Radovan Smitala) almost 7 years ago
duerst (Martin Dürst) wrote:
r.smitala (Radovan Smitala) wrote:
I also think migration to new method naming is really easy with simple regex replacing.
It's not so easy. There may be programs with variable names such as
fill
, user written classes with methods that have the same names as the ones you want to change, and so on.
You are right about migration. I don't count all variations of method using.
And the more heavy cost is to change all the knowledge that all the current Ruby programmers already have.
I think it is why Ruby popularity stagnatess or slightly decreases. No new blood or just little comes to community. Community getting older. Many went to another languages like Elixir not because they are fancy and new but i think because they are easier to learn and remember how they works. No different behaviour in whole language. Eg immutable, we no need to care about function behaviour. Everytimes it return new instance. And this is great. Uniformity.
Things like pattern matching or pipe operator are just syntactic sugar. It could be added anytime. But adapt language to new needs is required to make this language live in future.
Ruby is great language, i using it in my professional career for 8 years. Ruby's power it is human friendly universal language. Anyone can use it in imperative or functional way. And this is great opportunity to be bigger and more future-proof. But i think Ruby suffer for it's legacy. It was created in different age with different needs.
Ruby is preparing for great version 3. Isn't this option how to change some things?
For example global string freezing. How about this code?
"hello".concat("world")
will this code works with global string freezing anyway or it return exception?
If it return exemption or return new string instance it is change on fundamental behaviour, because in documentation this method change receiver. In both ways old code will not work.
I really like to see safe methods and dangerous methods cross all language.
If i use "hello".concat("world")
or "hello" << "world"
i will know it returns new instance. But if i use alternative with examation mark/bang i know it changes it receiver (eg unfreeze string and write into same variable name).
Isn't it easier to remember or to read by another developer?
Ruby influence many languages like Elixir or Crystal. They use similar syntax because it is strong advantage of Ruby, but they remove all ambiguity and it is their strong advantage, but not for Ruby at this age.
But as i use different languages more and more i miss some principles or functions in Ruby.
Like spread operator for hash {...anotherObject}, better asynchronous syntax (async await), uniform behaviour cross all language (always new value), pipe operator etc.
Modified and added:
Ruby doesn't have corporate background like C#, Java or Swift or is not only language in field like JavaScript. If we like it or not Ruby is just another language. People want new features, if Ruby doesn't deliver it they will leave.