Reputation
656
Top tag
Next privilege 1,000 Rep.
See votes, expandable usercard
Badges
5 8
Newest
 Commentator
Impact
~22k people reached

  • 0 posts edited
  • 0 helpful flags
  • 24 votes cast
Aug
19
comment Is there a need to keep tests for simple (self-contained) functions?
Also that function is a great candidate for a change to Horner's form (($a*$x + $b)*$x + $c)*$x + $d which is easy to get wrong, but is often considerably faster. Just because you think it wont change doesn't mean it wont.
Jul
6
answered Is overriding Object.finalize() really bad?
Jul
2
comment Is testable code better code?
I think the example in the piece you reference misses an important aspect. I would have both the no argument GetTimeOfDay and the one argument version. have GetTimeOfDay() forward to GetTimeOfDay(now()). The no argument version is so trivial that it doesn't need a unit test, and the other version is pure and easily testable. Client code need only use the no-argument version.
May
25
awarded  Commentator
May
25
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Sher10ck I'd not call it a God Class, it still has a well defined role, and adheres to the single responsibility principle. Having said that, the aim here was to keep the structure similar to the original code, while avoiding some of the problems of direct casting etc. Once the visitor starts to become unwieldy you need to switch to another pattern. In this case I'd likely convert the visitor to a Factory for Adaptor style objects - with each Adaptor implementing only one public function.
May
6
awarded  Good Answer
May
6
awarded  Yearling
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine, if you're happy to add that complete example into the end of the answer that might help others.
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine that looks good, but since your functions dont take arguments, instead of sub1 -> sub1.makeASandwich() you can just use Sub1::makeASandwich - which is even neater. (Thats not the case in the original example, so I had to stick with the long form lambda, rather than the method reference)
May
6
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
Changed `=>` to `->`.
May
6
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@FiveNine - stream() is part of the new java 8 streaming protocol. The forEach(...) takes a java 8 lambda and applies it to each of the elements of the stream. BaseVisitor.forEach(...) is a function you'd need to create that takes two java 8 lambdas of different types (one taking Sub1, the other taking Sub2) and returning a single lambda that takes a Base and applies the correct lambda using the visitor patter.
May
1
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Radiodef An advantage of using the visitor pattern over switching on type directly is that once you've added onSub3 to the visitor interface, you get a compile error in every location you create a new Visitor that hasn't yet been updated. In contrast switching on type will at best generate a run-time error - which can be trickier to trigger.
Apr
30
awarded  Nice Answer
Apr
30
awarded  Yearling
Apr
30
comment Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
@Mankarse A little syntactic sugar can make it very far from ugly - I've updated it with an example.
Apr
30
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
added 596 characters in body
Apr
30
awarded  Editor
Apr
30
revised Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
added 596 characters in body
Apr
30
answered Is it okay to have objects that cast themselves, even if it pollutes the API of their subclasses?
Feb
16
answered Why is base-for-all-objects discouraged in C++