2
\$\begingroup\$

I am new to programming and "programming thinking" as well. I have created a simple program that calculates future earnings from investing. It seems to be working correctly, but I want to be sure and ask some smarter guy out here, whether the program is really working as it should or whether it could be written in a better and cleaner form. Input in first month is 15000, Other months: 1000, End of time period, when I want to see the earnings: 72 months, growth/month: 0.75 %; Each new input grows every month by its 0.75 %

static void Main(string[] args)
        {
            double inpInt, inpMonth, inpMonthTrue, growMonth, monthCount, sum, inpTotal, earnTotal;
            inpInt = 15000;
            inpMonth = 0;
            inpMonthTrue = 1000;
            growMonth = 0.75;
            monthCount = 72;
            inpTotal = inpInt + inpMonthTrue * (monthCount - 1);
            sum = inpInt + inpMonth;
            inpInt += inpInt / 100 * 0.75;
            monthCount--;
            Console.WriteLine("Value in months:" );
            Console.WriteLine(inpInt);
            for (int i = 0; i < monthCount; i++)
            {
                inpMonth += inpMonthTrue;
                inpInt += inpInt / 100 * growMonth;
                inpMonth += inpMonth / 100 * growMonth;
                sum = inpInt + inpMonth;
                Console.WriteLine(sum);
            }
            earnTotal = sum - inpTotal;
            Console.WriteLine(String.Format("Total input: {0}", inpTotal));
            Console.WriteLine(String.Format("Total value: {0}", sum));
            Console.WriteLine(String.Format("Total earnings: {0}", earnTotal));
            Console.ReadLine();
        }

Thanks

\$\endgroup\$
1
  • 1
    \$\begingroup\$ which is true to your application logic 15000 ÷ (100 * 0.75) = 200 OR (15000 ÷ 100) x 0.75 = 112.5? as both have different results. if the correct result is the first one, then your calculation is invalid. \$\endgroup\$
    – iSR5
    May 16, 2020 at 18:39

2 Answers 2

1
\$\begingroup\$

Here are some points that I think can make your code look good -

  1. You can improve the readbility like you can group every set of operations with one line space. Like enter image description here
  2. The local variables can be renamed better as they're not clear to me when I look at the code directly without reading the problem statement. For example inpMonth and inpMonthTrue are confusing.

  3. You can include code comments on the logic that you're performing. For example in the below snippet, it is unclear what exactly is happening.

enter image description here

  1. The main() method can be broken into multiple meaningful methods to improve readability. Assuming that you'll be taking the input from the console enter image description here

This was just a gist of what I'm trying to convey. It needn't be the exact names, you can create any number of methods for grouping the related logic and code. Breaking a big tall method into multiple meaningful chunks gives more clarity. Plus, it is always helpful to detect any bugs that arise in the software, so you know where exactly to look into if the Interest is coming wrong for instance.

\$\endgroup\$
1
\$\begingroup\$

a few things you should considered :

  1. Never change the original values, and always make your changes on different copy.
  2. Always think of a good naming strategy (Naming Convention), based on type, and purpose of the code.
  3. Always make your code available for expansions by modulate your code into different group of actions or processes (use methods, properties, classes ..etc.) apply Object-Oriented-Programming principles.
  4. Always review your code, search for weaknesses, bugs, and simulate some invalid inputs (common ones) by testing the code and handle them (like adding validations, restrictions ..etc).
  5. Always separate the business logic from user logic (define classes for them along with any related necessary objects).
  6. before using a loop, search for a way to get the same results without a loop, sometimes you can get the same output and process without a loop specially in mathematical operations.
  7. simplify your code, and avoid redundancy whenever possible.
\$\endgroup\$

Your Answer

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

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