1

I don't know what everyone calls this but how do I make this piece of code shorter?

I was taught to not repeat code and at the GetAddress() method the if statement I repeat the same line twice, once from the if statement and then another from the else if statement below it

(_CClientFirstName == "John" || _CClientLastName == "Jenkins" || _CClientAge == 21)

How would I do this with less code? Im just wondering if it's possible because if it is, I would love to know. Also, please don't reply with anything too complex as Im only starting out, but if you do then try to explain it as much as you can thanks.

class ClientInfo
{
    private string _CClientFirstName = "Default";
    private string _CClientLastName = "Default";
    private int _CClientAge = 99;

    public ClientInfo(string FullName, string LastName, int Age)
    {
        _CClientFirstName = FullName;
        _CClientLastName = LastName;
        _CClientAge = Age;
    }

    public string GetAddress()
    {
        if (_CClientFirstName == "John" || _CClientLastName == "Jenkins" || _CClientAge == 21)
        {
            return $"{_CClientFirstName}'s address is: 67 Smokey Lane, London, B78 9JN, United Kingdom";
        }
        else if (_CClientFirstName == "Matt" || _CClientLastName == "Benks" || _CClientAge == 25)
        {

        }
    }
}
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Static_x
  • 11
  • 1
  • 2
    what you are excepting when both the if condition are not true .? – Dileep Aug 27 '16 at 17:12
  • 1
    Do you really have every combination of names and addresses baked into your code like this? What is the context here? – Ant P Aug 27 '16 at 17:13
  • 3
    There is no shroter way present. BTW, your `||` should be and condition – Rahul Aug 27 '16 at 17:14
  • This was part of a tutorial, I made this tiny program to practice using classes, fields, constructors etc I understand there would be another way to fetch the details but is this how you would check for them because when you search for people in a large database you would have to verify more than just the name and surname unless you have a unique id – Static_x Aug 27 '16 at 17:15
  • @NullOrNot Advice from a hot-shot developer who has been humbled by his experience: don't worry about making your code as short as possible. Rather, focus on [keeping it simple](https://en.wikipedia.org/wiki/KISS_principle). Your code is clear enough right now. If you find a better way, you can always come back and [refactor](http://refactoring.com/) it later. – kevin628 Aug 27 '16 at 17:32

0 Answers0