SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)

    are some of Ruby's classes violating single responsibility principle?

    One example I can find is Integer class, it is primarily used to represent a number, but it can do some math operations on itself. This may not seem to be a lot yet, but then it can actually act like iterators(the downto, upto and times methods) for simple statement. It feels cool, but on the other hand I wonder if such implementation of Integer class is actually violating the single responsibility principle for good OO design. After all, the integer class seems to be doing value representation/conversion, math operation and iteration/loop at the same time, thats a total of three different responsibilities. Any Rubyists mind explaining this? Are there good reasons for this possible violation of single responsibility principle?

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,335
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Welcome, @Hall of Famer ; to these forums.

    You bring up a good point. I think there is a little room for "interpretation" on such rules.

    Remember, design principles are GUIDELINES and not hard, fast rules to be followed strictly

    As an integral part of the set of BASE CLASSES in Ruby I think there needs to be even more latitude offered for FIXNUM as in your example.

    This is a terrific topic for some discussion. I am anxious to see what other opinions may be offered here.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ParkinT View Post
    Welcome, @Hall of Famer ; to these forums.

    You bring up a good point. I think there is a little room for "interpretation" on such rules.

    Remember, design principles are GUIDELINES and not hard, fast rules to be followed strictly

    As an integral part of the set of BASE CLASSES in Ruby I think there needs to be even more latitude offered for FIXNUM as in your example.

    This is a terrific topic for some discussion. I am anxious to see what other opinions may be offered here.
    Thanks for your comment, looks like people dont have much to comment about this. After all, Ruby is not very popular on SitePoint, at least compared to PHP. Still, its nicer to be able to hear from some ruby experts talking about this language.

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,335
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    After all, Ruby is not very popular on SitePoint, at least compared to PHP.
    That *does* appear to be true. Let's work to change that.
    @markbrown ; has been quite active here and you are an active member and obviously interested in Ruby. That makes THREE of us so far
    Perhaps we can "push the envelope" a bit.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Ruby is the Greatest Programming Language Of All Time!

    I don't see a compelling reason why numbers should be especially dumb, every programming language allows a ton of functionality on Strings. They are a lot more complicated than numbers but it seems like a natural evolution to me that if everything is an object, we should start giving the humble integer more power.
    Code ruby:
    "abc, def".split ","
    "abc, def"[0..2]
    "asdf, asdf".chars.each { |c| puts c }
    3.times { |i| puts i }
    3.odd?
    1.round(2)
    These types of methods are what make Ruby programs more natural.
    Which of these snippets is more readable?
    Code ruby:
    3.times do |i|
      print i.even?
    end
    Code javascript:
    for (var i=0; i<3; i++) {
      console.log(i % 2 == 0);
    }

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ParkinT View Post
    You bring up a good point. I think there is a little room for "interpretation" on such rules.

    Remember, design principles are GUIDELINES and not hard, fast rules to be followed strictly
    I agree with that - design principles are intended to help you write testable, readable, maintainable code and I think it's important to take them with a pinch of pragmatism. In the case of the SRP, the idea is obviously that you're aiming for your classes to only have a single reason to change, but with Ruby base classes you're not responsible for maintaining them and you won't be modifying them, so it seems acceptable to compromise the principle in trade-off for a more useable syntax.

    I'm not familiar with how Ruby works under the hood, but it could be that some of the functionality (iteration, for example) is handled by a separate class, and
    calling 3.times is just some kind of syntactic sugar.

    Also, I think that when trying to apply the SRP, it can sometimes be tricky to identify what constitutes a single responsibility. There's usually always the option to break things down to an even more granular level.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •