How to post code snippets

To effectively help with coding/output problems, we need to see that code or output clearly. Discourse provides all the tools here to make this easy once you know how to use them. There are really only two simple guidelines:

  1. Use fenced code blocks to post code snippets
    • They help show where you code begins and ends. Plus it doesn’t mess with your spacing or indents which help to make the structure clear, and will scale it appropriately to the screen size.
    • When people are communicating code and there is no indentation and lousy spacing, code is super-hard to read.
  2. Do not post screenshots of code/messages
    • Can be tricky to read, snippets/corrections take more time to write/quote.
    • Takes less time to copy/paste than to take/post screenshot!
    • The one exception to this is for errors in Geant4 GUI/Vis displays

How do I use fenced code blocks?
A fenced code block begins with three backticks, i.e., ```, followed by your code or output, then the block is closed with another three backticks ```. You’re not limited to C++ here, any terminal commands or output can be put in these blocks.

For example, we could write a code block as:

```
int main(int argc,char** argv)
{
auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
runManager->SetUserInitialization(new DetectorConstruction());
runManager->SetUserInitialization(new QBBC);
runManager->SetUserInitialization(new ActionInitialization());

G4UImanager* UImanager = G4UImanager::GetUIpointer();
G4String command = "/control/execute ";
G4String fileName = argv[1];
UImanager->ApplyCommand(command+fileName);

delete runManager;
}
```

which will be displayed in the post as:

int main(int argc,char** argv)
{
  auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
  runManager->SetUserInitialization(new DetectorConstruction());
  runManager->SetUserInitialization(new QBBC);
  runManager->SetUserInitialization(new ActionInitialization());

  G4UImanager* UImanager = G4UImanager::GetUIpointer();
  G4String command = "/control/execute ";
  G4String fileName = argv[1];
  UImanager->ApplyCommand(command+fileName);

  delete runManager;
}

Much nicer!

6 Likes