21

My eyes are destroyed, so I'm using a screen reader. Whenever I post some codes to Stack Overflow and Code Review, I always got some comments about my code formatting not being readable and indented even when pressing "ctrl+k".

I discussed this problem with a user in chat and he keeps telling me to click something to format my code. I forgot what it is but I remember it contains {} or something like that. However I really can't find it so I assume that is an image and he confirmed it. How can I correctly format my code here? Is there another way without clicking that image? Many questions I posted got edited because the users say my code is unreadable.

I'm using eclipse and it has a feature "correct indentation" which I think formats my code but it seems to be not working because of the edits to my codes here.

I will try @Martijn Pieters instructions. I will use a simple c++ code. I added 4 extra spaces in each line of the code.

The include directive, the main declaration and the closing brace doesn't have any spaces when I coppied from eclipse.

#include <iostream>

int main() {
    int *crash = nullptr;
    std::cout << *crash << std::endl;
    return 0;
}

Is this correct?

edit: I made a longer code. Just to make sure I got it.

#include <iostream>
#include <string>
#include "SALES_DATA.h"

int main() {
    Sales_data data1, data2;
    double price = 0;
    std::cin >> data1.bookNo >> data1.unitsSold >> price;
    data1.revenue = data1.unitsSold*price;
    std::cin >> data2.bookNo >> data2.unitsSold >> price;
    data2.revenue = data2.unitsSold*price;
    if (data1.bookNo == data2.bookNo) {
        unsigned totalCount = data1.unitsSold+data2.unitsSold;
        double totalRevenue = data1.revenue+data2.revenue;
        std::cout << data1.bookNo << " " << totalCount << " " << totalRevenue << " ";
        if (totalCount != 0) {
            std::cout << totalRevenue/totalCount << std::endl;
        } else {
            std::cout << "(no sales)" << std::endl;
        }
        return 0;
    } else {
        std::cerr << "Data must refer to the same ISBN" << std::endl;
        return -1;
    }
}

Is this totally correct?

10

Yes, the code button uses an image. It does have a tooltip, your screenreader may be able to identify it to you as Code Sample <pre><code> Ctrl+K.

Code blocks are marked up by indentation. In Eclipse you can add 4 spaces extra indentation to the whole block, and those 4 spaces then cause the block to be interpreted as code (including syntax highlighting).

That is exactly what the button does; if you have selected whole lines in the editor, the button (or pressing CTRL+K adds 4 spaces at the start of each of the selected lines. If there are already are 4 spaces at the start of every selected line, the reverse happens.

If text that is not a complete line (so a single word, or several words) is selected, backticks (`) are put around the selected text. This renders the selected text as code inline, without syntax highlighting. Use this for variable names, object types, etc.; anything code related.

The code block in your first post on code review was marked up correctly, albeit that within the code block there was no indentation of the code itself.

  • Isn't an extra alternative to enclose the code in an opening <code> and closing </code> tag? – rene Jul 5 '14 at 14:24
  • @rene: Then you don't get the right formatting. No syntax highlighting, lines are wrapped, and the changed background color applies to the text, not the block. In other words, ugly. – Martijn Pieters Jul 5 '14 at 14:26
  • @rene it also needs <pre> tags then – 3ventic Jul 5 '14 at 14:27
  • 1
    @3ventic: and even then the resulting layout is odd. – Martijn Pieters Jul 5 '14 at 14:30
  • How is it odd in this test? @MartijnPieters – rene Jul 5 '14 at 14:33
  • @rene: Normally, you'd nest <pre> and <code> the other way around. Note the extra newlines however around the code. Even if you remove all whitespace between the first line and the opening <code><pre> tags you get a dangling 'grey block'. – Martijn Pieters Jul 5 '14 at 14:35
  • OK, I accept that. Leaves open why hitting ctrl+K doesn't work for the OP. It does for me... – rene Jul 5 '14 at 14:37
  • @rene: I can imagine being legally blind and using a screenreader makes it a lot harder to select the correct set of lines to indent them. – Martijn Pieters Jul 5 '14 at 14:38
  • 1
    That problem doesn't change with trying to find the right tooltip in the menu. That was the reason I hoped an inline solution would be available. – rene Jul 5 '14 at 14:42
  • @Martijn Pieters pleas check the code in my edit – recursivePointer Jul 5 '14 at 14:50
  • Do you mean the button and ctrl+k do the same thing? But people still edit my codes even though I always use ctrl+k. – recursivePointer Jul 5 '14 at 15:04
  • @wormUser: The block you added is formatted correctly, yes. – Martijn Pieters Jul 5 '14 at 15:05
  • @wormUser: Yes, the button and CTRL+K do the same thing. The keyboard combination is a shortcut for the button. The editing on the one post I saw was just to add some more formatting; you did nothing wrong but someone thought the post would look better still with some additional editing. – Martijn Pieters Jul 5 '14 at 15:06
  • Ok, what about my another question. codereview.stackexchange.com/questions/52194/…. The code has already been correctly formatted by other users. But if you can see the previous revisions of the post, what's wrong with my first code formatting in the question? – recursivePointer Jul 5 '14 at 15:14
  • @wormUser: Yes, I can see you were struggling with the formatting; not until revision 4 did you get anything resembling a code block working. Another user saw that you initially had more indentation in your code and made a suggested edit that better reflected your original code formatting. – Martijn Pieters Jul 5 '14 at 15:22
4

The problem here is that the HTML code is wrong. This is the “button” for formatting:

<li class="wmd-button" style="left: 125px;" id="wmd-code-button" title="Code Sample <pre><code> Ctrl+K">
  <span style="background-position: -80px 0px;"></span>
</li>

It is neither a button nor a link, so you can not access it per keyboard easily. The span element should be a button with text or at least an aria-labelledby attribute.

  • Um, but why didn't they made it a button or a link? – recursivePointer Jul 10 '14 at 5:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .