0

In my zeal to improve as developer, I want to know my code it is possible to improve, I am new in this and have very small logical

this is my code:

private int Period(DateTime date)
{
    int period = 0;

    if(date!= null)
    {
        int numeroMes = int.Parse(date.Month.ToString());
        if(numeroMes <= 2)
        {
            period = 1;
        }
        else if (numeroMes <= 4 && numeroMes > 2)
        {
            periodo = 2;
        }
        else if (numeroMes <= 6 && numeroMes > 4)
        {
            period = 3;
        }
        else if (numeroMes <= 8 && numeroMes > 6)
        {
            period  = 4;
        }
        else if (numeroMes <= 10 && numeroMes > 8)
        {
            period = 5;
        }
        else if (numeroMes <= 12 && numeroMes > 8)
        {
            period = 6;
        }
    }

    return period ;
}

tkns for help me.

4
  • 3
    This question is a better fit for Code Review
    – Igor
    Jul 24, 2018 at 19:51
  • switch statement i would look into
    – Fuzzybear
    Jul 24, 2018 at 19:52
  • Depends on what you mean by "improve". Make it more readable? Move this to Code Review as was mentioned. Make it more performant? Learn to use a profiler.
    – Kenneth K.
    Jul 24, 2018 at 19:53
  • Thank you, it will eliminate the question Jul 24, 2018 at 19:53

3 Answers 3

4

You can use the flooring of integer division to your favour which should be faster.

private int Period(DateTime date)
{
    return (date.Month + 1) / 2;
}

However rounding might be easier to understand.

private int Period(DateTime date)
{
    return (int)Math.Ceiling(date.Month / 2.0);
}
3

You can calculate the period since every period is just two months.

As DaveShaw pointed out, there is no need to check for the date being null because DateTime is a value type and thus cannot be null.

private int Period(DateTime date)
{
    // already an int, no need to convert
    var month = date.Month;
    if (month % 2 == 0)
        return month / 2;
    else
        return (month + 1) / 2
}

Here's another even shorter option that A.S. suggested.

private int Period(DateTime date)
{
    return (int)Math.Ceiling(date.Month / 2.0);
    // or...
    // return (date.Month + 1) / 2;
    // but I prefer the Ceiling option since it is more obvious what is happening
}

Alternatively, you could also reduce your code in this way...

private int Period(DateTime date)
{
    int month = date.Month;
    if (month <= 2)
        return 1;
    else if (month <= 4)
        return 2;
    else if (month <= 6)
        return 3;
    else if (month <= 8)
        return 4;
    else if (month <= 10)
        return 5;
    else
        return 6;
}

Since I used return statements, there is no need for the second bool in each if statement.

3
  • 2
    Date can't be null, it's a value type
    – DaveShaw
    Jul 24, 2018 at 19:58
  • 1
    would it not be quicker to use division by 2 with Math.ceil()? It seems there are quite a few repeated if-statements that could be shortened.
    – Alex
    Jul 24, 2018 at 20:06
  • @A.S. Thanks, I added it to my answer. Jul 24, 2018 at 20:12
2

As others said, this would better fit in https://codereview.stackexchange.com/, since it's not a question to a problem.

Now for some improvements to your code.

if(date!= null)

This will always be true, since DateTime is a value type (struct), which can't be null. Only Nullable<DateTime>/DateTime? can be null. So you can simply remove that.

int numeroMes = int.Parse(date.Month.ToString());

Here you are converting date.Month (which is an int) to a string and then parse it back to an int. Don't do that, date.Month is an int already:

int numeroMes = date.Month;

The rest is ok so far, but i guess in this line:

else if (numeroMes <= 12 && numeroMes > 8)

you really meant numeroMes > 10. Doesn't make a difference though as values 9 and 10 are matched before that.

Finally, you could shorten all those ifs to a simple formula:

return (int)Math.Ceiling(date.Month / 2.0);

Math.Ceiling rounds up the passed decimal number to the next integral number. So 1.5 would become 2 for example.

0

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.