1
\$\begingroup\$

I have created a code that displays a table with exchange rate data. Have I used loops and conditions (if else) in a good way?

@model IEnumerable<CurrencyResponse>
@{
    ViewData["Title"] = "Index";
}
@foreach (var obj in Model)
{
    <div>
        <h5>Table: @obj.Table</h5>
        <h5>Table number: @obj.No</h5>
        <h5>Date of publication: @obj.EffectiveDate</h5>
        @if (obj.Table == "C")
        {
            <h5>Quotation date: @obj.TradingDate</h5>
        }
    </div>
}
<table class="table table-bordered table-striped" style="width: 100%">
    <thead>
    <tr>
        <th>
            Currency
        </th>
        <th>
            Code
        </th>
    @foreach (var obj in Model)
    {
        @if (obj.Table != "C")
        {
            <th>
                Mid
            </th>
        }
        else
        {
            <th>
                Bid
            </th>
            <th>
                Ask
            </th>
        }
    }
    </tr>
    </thead>
    <tbody>
        @foreach (var obj in Model)
        {
            foreach (var element in obj.Rates)
            {
                <tr>
                    <td width="50%">
                        @element.Currency
                    </td>
                    <td width="50%">
                        @element.Code
                    </td>
                    @if (obj.Table != "C")
                    {
                        <td width="50%">
                            @element.Mid
                        </td>
                    }
                    else
                    {
                        <td width="50%">
                            @element.Bid
                        </td>
                        <td width="50%">
                            @element.Ask
                        </td>
                    }
                </tr>
            }
        }
    </tbody>
</table>

As you can see, I'm using 4 loops to display the data the right way. I am using conditions to add extra html. I wonder if what I'm doing is right. Can it be somehow more optimized? In the created div I use h5 tags to display information about the table (A, B or C) and information about the date of publication of the exchange rates.

xml structure - (tables A, B, C):

endpointA

endpointB

endpointC

In case of ambiguity, I also attach all the code to the github: code

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$
    – Mast
    Aug 31, 2022 at 13:07

1 Answer 1

1
\$\begingroup\$

I'm not too familiar with MVC best practices, but I have some general suggestions.

First, if (obj.Table != "C") is pretty meaningless to a new reader. What is the actual condition being tested for here? Can you encapsulate this test in a function with a suggestive name?

Second, you should prefer using css classes to assigning styles like width directly to elements. You just have to decide how the styles should be scoped in your case - maybe with a class on the table.

\$\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.