Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-@a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-@a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-@a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0
added 1 character in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-a@a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0

There is a bug in your quadratic formula. Watch your operator associativity!

I'm not a fan of the method names qudrtc_frml_with_a_b_c and pythgrn_thrm_with_a_b, and I think that the poor naming is a symptom of a poor class design.

First of all, drpng vwls makes your interface hard for others to use. Learn from Ken Thompson's greatest regret.

Next, I would remove "formula" and "theorem" from the method names. The caller generally doesn't care how you solve the quadratic equation — whether you do it by using the quadratic formula, completing the square, etc. Similarly, the fact that there is a Pythagorean Theorem is unimportant; the focus should be on the subject of the Pythagorean Theorem, which is the right triangle.

Third, I question the benefit of adding all of these unrelated equations as methods of one class.

Finally, the suffixes ..._with_a_b and ..._with_b_c for your Pythagorean solver suggest that your solution is too rigid. It would be nice if the user could enter all but one of the variables as constraints, and ask the program to solve for the remaining unknown.

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-@a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

Here it is in use:

irb(main):001:0> require 'formula'
=> true
irb(main):002:0> triangle = RightTriangle.new
=> #<RightTriangle:0x007fe14301f500>
irb(main):003:0> triangle.hypotenuse = 5
=> 5
irb(main):004:0> triangle.a = 3
=> 3
irb(main):005:0> triangle.b
=> 4.0
Incorporated suggestion from @UriAgassi to leave constraints uninitialized
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft an verya human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def initialize
 a = b = c = x = 0
 end
  def x
  positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def initialize
 a = b = c = 0
 end
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft an very human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def initialize
 a = b = c = x = 0
 end
  def x
  positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def initialize
 a = b = c = 0
 end
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end

I suggest the following design. It takes advantage of one of Ruby's strengths, which is the ability to craft a human-friendly domain-specific language.

require 'cmath'
class QuadraticEquation
 attr_writer :a, :b, :c, :x
 def x
 positive = (-@b + CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 negative = (-@b - CMath.sqrt(@b ** 2 - 4 * @a * @c)) / (2 * @a)
 [positive, negative]
 end
 def a
 (-@b * @x - @c) / (@x ** 2)
 end
 def b
 (-a * @x ** 2 - @c) / @x
 end
 def c
 -@a * @x ** 2 - @b * @x
 end
end
class RightTriangle
 attr_writer :a, :b, :c
 def a
 Math.sqrt(@c ** 2 - @b ** 2)
 end
 def b
 Math.sqrt(@c ** 2 - @a ** 2)
 end
 def c
 Math.sqrt(@a ** 2 + @b ** 2)
 end
 alias :hypotenuse :c
 alias :hypotenuse= :c=
end
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Loading
lang-rb

AltStyle によって変換されたページ (->オリジナル) /