Lädt...


🔧 Proposal for making private method work on constants too


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

There was a proposal on the Ruby bug tracker a while back about making the private method work also on constants:

Screenshot of the proposal at Ruby Bug Tracker

See https://bugs.ruby-lang.org/issues/17171

That proposal is now closed with concerns about backward compatibility.

We should reopen this discussion and try to find ways around backward compatibility. Let me explain why I think so:

A possible code in Ruby

This might be a possible code design found in a Ruby project:

class MyObject 
  DEFAULT_VALUE = 'active'
  private_constant :DEFAULT_VALUE

  def a_public_method
    # doing something important here
  end
end

When reading this file, we first read DEFAULT_VALUE then we find out it is a private constant and then we go to read the actual public interface. I would assume that usually the information architecture that we would want when reading this file would be something like this:

  1. Public methods

  2. Public constants

  3. Private methods

  4. Private constants

Let's take two possible cases for reading this file:

  • Trying to understand what this object does: in this case I want to see first the public method(s) and then go down inside private methods if I want to expand my knowledge and understand some fine grained details.

  • Trying to understand what is the content of a public constant called from some other place: In this casde I want to see first the public constant

Anyhow no matter the objective I almost never want to read first the private methods or private constants.

Going to private constants: I don't think there is a good case to be made for putting a private constant in the public space of the object. The constant is private because it should be used only inside the current object in its either public or private methods.

So maybe the following code design is a better fit from the point of information architecture:

class MyObject 
  def a_public_method
    # doing something important here
  end

  private 

    DEFAULT_VALUE = 'active'
    private_constant :DEFAULT_VALUE
end

Of course in this case while the intention of the information is communicated pretty clear and the code is organised in public interface to the top and private interface to the bottom, there is a duplicate information there:

private and private_constant

But moreso it is a confusing thing that when first learning Ruby you just have to learn it: the private method does not affect constants so you have to use private_constant.

I think a better (in the sense of simplicity and expectations) code design would be to write:

class MyObject 
  def a_public_method
    # doing something important here
  end

  private 
    DEFAULT_VALUE = 'active'
end

And make Ruby private method to affect also constants. So MyObject::DEFAULT_VALUE should throw NameError .

Arguments in favor of this feature

Principle of least surprise

The main argument for making the private method work for me would be based on the principle of least surprise: seeing a code like the one above will make everybody think that DEFAULT_VALUE is a private constant. So it should work that way.

Removing boilerplate

The second argument is that it will remove a boilerplate code: the private_constant method should be used in a small number of situations. It seems to me that it should remain in the language but for specific usage.

Removes the need to know a trick (maybe still the principle of least surprise)

When encountering the method private the expectation is that what follows is something private. Writing a constant there is now making it public, disrupting the flow of thoughts about the visibility of what is in that section of the code.

Compatibility concerns

I know that the previous feature request was closed with a concern for breaking compatibility by Matz. Which is a valid concern and I appreciate so much the case he puts into making our life easier when upgrading.

But I do wonder if in this case the worrying is real.

What is the main case of implementing breaking changes? When someone added the constant in the private section or called the private method for it and then used it as being public.

Something like this:

class MyObject 
  def a_public_method
    # doing something important here
  end

  private 
    DEFAULT_VALUE = 'active'
end

class AnotherObject 
  def some_method
    MyObject::DEFAULT_VALUE
  end
end

I do wonder two things:

  1. How common is this way of writing code: putting the declaration of a private constant in the private section of an object?

  2. And if this exists how common it is for the constant defined like that to be accessed outside that object?

My main assertion is that if someone writes a constant in the private area of an object, their intention (even if it is not actually enforced during execution) is that they want to say or show that constant is private.

Calling that constant from other objects is breaking this contract. It is for me the same as calling send or __send__ on a private method. It can be done but that breaks the contract for that object. But if want to achieve the same for cosntants we already have const_get that will get any constant from an object.

Since we have this repo now Gem CodeSearch I will try to see at least for gems if I can find a way to search for this specific case (using a constant defined as private - but not with private_constant into another object).

New functionality

I think making the private work on constants will open the case for making a class defined inside another class private.

Take this code for example:

class MyObject
  class AnotherObject
    def hello
      puts "I am here"
    end
  end

  def a_public_method
    AnotherObject.new.hello
  end
end

MyObject.new.a_public_method

I think AnotherObject remains public here unintentionally.

Most of the time this code should be:

class MyObject
  class AnotherObject
    def hello
      puts "I am here"
    end
  end

  private_constant :AnotherObject

  def a_public_method
    AnotherObject.new.hello
  end
end

MyObject.new.a_public_method
# => 'I am here'

MyObject::AnotherObject.new.hello
# => private constant MyObject::AnotherObject referenced (NameError)

But people usually forget to add the private_constant here. And even so this goes to the first point where when reading this file, I first read an object that is private.

I think making the private method affect constants, will open the case to write something like this:

class MyObject
  def a_public_method
    AnotherObject.new.hello
  end

  private
    class AnotherObject
      def hello
        puts "I am here"
      end
    end
end

MyObject.new.a_public_method
# => 'I am here'

MyObject::AnotherObject.new.hello
# => private constant MyObject::AnotherObject referenced (NameError)

So we could have private objects that can be defined with private method and be accessible only to the object where they are defined.

What's next

I am talking mostly from the perspective of someone that uses the language, so I for sure don't know all the possible problems this change will create. But I think we should have another conversation about this. And see if we can make it work.

Enjoyed this article?

👉 Join my Short Ruby Newsletter for weekly Ruby updates from the community and visit rubyandrails.info, a directory with learning content about Ruby.

👐 Subscribe to my Ruby and Ruby on Rails courses over email at learn.shortruby.com- effortless learning about Ruby anytime, anywhere

🤝 Let's connect on Ruby.social or Linkedin or Twitter where I post mainly about Ruby and Ruby on Rails

🎥 Follow me on my YouTube channel for short videos about Ruby

...

🔧 Proposal for making private method work on constants too


📈 82.48 Punkte
🔧 Programmierung

📰 The state of data quality: Too much, too wild and too skewed


📈 28.88 Punkte
📰 IT Security Nachrichten

🔧 How to unit test a private method in Kotlin without making it public


📈 27.52 Punkte
🔧 Programmierung

🔧 Making It Work vs. Making It Perfect?


📈 24.71 Punkte
🔧 Programmierung

📰 Making private browsing more private


📈 24.43 Punkte
📰 IT Security Nachrichten

📰 Four-Day Work Week Proposal by UK Government Raises Issues, Says Econ Professor


📈 22.98 Punkte
📰 IT Security Nachrichten

🕵️ ActionApps constants.php3 GLOBALS[AA_INC_PATH] privilege escalation


📈 22.35 Punkte
🕵️ Sicherheitslücken

🔧 Static Variables, Constants, and Methods


📈 22.35 Punkte
🔧 Programmierung

📰 Linux 6.11: Mehr Rust, Torvalds ergänzt "Runtime Constants"


📈 22.35 Punkte
📰 IT Nachrichten

🕵️ An IDA Python script to extract information from string constants


📈 22.35 Punkte
🕵️ Reverse Engineering

🔧 Mastering Go: Guide to Type Declarations, Variables, and Constants


📈 22.35 Punkte
🔧 Programmierung

🔧 Introduction to Constants in GBase 8a MPP Cluster


📈 22.35 Punkte
🔧 Programmierung

📰 Protected API Calls and String Constants: Looting Dridex’s Candy Box


📈 22.35 Punkte
📰 IT Security

🔧 Swift 101: Understanding Types, Variables, and Constants


📈 22.35 Punkte
🔧 Programmierung

🔧 July 10, 2024 Python | DataTypes, Variables, Constants


📈 22.35 Punkte
🔧 Programmierung

📰 Protected API Calls and String Constants: Looting Dridex’s Candy Box


📈 22.35 Punkte
📰 IT Security

🔧 Constants in Solidity


📈 22.35 Punkte
🔧 Programmierung

🔧 What are Variables and Constants in Go? Explained With Examples


📈 22.35 Punkte
🔧 Programmierung

🔧 Code Smell 249 - Constants as Numbers


📈 22.35 Punkte
🔧 Programmierung

🔧 Simplify SVG Management: Convert Paths to a Single JS File of Constants


📈 22.35 Punkte
🔧 Programmierung

🔧 Constants in C++


📈 22.35 Punkte
🔧 Programmierung

🔧 Code Smell 262 - Not Replaced Constants


📈 22.35 Punkte
🔧 Programmierung

🔧 Constants are no longer constant in PHP


📈 22.35 Punkte
🔧 Programmierung

🔧 Constants in JS and what do Plato and Aristotle have to do with it


📈 22.35 Punkte
🔧 Programmierung

🔧 Variables, Shadowing, and Constants in Rust


📈 22.35 Punkte
🔧 Programmierung

🔧 PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES


📈 22.35 Punkte
🔧 Programmierung

🕵️ Binary ninja equivalent for symbolic constants?


📈 22.35 Punkte
🕵️ Reverse Engineering

🕵️ CVE-2023-1712 | deepset-ai haystack prior 0.1.30 hard-coded, security-relevant constants


📈 22.35 Punkte
🕵️ Sicherheitslücken

📰 Rust Basics Series #2: Using Variables and Constants in Rust Programs


📈 22.35 Punkte
🐧 Unix Server

🔧 Task 2 - Constants and variables


📈 22.35 Punkte
🔧 Programmierung

🔧 Variables and Constants: Declaration and Usage


📈 22.35 Punkte
🔧 Programmierung

🔧 10/7/24 - Day 2 - Data types,variables,constants


📈 22.35 Punkte
🔧 Programmierung

🍏 PCalc for iOS and Mac enhanced with new capabilities for functions, conversions, and constants


📈 22.35 Punkte
🍏 iOS / Mac OS

🕵️ CVE-2024-32021 | Git objects/ hard-coded, security-relevant constants


📈 22.35 Punkte
🕵️ Sicherheitslücken

matomo