0
\$\begingroup\$

Task

interface IJuiceable
{
    void MakeJuice();
}
interface ISliceable
{
    void MakeSlices();
}

class Fruit:IJuiceable,ISliceable
{
    public string Name { get; set; }
    public void MakeJuice()=>Console.WriteLine($"Juice from {Name}");     

    public void MakeSlices()=>Console.WriteLine($"Slices of {Name}");         
}

class Fresh
{
    List<IJuiceable> juices = new List<IJuiceable>();
    List<ISliceable> slices = new List<ISliceable>();

    public void AddToJuices(IJuiceable fruit)=>juices.Add(fruit);       
    public void AddToSlices(ISliceable fruit)=> slices.Add(fruit);

    public void MakeFreshes()
    {
        juices.ForEach(x => x.MakeJuice());
        slices.ForEach(x => x.MakeSlices());           
    }
}
\$\endgroup\$
3
  • 6
    \$\begingroup\$ Welcome to Code Review! Your question could use some fixing up: (1) the title should say the purpose of your code, and (2) please put the problem in plain text in the body, rather than in an image. \$\endgroup\$
    – SirPython
    Commented Apr 6, 2016 at 19:55
  • \$\begingroup\$ Agreed. I gave you the benefit of the doubt and answered the question, but please do not just paste bitmaps, especially as it's so small it's difficult to read \$\endgroup\$ Commented Apr 6, 2016 at 21:03
  • 1
    \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. \$\endgroup\$
    – Jamal
    Commented Apr 7, 2016 at 1:41

2 Answers 2

1
\$\begingroup\$

You have not implemented exception handling (the last requirement). Eg. if calling code passes null to AddToJuices or AddToSlices, it won't crash straight away, but as soon as you call MakeFreshes, it will result in an unhandled NullReferenceException once it gets to this null "fruit".

Also note that we don't know what calling code may pass in as an argument - could be some object that implements one of the interfaces, but actually explodes once our code calls its MakeJuice or MakeSlices... The requirement states that it's the responsibility of this code to handle exceptions, so if this was my homework, I'd prefer to be on the safe side here.

Less importantly, you implemented Name as a property of Fruit rather than a field (which is what the description asks for), although public fields actually are considered a bit of a code smell, and a backing field is automatically created for properties, so I would leave it. Aiming at good code style you could make it immutable though (as a readonly field, or a property with a private setter), and set it in the constructor - it stands to reason that name of a given fruit isn't subject to change. juices and slices could be readonly too, but that's nitpicking already.

Apart from the lack of exception handling, it seems okay to me. I like that you used some modern C# features to cut down on verbosity.

\$\endgroup\$
0
0
\$\begingroup\$

Implementation looks good except handling null in AddToJuices(...) and AddToSlices(...) methods.

As a side note to @Konrad Morawski's comment on

Also note that we don't know what calling code may pass in as an argument - could be some object that implements one of the interfaces, but actually explodes once our code calls its MakeJuice or MakeSlices...

Since the methods are expecting a specific type, callers won't be able to pass objects as they want.

Eg

internal class Apple : ISliceable
{
    public void MakeSlices() => Console.WriteLine($"Slices of Apple");

}

You won't be able to pass this implementation to AddToJuices(...)

@Konrad Morawski, correct me if I'm wrong.

\$\endgroup\$
3
  • \$\begingroup\$ @RSP it's a misunderstanding - I didn't mean an object of wrong type, here you're obviously right a strongly typed language prevents it in compile time. I simply meant an object that does support (say) ISliceable, but when you call MakeSlices on it, it throws an exception for some reason. Everyone can implement that interface, so we can't guarantee it won't happen, and requirements state that handling exceptions is on us. \$\endgroup\$ Commented Apr 7, 2016 at 7:41
  • \$\begingroup\$ I wrote "...could be some object that implements one of the interfaces, but actually explodes once our code calls its MakeJuice..." \$\endgroup\$ Commented Apr 7, 2016 at 9:16
  • \$\begingroup\$ @KonradMorawski, Got it ! Appreciate your turn up ! \$\endgroup\$
    – RSF
    Commented Apr 7, 2016 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.