17

I often want to write filenames for code.

Normally I write like this:

Filename

string = "This is example code"

Is this okay? Is there a standard Stack Overflow way of markdown for writing filename?

  • 2
  • 1
    File names can be long and contain dots that might be confused with punctuation, and spaces, as sometimes happens in MS Windows. (Whole file paths can even contain slashes and colons.) Inline code style can prevent the such file names from blending in with surrounding sentence and thereby ruining it. For example: This is a sentence containing a file named oh this name has spaces.and.dots which may be hard to read without the file in inline code style. Italics or bold don't solve the problem well since they don't "bind" the parts of the file name as closely as the code style box. – Carsten Führmann Dec 10 '18 at 9:46
11

This is ok if you write it the way you shown (the filename on one line, then on other lines code following). But if you have something like this:

In my viewcontroller.h I have:

some code there

I personally would like to see it as

In my viewcontroller.h I have:

some code there
  • 2
    So viewcontroller.h is the name of the method here as indicated by the code formatting? – random Jan 10 '15 at 20:54
  • @rand no... it is just a bit unreadable without codformatting. – nicael Jan 10 '15 at 20:55
  • What is the actual markdown syntax to create example 2? – Kurt Feb 15 '17 at 18:04
  • 1
    Click edit and see yourself, @Kurt :) – nicael Feb 15 '17 at 18:25
4

You can make a very nice-looking named code block using small headers and inline code, like so:

#### **`hello_world.js`**
``` js
alert("Hello world!");
```
hello_world.js
alert("Hello world!");

I've accidentally found it in this post on Formatting Sandbox and just don't want this great trick to get lost there as I was unsuccessfully looking for this some time before.

  • it seems to produce <blockquote> <code>code.js</code> <pre>...</pre> </blockquote> – myrdd Jul 28 at 5:19
  • @myrdd: Not sure what you mean. Blockquote was added by the author, the code produces what's inside blockquote. – EvgenKo423 Jul 28 at 8:12
  • 1
    Related question and an answer by me explaining, why this seems no be no good idea: meta.stackexchange.com/q/327657/364003 – MEE - Reinstate Monica Jul 28 at 16:03
  • @EvgenKo423 it's simply the HTML code corresponding to the Markdown code (#### + ```) – myrdd Jul 29 at 7:09
0

I'd never thought about it until seeing nicael's answer, but I kind of like "quoting the file".

test.bat

@echo off
setlocal enableDelayedExpansion
set argument1=!%1!
echo %argument1%>test.txt

file2.bat

@echo off
echo %~1
  • And why posting a new answer, just to repeat what other user answered? – Shadow The Princess Wizard Feb 1 '16 at 21:44
  • 1
    He wasn't literally recommending using quotes. I am. – Devil's Advocate Feb 1 '16 at 21:45
-3

Bolding is fine, though I think that the header markup you've used may be a little too forceful. Large text could help, though, if you had a lot of (hopefully small!) snippets in your post. I'm not sure we need a standard for this: use what makes your post most easily read and understood.

I will say, however, that code markup is definitely not the proper choice. The name of a file is not an element of a program; marking it as such is semantically incorrect.

  • 2
    "code markup is definitely not the proper choice" - it is hard to read completely unformatted complex file names with extensions when embedded inline, not the file name on one line and nothing else on this line – nicael Jan 10 '15 at 20:22
  • 2
    The name of the files are definitely elements of the program - If ArrayUtils.java isn't the name for a class of utility methods for arrays, the asker is doing it wrong. main.js, static.js, etc. – Unihedron Jan 10 '15 at 20:41
  • 2
    The currently-chosen formatting is not a justification for markup abuse, @nicael. – Josh Caswell Jan 10 '15 at 20:42
  • 1
    They are not executable code in themselves, @Unihedro. – Josh Caswell Jan 10 '15 at 20:43
  • 3
    @JoshCaswell Should XML files of an Android project not be wrapped in code formatting then? They may be part of the program (an essential one), but are not executable. – Unihedron Jan 10 '15 at 20:45
  • 2
    What part of a filename is code unless it's used in the code? @uni – random Jan 10 '15 at 20:55
  • @random: I never said or implied that they are code, just that they are indeed elements of a program, contrary to what Josh attempted to bring otherwise. – Unihedron Jan 10 '15 at 20:58
  • So are we or are we not talking about semantics? @uni – random Jan 10 '15 at 21:00
  • 1
    XML is parsed by another program and interpreted, @Unihedro. A bare filename is not. – Josh Caswell Jan 10 '15 at 21:03
  • 1
    @random: Elements of a program are formatted with `inline code`, so includes standard input, test cases, relevant directories and file names and executables and constants and related configurations. I do it, and I see everyone else do it, and I teach new users to use the site by leaving a comment about using inline styles instead of italics, and they agree. Not sure what being parsed takes significance here, @Josh. Perhaps python2 myprogram.py foo bar... – Unihedron Jan 10 '15 at 21:07
  • 1
    Context matters. help.github.com/articles/generating-ssh-keys But why would anyone think italics, which are used for titles or section headings, is first before bold on filenames? @uni – random Jan 10 '15 at 21:11
  • 1
    @random Your linked article formatted "Keep in mind that your key may also be named id_dsa.pub, id_ecdsa.pub or id_ed25519.pub" inline with code formatting, that's what I meant because it's related to a keyfile. As for using italics for filenames, I guess they used a "traditional" BBCode forum with no code formatting ever, but I can't be sure. – Unihedron Jan 10 '15 at 21:16
  • 1
    The link shows two ways to format filenames. Unformatted when used in text, formatted as code when shown as part of an output or parameter. @uni – random Jan 10 '15 at 21:39

You must log in to answer this question.

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