Panel | ||
---|---|---|
| ||
A key aspect of variable naming accuracy is including the primary “what” in the variable name for what the variable represents. Variables typically will represent some kind of “noun.” Make sure you don’t leave that information out of the variable name when naming it. |
Consider the following code:
Navitabs tab wizard |
---|
{"type":"custom","custom":{"tabs":[{"id":"6beb2758-cabe-4575-b7b3-cf26b2c25522","value":"C++","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"ExecutableProgram runnable_on_linux = GetProgram(executable_path);"}]},{"type":"paragraph","content":[{"type":"text","text":"From the data type assigned to this variable, it looks like this variable is for some kind of “executable program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnable_on_linux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"ExecutableProgram linux_program = GetProgram(executable_path);"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"bd6bd755-3ffd-4cc0-8657-fe132035a30e","value":"C#","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"ExecutableProgram runnableOnLinux = GetProgram(executablePath);"}]},{"type":"paragraph","content":[{"type":"text","text":"From the data type assigned to this variable, it looks like this variable is for some kind of “executable program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnableOnLinux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"ExecutableProgram linuxProgram = GetProgram(executablePath);"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"aec743a9-ddd6-4855-b107-6089fd5f121d","value":"Java","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"ExecutableProgram runnableOnLinux = getProgram(executablePath);"}]},{"type":"paragraph","content":[{"type":"text","text":"From the data type assigned to this variable, it looks like this variable is for some kind of “executable program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnableOnLinux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"ExecutableProgram linuxProgram = getProgram(executablePath);"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"0815c6f4-53b6-4503-b980-f0ef904c88f6","value":"Python","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"runnable_on_linux: ExecutableProgram = get_program(executable_path)"}]},{"type":"paragraph","content":[{"type":"text","text":"From the data type assigned to this variable, it looks like this variable is for some kind of “executable program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnable_on_linux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"linux_program: ExecutableProgram = get_program(executable_path)"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"ea755adf-2916-4749-a8c3-20cb2facee41","value":"JavaScript","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"let runnableOnLinux = getProgram(executablePath);"}]},{"type":"paragraph","content":[{"type":"text","text":"From the function called whose return value assigned to this variable, it looks like this variable is for some kind of “program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnableOnLinux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"let linuxProgram = getProgram(executablePath);"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"78e307b1-5b65-4c32-a9e0-12552b3b1962","value":"TypeScript","body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A poor variable name lacking the primary “what” in its name:","marks":[{"type":"strong"}]}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"let runnableOnLinux: ExecutableProgram = getProgram(executablePath);"}]},{"type":"paragraph","content":[{"type":"text","text":"From the data type assigned to this variable, it looks like this variable is for some kind of “executable program” object. However, that information isn’t evident from the "},{"type":"text","text":"runnableOnLinux","marks":[{"type":"code"}]},{"type":"text","text":" variable name itself - that variable name looks like it might be a boolean, and there’s no real indication in the variable name itself of exactly “what” is runnable on Linux."}]},{"type":"paragraph","content":[{"type":"text","text":"Revising the name to include the “what”: ","marks":[{"type":"strong"}]},{"type":"text","text":"You’d want to at least include the primary noun (“program”) in the variable name. That makes it much clearer “what” the primary thing the variable holds is:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"let linuxProgram: ExecutableProgram = getProgram(executablePath);"}]},{"type":"paragraph","content":[]}]},"type":"your-content"}],"design":null,"minHeight":0,"vertical":false,"isEmbedded":false}} |