{"id":398459,"date":"2024-06-29T14:05:20","date_gmt":"2024-06-29T14:05:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=398459"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=398459","title":{"rendered":"<span>Guide to naming in code<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Summary\u00a0<\/h3>\n<p>We present\u00a0a guide\u00a0to\u00a0name\u00a0entities in code\u00a0based on\u00a0putting naming\u00a0in perspectives of\u00a0semantic\u00a0space, design,\u00a0and\u00a0readability.\u00a0The\u00a0main\u00a0idea is that naming\u00a0should not be considered as\u00a0creation of\u00a0tags, but as\u00a0a fundamental part\u00a0of design\u00a0process, which implies integral and consistent vocabulary to be used. We discuss naming process and naming formalism from these perspectives and we provide guides for practical use. The work is based on 15 years of experience in engineering work, coding and development management in high-tech industries.<\/p>\n<h3>Contents<\/h3>\n<p><a href=\"#names_in_engineering\" rel=\"noopener noreferrer nofollow\">Names in Software Engineering<\/a><br \/><a href=\"#principles_of_name_design\" rel=\"noopener noreferrer nofollow\">Principles of Name Design<\/a><br \/><a href=\"#bad_naming\" rel=\"noopener noreferrer nofollow\">Bad Naming<\/a><br \/><a href=\"#naming_process\" rel=\"noopener noreferrer nofollow\">Naming Process<\/a><br \/><a href=\"#fast_method\" rel=\"noopener noreferrer nofollow\">Fast Method<\/a><br \/><a href=\"#long_method\" rel=\"noopener noreferrer nofollow\">Long Method<\/a><br \/><a href=\"#formatting\" rel=\"noopener noreferrer nofollow\">Formatting<\/a><br \/><a href=\"#details_and_examples\" rel=\"noopener noreferrer nofollow\">Details &amp; Examples<\/a><br \/><a href=\"#final_remarks\" rel=\"noopener noreferrer nofollow\">Final Remarks<\/a><\/p>\n<p><a class=\"anchor\" name=\"names_in_engineering\" id=\"names_in_engineering\"><\/a><\/p>\n<h2>Names in Software Engineering<\/h2>\n<p>Let us consider what a programmer does. He models a situation in life to create a program\u00a0which adds some value to it.<\/p>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/280\/0ff\/b79\/2800ffb799f22c4db2a450bd69f58451.png\" width=\"749\" height=\"80\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/280\/0ff\/b79\/2800ffb799f22c4db2a450bd69f58451.png\"\/><figcaption><\/figcaption><\/figure>\n<p>While doing this he\u00a0extracts some things from the situation, conceptualizes them, and names\u00a0them. Like a table is a concept of a wooden construction we see, and we name it using a word \u201ctable\u201d.\u00a0Note, all three are different.\u00a0  <\/p>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a2e\/c5d\/438\/a2ec5d43843460a46c5488637274dfa7.png\" width=\"1126\" height=\"118\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a2e\/c5d\/438\/a2ec5d43843460a46c5488637274dfa7.png\"\/><figcaption><\/figcaption><\/figure>\n<p>In all this work\u00a0a programmer\u00a0uses some language to describe the situation with a certain vocabulary.\u00a0This vocabulary defines and reflects meaning we put into our program, so it also defines if the program is modelling the situation well enough to add\u00a0some value to it.\u00a0Vocabulary in turn can be a source for building names of parts of\u00a0a\u00a0program.\u00a0<\/p>\n<p>We see three\u00a0dimensions meeting\u00a0here: world, thinking, artifact. These dimensions are pair-wise connected.\u00a0\u00a0<\/p>\n<p>We also see two levels of abstraction meeting\u00a0here: low-level, dealing with a part;\u00a0high-level, dealing with a whole. These are also cross-dependent.\u00a0<\/p>\n<p>This\u00a0can be visualized in\u00a0a\u00a0triangle model where entities are pair-wise connected.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0c3\/40e\/541\/0c340e5411e687905d55e1d24dfdb41c.png\" width=\"1222\" height=\"596\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0c3\/40e\/541\/0c340e5411e687905d55e1d24dfdb41c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Name is a reflection of meaning and design. When you name properly, you think properly, you design properly, and you use properly. Bad names lead to improper usage, bad design and bugs.\u00a0  <\/p>\n<p>Programming is also creation of texts.\u00a0Other programmers or the same programmer in future read the text and decode its concepts. Names are essential part of transferring proper meaning, so actions of future maintainers will not harm integrity of the program.\u00a0\u00a0  <\/p>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/aac\/59b\/5b3\/aac59b5b3bd82829a714696f646b270f.png\" width=\"582\" height=\"310\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/aac\/59b\/5b3\/aac59b5b3bd82829a714696f646b270f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>This shows that naming\u00a0is\u00a0essential part the whole\u00a0process of\u00a0software engineering, so their design impact all the rest in\u00a0it.  <\/p>\n<h2>Principles of Name Design<\/h2>\n<p><a class=\"anchor\" name=\"principles_of_name_design\" id=\"principles_of_name_design\"><\/a><\/p>\n<p>Thus, design of names, design of vocabulary and design of a\u00a0model\u00a0should be aligned, so should the principles we use for it.\u00a0<\/p>\n<p>In\u00a0software engineering, as in any engineering discipline, in most cases\u00a0a model is mechanical,\u00a0i.e.\u00a0<\/p>\n<ul>\n<li>\n<p>a\u00a0system is a set of separate parts, each having its own concrete function.<\/p>\n<\/li>\n<\/ul>\n<p>Engineer needs to limit\u00a0entities he works with\u00a0simultaneously\u00a0because of limitation of brain, and he usually creates\u00a0generalizations and hierarchy\u00a0in a model, which implies:\u00a0<\/p>\n<ul>\n<li>\n<p>one entity\u00a0has\u00a0one name,\u00a0<\/p>\n<\/li>\n<li>\n<p>vocabulary\u00a0has hierarchical structure,\u00a0<\/p>\n<\/li>\n<li>\n<p>no overlaps\u00a0between names at the same hierarchy level.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Usually,\u00a0an engineer works with other engineers, so\u00a0\u00a0<\/p>\n<ul>\n<li>\n<p>the vocabulary should be shared between all the engineers.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Try to think of these general principles of software design in application to naming:\u00a0<\/p>\n<ul>\n<li>\n<p>consistency between parts\u00a0of an integral system;\u00a0<\/p>\n<\/li>\n<li>\n<p>proper level of specification\/generalization;\u00a0<\/p>\n<\/li>\n<li>\n<p>proper level of modularity\/coupling;\u00a0<\/p>\n<\/li>\n<li>\n<p>maintainability and cognitive-friendly interface;\u00a0<\/p>\n<\/li>\n<li>\n<p>future-awareness \u2013 need to design taking evolution of system into account;\u00a0<\/p>\n<\/li>\n<li>\n<p>support of various scenarios and corner cases.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>As program is a text, we need to make names readable as easy in a normal text to improve understanding of it while reading. It\u00a0means we need to prefer English over formal language, and full words over excessive shortening.\u00a0  <\/p>\n<p><a class=\"anchor\" name=\"bad_naming\" id=\"bad_naming\"><\/a><\/p>\n<h2>Bad Naming<\/h2>\n<p>As it is usually not that easy to\u00a0fix\u00a0the architecture, the same holds for the vocabulary.\u00a0Terms you use to describe your system\u00a0cannot be fixed by simple\u00a0Find\/Replace in most cases.\u00a0They\u00a0impact\u00a0a\u00a0maintainer, and\u00a0lead to wrong\u00a0derivative\u00a0changes\u00a0in other names and in structure.\u00a0\u00a0<\/p>\n<p>Names can be bad if they\u00a0mislead,\u00a0can mislead,\u00a0are ambiguous, are too specific or too general,\u00a0require time to understand,\u00a0are\u00a0inconsistent with context\u00a0or\u00a0other names\u00a0or a convention,\u00a0describe too little or too much.\u00a0<\/p>\n<p>Names can be bad because of bad design, for example if the same entity\u00a0has different meaning\u00a0in different contexts.\u00a0In this case to fix name you need to redesign the entity.\u00a0<\/p>\n<p>Bad names in small pieces of code are\u00a0not\u00a0an exclusion.\u00a0It is better to speak the same language in all parts of an integral space of your program.\u00a0\u00a0<\/p>\n<p><a class=\"anchor\" name=\"naming_process\" id=\"naming_process\"><\/a><\/p>\n<h2>Naming Process<\/h2>\n<p>Naming\u00a0can be approached as a\u00a0process of finding meaning of entities\u00a0and expressing it\u00a0clearly\u00a0within a set of other names.\u00a0\u00a0<\/p>\n<p>Since you spend time on design in general, you need to make time\u00a0for\u00a0name design. Reluctance of doing the latter is inconsistency which has no excuse, because it harms design and\u00a0semantic space.\u00a0<\/p>\n<p>Engineering work contains of iterations.\u00a0The\u00a0knowledge of what you work with and what is to be done increase gradually:\u00a0<\/p>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/75a\/0c1\/07f\/75a0c107f0e4f1c7a526413701296b8c.png\" width=\"548\" height=\"284\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/75a\/0c1\/07f\/75a0c107f0e4f1c7a526413701296b8c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Speculative (mental) experiments\u00a0are naturally more\u00a0used for naming\u00a0since they are faster and since they precede\u00a0more\u00a0expensive\u00a0physical experimentation.\u00a0Also, impact\u00a0of names\u00a0is long-term and not always easily physically tested\u00a0right away.<\/p>\n<p>Names are derivatives from terms you have in a vocabulary you\u00a0use to describe\u00a0your\u00a0model.\u00a0So,\u00a0naming process starts with understanding of what terms are more appropriate\u00a0for\u00a0your\u00a0model to reflect a situation in life you work with.\u00a0\u00a0<\/p>\n<p>Terms are more heavyweight than names, so more time should be spent\u00a0on\u00a0them.\u00a0It is the time\u00a0when\u00a0you\u00a0create\u00a0a\u00a0value of\u00a0an\u00a0insight about\u00a0a\u00a0problem and a solution.\u00a0\u00a0<\/p>\n<p>Terms also define\u00a0ability of a solution to evolve when a situation in life evolves.\u00a0Fitting of key things of a situation with the terms\u00a0you use in your solution lead to\u00a0a robustness of a solution.\u00a0\u00a0<\/p>\n<p>Concrete names should be built\u00a0of terms\u00a0in\u00a0a\u00a0more formal way,\u00a0using\u00a0conventions.\u00a0<\/p>\n<p><a class=\"anchor\" name=\"fast_method\" id=\"fast_method\"><\/a><\/p>\n<h2>Fast Method<\/h2>\n<p>Fast\u00a0method is for usage while writing a draft or a small part in an already established code.\u00a0<\/p>\n<ol>\n<li>\n<p><strong>Describe\u00a0an entity \u201cusing your words\u201d<\/strong>\u00a0<br \/>What is your intention? How would you use it?\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Use terms from the context\/existing vocabulary to compile a derivative name<\/strong>\u00a0<br \/>Do not create synonyms and do not use different wording for the same thing.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check if the entity can be\u00a0properly understood from the same line it will be used in<br \/><\/strong>Do not\u00a0imply that the reader has go to the definition\/somewhere else to understand it correctly.\u00a0Switching between contexts is for finding details, not for finding the idea. Switching to another context make you lose the original one.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Ask yourself: can others misinterpret the name?<br \/><\/strong>Even if the misinterpretation will happen in 5% of cases it may lead to hours of debugging and worsen maintainability.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check readability,\u00a0prefer English over formal.<\/strong>\u00a0<br \/>A text that can be easily read is easier to understand.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"long_method\" id=\"long_method\"><\/a><\/p>\n<h2>Long Method<\/h2>\n<p>Hard cases usually mean there are flaws in the design and the understanding of how the system (should) work. Instead of thinking \u201clet us leave it as is and do some real work\u201d, spending extra time in this case is a natural need.\u00a0<\/p>\n<ol>\n<li>\n<p><strong>Create\u00a0a long\u00a0description\u00a0of an entity\u00a0\u201cusing your words\u201d<\/strong>\u00a0<br \/>What is your intention? How would you use it?\u00a0\u00a0<br \/>Write\u00a0the long description down, look at it.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Make the description\u00a0precise, i.e. create a definition<\/strong>\u00a0<br \/>What is the entity\u2019s place and\u00a0scope in the\u00a0system? How is it different from\u00a0the\u00a0other entities you have in the same system and from\u00a0the\u00a0entities in use context of the system (if it is visible outside)? Do you put more specifics than needed? Do you generalize what you should not?\u00a0Usually you increase level of abstraction vs. the values of the entity.\u00a0<br \/>Write\u00a0precise description down, look at it.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Create\u00a0variants\u00a0of long names<\/strong>\u00a0<br \/>Try to put the essence of the\u00a0definition\u00a0in few words. Use English, not formal language here.\u00a0Create multiple variants.\u00a0<br \/>Write\u00a0the list down, look at it.\u00a0<br \/>Put names in use context in code, look\u00a0at\u00a0the code.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check vocabulary\u00a0<\/strong>\u00a0<br \/>Check consistency with other parts of your code.\u00a0Do you create a synonym?\u00a0Do you use terms which may\u00a0not be understood by\u00a0a newcomer who will read your code 10 years later? Is it easy to read? Can it mislead someone even if it is formally perfect? How much time is required to figure out the correct meaning of the variable? A user should be able to read your code as fast as he reads\u00a0a normal text.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Consider context to shorten names<\/strong>\u00a0<br \/>What will be understood from the usage, so can be omitted? What\u00a0can be omitted without\u00a0losing\u00a0possibility to distinguish\u00a0the entity\u00a0from other entities?\u00a0\u00a0<br \/>Several words are OK if needed,\u00a0if you move 30 lines into a new function, it is OK to spend few more words to describe it in\u00a0a\u00a0name.\u00a0<br \/>Put\u00a0shorter\u00a0names in use context in code, look\u00a0at\u00a0the code.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Make a survey to decide\u00a0on\u00a0a winner<\/strong>\u00a0<br \/>The survey can be\u00a0a\u00a0mental experiment\u00a0of physical. For\u00a0the\u00a0mental experiment\u00a0you need to\u00a0compare\u00a0the names as\u00a0other\u00a0code users.\u00a0Both ways have limitations.\u00a0If you will ask\u00a0people,\u00a0they will be unable to identify\u00a0in a short time\u00a0if what you suggest is aligned with your design and purpose.\u00a0Mental experimentation\u00a0requires a skill and usually is very biased by your own opinion.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Sometimes naming analysis leads to redesign,\u00a0do\u00a0it<\/strong>\u00a0<br \/>This is a natural consequence of you getting more knowledge of the system you work with, while finding meaning of entities.\u00a0\u00a0<br \/>Single entity with multiple responsibilities often\u00a0makes it uneasy to name it and maintain it. It might happen if your entity is not logically integral. You may split it into parts in this case.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Create appropriate comment about complex entities\u00a0<br \/><\/strong>It may happen, that a name cannot hold everything you want to share with a reader. Add a comment\u00a0with\u00a0full description.\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Non-descriptive names should be rare negotiated conventions<br \/><\/strong>You may agree with your colleagues that you will use the name for this entity even if it cannot be understood straight without additional knowledge. For example, abbreviations go here.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"formatting\" id=\"formatting\"><\/a><\/p>\n<h2>Formatting<\/h2>\n<p>As\u00a0code is a text\u00a0which\u00a0is read by people, we may reuse principles of creation of a natural language text for code. In\u00a0a\u00a0natural language text\u00a0words are separated by\u00a0spaces of the same length in a line,\u00a0comma is followed by a space and has no leading space, etc.\u00a0And this is applied for all paragraphs. It is better to do the same in code.\u00a0<\/p>\n<p>The broken window principle\u00a0is\u00a0applicable\u00a0for name formatting. If one breaks a window in a car\u00a0in an unsafe district, after some time the car will be left without wheels. Something done improperly sets the culture of negligence and pollutes everything around.\u00a0<\/p>\n<p>Stick to a style which is preferred in the code you change.\u00a0If you begin a new module, you are\u00a0freer\u00a0in choice.\u00a0Main\u00a0name formatting\u00a0styles are:\u00a0<\/p>\n<ul>\n<li>\n<p><code>snake_case<\/code>\u00a0\u2013 fluently readable as underscore is very similar to a space;\u00a0<\/p>\n<\/li>\n<li>\n<p><code>CamelCase<\/code> \u2013 readable, capital letters naturally attract attention;\u00a0<\/p>\n<\/li>\n<li>\n<p><code>camelCase<\/code> \u2013 readable, can be used to distinguish from CamelCase.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Convention and consistency\u00a0in naming\u00a0open a very useful possibility to\u00a0encode more meaning in names\u00a0using styles.\u00a0Styles can be used to differentiate entities by their\u00a0class, which increase readability a lot, e.g.:\u00a0<\/p>\n<ul>\n<li>\n<p><code>snake_case <\/code>for variables, <code>CamelCase<\/code> for classes and functions;\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><code>snake_case <\/code>for variables, <code>CamelCase<\/code> for classes, <code>camelCase<\/code> for functions.\u00a0\u00a0<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p><code>camelCase<\/code> for variables, <code>CamelCase<\/code> for classes and functions.\u00a0<\/p>\n<\/li>\n<li>\n<p><code>snake_case <\/code>for variables and functions, <code>CamelCase<\/code>\u00a0for classes in Python\u00a0<\/p>\n<\/li>\n<li>\n<p><code>snake_case <\/code>for almost everything in C++ STL\u00a0<\/p>\n<\/li>\n<li>\n<p><code>QCamelCase<\/code>\u00a0for classes and camelCase for methods\u00a0in Qt\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Hungarian notation\u00a0put\u00a0variable\u00a0type in\u00a0all variable\u00a0names.\u00a0If you create a new style, avoid that, names are for meaning,\u00a0not for implementation.\u00a0However,\u00a0if\u00a0a type is a part of meaning,\u00a0in\u00a0can be in the name.\u00a0For example, is you create an instance of a class, the class name is usually already abstracted from the implementation well enough to be used in an instance\u00a0name.\u00a0<\/p>\n<p>Some styles use <code>_<\/code>\u00a0pre- and postfixes\u00a0for marking non-public fields of classes. This is useful because it shows the scope in which a variable is used, which adds to understanding of its impact and its dependencies:\u00a0<\/p>\n<ul>\n<li>\n<p><code>_private_function<\/code>\u00a0#\u00a0in\u00a0Python\u00a0and Dart\u00a0<\/p>\n<\/li>\n<li>\n<p><code>int\u00a0private_member_;<\/code> \/\/\u00a0Google C++ Style Guide\u00a0<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"details_and_examples\" id=\"details_and_examples\"><\/a><\/p>\n<h2>Details &amp; Examples<\/h2>\n<ol>\n<li>\n<p><strong>Meaning should be understood from the same line.\u00a0<\/strong>Switching of contexts takes time and distracts from thinking on a topic.\u00a0Do not expect jumping of eye back and forth as a standard way to understand your code.\u00a0Do not expect that the correct meaning can only be understood from the definition, people will unlikely\u00a0to\u00a0go to the definition and will work with their guess instead.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>If meaning cannot be understood from the same line \u2013\u00a0make it explicitly visible.\u00a0<\/strong>Place a comment to describe what is happening and\u00a0why, and\u00a0provide a reference for further reading if needed.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Do not use one-two-letter-per-word abbreviations<\/strong>, even if this is\u00a0very local.\u00a0Eventually it may become bigger and,\u00a0even if not,\u00a0such code\u00a0is not easy to understand from this line only, you need to get back to definitions of these variables\u00a0and spend extra time to understand what is going on correctly.\u00a0Abbreviations which are agreed convention is an exception, but these should be rare.\u00a0<\/p>\n<p><strong>Bad:<\/strong>\u00a0<code>wcf.add(cr);<\/code>\u00a0\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Prefer\u00a0English\u00a0over\u00a0formal<\/strong>\u00a0(but\u00a0keep\u00a0it formally\u00a0correct).\u00a0Name things so they can be read naturally in English. Avoid compound noun form if it can be misleading.\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0 <code>bool\u00a0selection_all() const;<\/code>\u00a0<br \/><strong>Good<\/strong>: <code>bool\u00a0all_selected() const<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<code>file_unable_to_parse,<\/code>\u00a0<br \/><strong>Good:<\/strong>\u00a0<code>unable_to_parse_file,<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<code>DesignTypeNeedToOpen();<\/code>\u00a0<br \/><strong>Good<\/strong>:\u00a0<code>TypeOfDesignToOpen();<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>SetAbstractGeometryStatus();<\/code> \/\/ What is abstract geometry status? Status of abstract geometry? abstract status of geometry? something else? Just by reading the name you cannot understand what it really is and is made for. In most cases it means that the name is wrong.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Narrow scope of usage and visibility.\u00a0<\/strong>\u00a0<strong>Create variables as close to their usage as possible.\u00a0<\/strong>This\u00a0improves\u00a0readability and decrease coupling of code.\u00a0This helps\u00a0with\u00a0having simpler names, as narrow context of usage requires less specifics to distinguish entities from each other.\u00a0This will also tell a reader that there is no need to check if this variable is used anywhere else.\u00a0Use code blocks\u00a0and namespaces\u00a0for that.\u00a0<\/p>\n<p>Violating this rule harms\u00a0code understanding and harms design, since others may use variables in an unintended way, creating more coupling between modules than needed.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Do not create synonyms<\/strong>, use one term for everything\u00a0related to what is meant by this term.\u00a0You need to name\u00a0one\u00a0entity the same way everywhere.\u00a0This way people will faster understand code and will be more careful when noting differences in names \u2013 they will expect the meaning is also different.\u00a0\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<code>char\u00a0console[] = \"STD_OUT\";<\/code>\u00a0<br \/><strong>Good<\/strong>: <code>char\u00a0std_out_name[]\u00a0= \"STD_OUT\";<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<br \/><code>QLineEdit*\u00a0rundir_editor_ =\u00a0nullptr;<br \/>QPushButton*\u00a0select_dir_ =\u00a0nullptr;\u00a0\/\/ is\u00a0dir\u00a0different from\u00a0rundir?<\/code>\u00a0<br \/><strong>Good<\/strong>:\u00a0<br \/><code>QLineEdit*\u00a0rundir_editor_ =\u00a0nullptr;\u00a0<br \/>QPushButton*\u00a0select_rundir_ =\u00a0nullptr;<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>return\u00a0RecentMenuItemTextAndTooltip(mru_text,\u00a0menu_item_tooltip);<\/code>\u00a0\/\/ all names refer to one entity using different wording\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Do not use shortenings of words as a main method. <\/strong>It leads to overly relaxed attitude to naming and indulges to the reluctance to find simpler and shorter terms. It also adds cognitive complexity, because it leads to non-intuitive shortenings.<\/p>\n<\/li>\n<li>\n<p><strong>Remove unnecessary prefixes and postfixes<\/strong>.\u00a0\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<code>bool\u00a0is_visual_mode() const { return\u00a0visual_mode_; }<\/code>\u00a0<br \/><strong>Bad<\/strong>:\u00a0 <code>bool\u00a0get_visual_mode() const { return\u00a0visual_mode_; }<\/code>\u00a0<br \/><strong>Good<\/strong>: <code>bool\u00a0visual_mode()\u00a0const { return\u00a0visual_mode_; }<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Object should be named per its purpose and data, not per\u00a0its\u00a0type.<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>: <code>Coloring\u00a0coloring_;<\/code>\u00a0\u00a0<br \/><strong>Good<\/strong>: <code>Coloring\u00a0custom_colors_;<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Use inline comment to identify meaning of arguments passed by value,\u00a0<\/strong>so the line can be understood without checking the function definition.\u00a0<\/p>\n<p><strong>Good<\/strong>:\u00a0<code>traits.set_color(color, true \/*custom*\/);<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>If\u00a0a\u00a0function does\u00a0some things, it should be named with a verb.<\/strong>\u00a0<\/p>\n<p><strong>Bad:<\/strong>\u00a0 <code>void\u00a0JobsQueueUpdate();<\/code>\u00a0<br \/><strong>Good:<\/strong>\u00a0<code>void\u00a0UpdateJobsQueue();<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>If\u00a0a function is a\u00a0simple\u00a0getter of a parameter\u00a0or a property, it should be a noun\u00a0or an adjective.\u00a0<\/strong>To preserve that in\u00a0some conventions\u00a0simple getters can be named\u00a0using\u00a0the same style as variables, e.g. in\u00a0<code>snake_case()<\/code><strong>.<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>: <code>string\u00a0GetName() const<\/code>;\u00a0<br \/><strong>Good<\/strong>: <code>sting name() const<\/code>;\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Function name should not contain argument type in general,\u00a0<\/strong>because each function call will already mention its argument.\u00a0<\/p>\n<p><strong>Bad<\/strong>: <code>bool\u00a0ValidateRectangle(const Rectangle&amp;) const;<\/code>\u00a0<br \/><strong>Good<\/strong>: <code>bool\u00a0Validate(const Rectangle&amp;);<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Function name\u00a0should not mislead about what it does.\u00a0<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<br \/><code>bool\u00a0FilesAreAvailable(files)\u00a0{ \/\/ may set expectation that all files will be checked\u00a0<br \/>\u00a0 for file in files:\u00a0<br \/>\u00a0\u00a0\u00a0 if\u00a0reader.open_existing(file):\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 return true;\u00a0 \/\/\u00a0but\u00a0it only checks if\u00a0at least one file is available<br \/>\u00a0\u00a0return false;\u00a0<br \/>}<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Name function by\u00a0its\u00a0resulting effect, not by <em>one<\/em>\u00a0of possible applications\u00a0<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<br \/><code>void\u00a0RunVisualDebugger(char*\u00a0argv[], int*\u00a0exit_status) {\u00a0<br \/>\u00a0\u00a0forksys(argv,\u00a0exit_status);\u00a0<br \/>}<\/code>\u00a0<br \/><strong>Good<\/strong>:\u00a0<br \/><code>void Fork(char*\u00a0argv[], int*\u00a0exit_status) {\u00a0<br \/>\u00a0\u00a0forksys(argv,\u00a0exit_status);\u00a0<br \/>}<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Avoid putting implementation\u00a0in name<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<br \/><code>void\u00a0CallLsfork(char*\u00a0argv[], int*\u00a0exit_status) {\u00a0<br \/>\u00a0\u00a0lsforksys(argv,\u00a0exit_status);\u00a0\/\/can change in future\u00a0<br \/>}<\/code>\u00a0<br \/><strong>Good<\/strong>:\u00a0<br \/><code>void Fork(char*\u00a0argv[], int*\u00a0exit_status) {\u00a0<br \/>\u00a0\u00a0lsforksys(argv,\u00a0exit_status);\u00a0<br \/>}<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Predicates should be named such that they\u00a0will\u00a0be interpreted as predicates<\/strong>: having true\/false possible states, not multiple states, are not confused with an instance of a class.\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>failed_state\u00a0= True<\/code> # name assumes multiple failed values; also, is this about politics and sociology?\u00a0<br \/><strong>Good<\/strong>: <code>failed =\u00a0True<\/code> #\u00a0`if\u00a0failed:`\u00a0is perfectly readable\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>&lt;instance>.worker_\u00a0= True<\/code> #\u00a0member\u00a0&#8216;worker_&#8217; can be read as instance of Worker class, not as predicate\u00a0in other contexts\u00a0<br \/><strong>Good<\/strong>:\u00a0<code>&lt;instance>.is_worker_<\/code>\u00a0= True # no misinterpretation is possible\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>is_active_user\u00a0= True<\/code> #\u00a0if this is not a property,\u00a0but\u00a0boolean\u00a0local data holder\u00a0<br \/><strong>Bad<\/strong>:\u00a0<code>active_user\u00a0= True<\/code> # like a variable with User instance\u00a0<br \/><strong>Good<\/strong>:\u00a0<code>user_is_active\u00a0= True<\/code>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>at_least_one_running_instance\u00a0= True<\/code>\u00a0<br \/><strong>Good<\/strong>:\u00a0<code>at_least_one_instance_is_running\u00a0= True<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Do not sacrifice meaning over beauty.<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<br \/><code>\/\/ note that all names are rather small\u00a0<br \/>class\u00a0Status(Enum):\u00a0<br \/>  SENT = 0\u00a0<br \/>\u00a0 ACCEPTED = 1\u00a0<br \/>\u00a0 REFUSED = 2\u00a0<br \/>\u00a0 STARTED = 3\u00a0\u00a0<br \/>\u00a0 COMPLETED = 4 \/\/ successfully?\u00a0<br \/>\u00a0 FAILED = 5\u00a0<br \/>\u00a0 IN_PROGRESS = 6\u00a0<br \/>\u00a0 ALIVE = 7<\/code>\u00a0<\/p>\n<p><strong>Better<\/strong>:\u00a0<br \/><code>class\u00a0Status(Enum):\u00a0<br \/>\u00a0\u00a0SENT = 0\u00a0<br \/>\u00a0\u00a0ACCEPTED = 1\u00a0<br \/>\u00a0\u00a0REFUSED = 2\u00a0<br \/>\u00a0\u00a0STARTED = 3\u00a0<br \/>\u00a0 SUCCESSFULLY_COMPLETED = 4\u00a0\/\/ long, but clear\u00a0<br \/>\u00a0\u00a0FAILED = 5\u00a0<br \/>\u00a0\u00a0IN_PROGRESS = 6\u00a0<br \/>\u00a0\u00a0ALIVE = 7<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Meaning should be\u00a0understood\u00a0without knowing\u00a0of\u00a0rest associated\u00a0names<\/strong>.\u00a0<strong>Classification should be nonoverlapping.\u00a0<\/strong>This is because such names will be broadly used in contexts alone, which will mislead readers about their meaning.\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<br \/><code>FINISHED = 5<\/code> \/\/ successfully? prematurely?\u00a0<br \/><code>FAILED = 6<\/code> \/\/ only this line helps to understand previous line meaning,\u00a0and\u00a0we are still not sure if something which is FINISHED can\u00a0also be\u00a0FAILED\u00a0<br \/><strong>Good<\/strong>:\u00a0<br \/><code>SUCCEEDED\u00a0= 5<\/code>\u00a0<br \/><code>FAILED = 6<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Singularity\u00a0and inconsistency\u00a0in d\u00e9cor and formatting attract attention and slows down reading.<\/strong>\u00a0Rules of formatting are created also for faster reading. If you don\u2019t have any special reason, do not break the formatting rules.\u00a0<\/p>\n<p><strong>Bad:\u00a0<\/strong>\/\/break a rule of one blank line between function bodies (if it is set) if the functions are similar\u00a0<\/p>\n<p><strong>Bad:<\/strong>\u00a0<br \/><code>void\u00a0StartUp();\u00a0<br \/>void PROCESS_DATA(); \/\/ why capitals?<\/code>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Avoid misleading terms.<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0\u00a0<code>QCheckBox*\u00a0check_geometry_<\/code>; \/\/\u00a0using \u201ccheck\u201d to show that it is a checkbox can mislead a reader that this means\u00a0\u201ctest geometry for errors\u201d, hower it just enables showing of it\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Do not mix more and less general terms as synonyms.<\/strong>\u00a0<\/p>\n<p><strong>Bad<\/strong>:\u00a0<code>DesignLoadStatus\u00a0error;<\/code> \/\/ status can be either error or success\u00a0or something else, in practice <em>error<\/em> variable may hold status of success<\/p>\n<\/li>\n<li>\n<p><strong>Name\u00a0associative containers such that one can understand their two-fold nature.\u00a0\u00a0<\/strong>In some conventions you may use \u201cto\u201d for that:\u00a0key_<em>to<\/em>_value\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Compound nouns are good if they are not ambiguous.<\/strong>\u00a0<\/p>\n<p><strong>Good?:<\/strong>\u00a0id_of_task_of_parent_stage\u00a0<br \/><strong>Good:\u00a0<\/strong>parent_stage_task_id<\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"final_remarks\" id=\"final_remarks\"><\/a><\/p>\n<h2>Final Remarks<\/h2>\n<p>All of the above is a try to formulate a consistent methodology of thinking while coding and designing. I hope this may also help development managers to facilitate interns&#8217; progress. Thank you for your time!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/567870\/\"> https:\/\/habr.com\/ru\/articles\/567870\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Summary\u00a0<\/h3>\n<p>We present\u00a0a guide\u00a0to\u00a0name\u00a0entities in code\u00a0based on\u00a0putting naming\u00a0in perspectives of\u00a0semantic\u00a0space, design,\u00a0and\u00a0readability.\u00a0The\u00a0main\u00a0idea is that naming\u00a0should not be considered as\u00a0creation of\u00a0tags, but as\u00a0a fundamental part\u00a0of design\u00a0process, which implies integral and consistent vocabulary to be used. We discuss naming process and naming formalism from these perspectives and we provide guides for practical use. The work is based on 15 years of experience in engineering work, coding and development management in high-tech industries.<\/p>\n<h3>Contents<\/h3>\n<p><a href=\"#names_in_engineering\" rel=\"noopener noreferrer nofollow\">Names in Software Engineering<\/a><br \/><a href=\"#principles_of_name_design\" rel=\"noopener noreferrer nofollow\">Principles of Name Design<\/a><br \/><a href=\"#bad_naming\" rel=\"noopener noreferrer nofollow\">Bad Naming<\/a><br \/><a href=\"#naming_process\" rel=\"noopener noreferrer nofollow\">Naming Process<\/a><br \/><a href=\"#fast_method\" rel=\"noopener noreferrer nofollow\">Fast Method<\/a><br \/><a href=\"#long_method\" rel=\"noopener noreferrer nofollow\">Long Method<\/a><br \/><a href=\"#formatting\" rel=\"noopener noreferrer nofollow\">Formatting<\/a><br \/><a href=\"#details_and_examples\" rel=\"noopener noreferrer nofollow\">Details &amp; Examples<\/a><br \/><a href=\"#final_remarks\" rel=\"noopener noreferrer nofollow\">Final Remarks<\/a><\/p>\n<p><a class=\"anchor\" name=\"names_in_engineering\" id=\"names_in_engineering\"><\/a><\/p>\n<h2>Names in Software Engineering<\/h2>\n<p>Let us consider what a programmer does. He models a situation in life to create a program\u00a0which adds some value to it.<\/p>\n<figure class=\"float full-width\"><figcaption><\/figcaption><\/figure>\n<p>While doing this he\u00a0extracts some things from the situation, conceptualizes them, and names\u00a0them. Like a table is a concept of a wooden construction we see, and we name it using a word \u201ctable\u201d.\u00a0Note, all three are different.\u00a0  <\/p>\n<figure class=\"float full-width\"><figcaption><\/figcaption><\/figure>\n<p>In all this work\u00a0a programmer\u00a0uses some language to describe the situation with a certain vocabulary.\u00a0This vocabulary defines and reflects meaning we put into our program, so it also defines if the program is modelling the situation well enough to add\u00a0some value to it.\u00a0Vocabulary in turn can be a source for building names of parts of\u00a0a\u00a0program.\u00a0<\/p>\n<p>We see three\u00a0dimensions meeting\u00a0here: world, thinking, artifact. These dimensions are pair-wise connected.\u00a0\u00a0<\/p>\n<p>We also see two levels of abstraction meeting\u00a0here: low-level, dealing with a part;\u00a0high-level, dealing with a whole. These are also cross-dependent.\u00a0<\/p>\n<p>This\u00a0can be visualized in\u00a0a\u00a0triangle model where entities are pair-wise connected.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Name is a reflection of meaning and design. When you name properly, you think properly, you design properly, and you use properly. Bad names lead to improper usage, bad design and bugs.\u00a0  <\/p>\n<p>Programming is also creation of texts.\u00a0Other programmers or the same programmer in future read the text and decode its concepts. Names are essential part of transferring proper meaning, so actions of future maintainers will not harm integrity of the program.\u00a0\u00a0  <\/p>\n<figure class=\"float full-width\"><figcaption><\/figcaption><\/figure>\n<p>This shows that naming\u00a0is\u00a0essential part the whole\u00a0process of\u00a0software engineering, so their design impact all the rest in\u00a0it.  <\/p>\n<h2>Principles of Name Design<\/h2>\n<p><a class=\"anchor\" name=\"principles_of_name_design\" id=\"principles_of_name_design\"><\/a><\/p>\n<p>Thus, design of names, design of vocabulary and design of a\u00a0model\u00a0should be aligned, so should the principles we use for it.\u00a0<\/p>\n<p>In\u00a0software engineering, as in any engineering discipline, in most cases\u00a0a model is mechanical,\u00a0i.e.\u00a0<\/p>\n<ul>\n<li>\n<p>a\u00a0system is a set of separate parts, each having its own concrete function.<\/p>\n<\/li>\n<\/ul>\n<p>Engineer needs to limit\u00a0entities he works with\u00a0simultaneously\u00a0because of limitation of brain, and he usually creates\u00a0generalizations and hierarchy\u00a0in a model, which implies:\u00a0<\/p>\n<ul>\n<li>\n<p>one entity\u00a0has\u00a0one name,\u00a0<\/p>\n<\/li>\n<li>\n<p>vocabulary\u00a0has hierarchical structure,\u00a0<\/p>\n<\/li>\n<li>\n<p>no overlaps\u00a0between names at the same hierarchy level.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Usually,\u00a0an engineer works with other engineers, so\u00a0\u00a0<\/p>\n<ul>\n<li>\n<p>the vocabulary should be shared between all the engineers.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>Try to think of these general principles of software design in application to naming:\u00a0<\/p>\n<ul>\n<li>\n<p>consistency between parts\u00a0of an integral system;\u00a0<\/p>\n<\/li>\n<li>\n<p>proper level of specification\/generalization;\u00a0<\/p>\n<\/li>\n<li>\n<p>proper level of modularity\/coupling;\u00a0<\/p>\n<\/li>\n<li>\n<p>maintainability and cognitive-friendly interface;\u00a0<\/p>\n<\/li>\n<li>\n<p>future-awareness \u2013 need to design taking evolution of system into account;\u00a0<\/p>\n<\/li>\n<li>\n<p>support of various scenarios and corner cases.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>As program is a text, we need to make names readable as easy in a normal text to improve understanding of it while reading. It\u00a0means we need to prefer English over formal language, and full words over excessive shortening.\u00a0  <\/p>\n<p><a class=\"anchor\" name=\"bad_naming\" id=\"bad_naming\"><\/a><\/p>\n<h2>Bad Naming<\/h2>\n<p>As it is usually not that easy to\u00a0fix\u00a0the architecture, the same holds for the vocabulary.\u00a0Terms you use to describe your system\u00a0cannot be fixed by simple\u00a0Find\/Replace in most cases.\u00a0They\u00a0impact\u00a0a\u00a0maintainer, and\u00a0lead to wrong\u00a0derivative\u00a0changes\u00a0in other names and in structure.\u00a0\u00a0<\/p>\n<p>Names can be bad if they\u00a0mislead,\u00a0can mislead,\u00a0are ambiguous, are too specific or too general,\u00a0require time to understand,\u00a0are\u00a0inconsistent with context\u00a0or\u00a0other names\u00a0or a convention,\u00a0describe too little or too much.\u00a0<\/p>\n<p>Names can be bad because of bad design, for example if the same entity\u00a0has different meaning\u00a0in different contexts.\u00a0In this case to fix name you need to redesign the entity.\u00a0<\/p>\n<p>Bad names in small pieces of code are\u00a0not\u00a0an exclusion.\u00a0It is better to speak the same language in all parts of an integral space of your program.\u00a0\u00a0<\/p>\n<p><a class=\"anchor\" name=\"naming_process\" id=\"naming_process\"><\/a><\/p>\n<h2>Naming Process<\/h2>\n<p>Naming\u00a0can be approached as a\u00a0process of finding meaning of entities\u00a0and expressing it\u00a0clearly\u00a0within a set of other names.\u00a0\u00a0<\/p>\n<p>Since you spend time on design in general, you need to make time\u00a0for\u00a0name design. Reluctance of doing the latter is inconsistency which has no excuse, because it harms design and\u00a0semantic space.\u00a0<\/p>\n<p>Engineering work contains of iterations.\u00a0The\u00a0knowledge of what you work with and what is to be done increase gradually:\u00a0<\/p>\n<figure class=\"float full-width\"><figcaption><\/figcaption><\/figure>\n<p>Speculative (mental) experiments\u00a0are naturally more\u00a0used for naming\u00a0since they are faster and since they precede\u00a0more\u00a0expensive\u00a0physical experimentation.\u00a0Also, impact\u00a0of names\u00a0is long-term and not always easily physically tested\u00a0right away.<\/p>\n<p>Names are derivatives from terms you have in a vocabulary you\u00a0use to describe\u00a0your\u00a0model.\u00a0So,\u00a0naming process starts with understanding of what terms are more appropriate\u00a0for\u00a0your\u00a0model to reflect a situation in life you work with.\u00a0\u00a0<\/p>\n<p>Terms are more heavyweight than names, so more time should be spent\u00a0on\u00a0them.\u00a0It is the time\u00a0when\u00a0you\u00a0create\u00a0a\u00a0value of\u00a0an\u00a0insight about\u00a0a\u00a0problem and a solution.\u00a0\u00a0<\/p>\n<p>Terms also define\u00a0ability of a solution to evolve when a situation in life evolves.\u00a0Fitting of key things of a situation with the terms\u00a0you use in your solution lead to\u00a0a robustness of a solution.\u00a0\u00a0<\/p>\n<p>Concrete names should be built\u00a0of terms\u00a0in\u00a0a\u00a0more formal way,\u00a0using\u00a0conventions.\u00a0<\/p>\n<p><a class=\"anchor\" name=\"fast_method\" id=\"fast_method\"><\/a><\/p>\n<h2>Fast Method<\/h2>\n<p>Fast\u00a0method is for usage while writing a draft or a small part in an already established code.\u00a0<\/p>\n<ol>\n<li>\n<p><strong>Describe\u00a0an entity \u201cusing your words\u201d<\/strong>\u00a0<br \/>What is your intention? How would you use it?\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Use terms from the context\/existing vocabulary to compile a derivative name<\/strong>\u00a0<br \/>Do not create synonyms and do not use different wording for the same thing.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check if the entity can be\u00a0properly understood from the same line it will be used in<br \/><\/strong>Do not\u00a0imply that the reader has go to the definition\/somewhere else to understand it correctly.\u00a0Switching between contexts is for finding details, not for finding the idea. Switching to another context make you lose the original one.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Ask yourself: can others misinterpret the name?<br \/><\/strong>Even if the misinterpretation will happen in 5% of cases it may lead to hours of debugging and worsen maintainability.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check readability,\u00a0prefer English over formal.<\/strong>\u00a0<br \/>A text that can be easily read is easier to understand.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"long_method\" id=\"long_method\"><\/a><\/p>\n<h2>Long Method<\/h2>\n<p>Hard cases usually mean there are flaws in the design and the understanding of how the system (should) work. Instead of thinking \u201clet us leave it as is and do some real work\u201d, spending extra time in this case is a natural need.\u00a0<\/p>\n<ol>\n<li>\n<p><strong>Create\u00a0a long\u00a0description\u00a0of an entity\u00a0\u201cusing your words\u201d<\/strong>\u00a0<br \/>What is your intention? How would you use it?\u00a0\u00a0<br \/>Write\u00a0the long description down, look at it.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Make the description\u00a0precise, i.e. create a definition<\/strong>\u00a0<br \/>What is the entity\u2019s place and\u00a0scope in the\u00a0system? How is it different from\u00a0the\u00a0other entities you have in the same system and from\u00a0the\u00a0entities in use context of the system (if it is visible outside)? Do you put more specifics than needed? Do you generalize what you should not?\u00a0Usually you increase level of abstraction vs. the values of the entity.\u00a0<br \/>Write\u00a0precise description down, look at it.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Create\u00a0variants\u00a0of long names<\/strong>\u00a0<br \/>Try to put the essence of the\u00a0definition\u00a0in few words. Use English, not formal language here.\u00a0Create multiple variants.\u00a0<br \/>Write\u00a0the list down, look at it.\u00a0<br \/>Put names in use context in code, look\u00a0at\u00a0the code.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Check vocabulary\u00a0<\/strong>\u00a0<br \/>Check consistency with other parts of your code.\u00a0Do you create a synonym?\u00a0Do you use terms which may\u00a0not be understood by\u00a0a newcomer who will read your code 10 years later? Is it easy to read? Can it mislead someone even if it is formally perfect? How much time is required to figure out the correct meaning of the variable? A user should be able to read your code as fast as he reads\u00a0a normal text.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Consider context to shorten names<\/strong>\u00a0<br \/>What will be understood from the usage, so can be omitted? What\u00a0can be omitted without\u00a0losing\u00a0possibility to distinguish\u00a0the entity\u00a0from other entities?\u00a0\u00a0<br \/>Several words are OK if needed,\u00a0if you move 30 lines into a new function, it is OK to spend few more words to describe it in\u00a0a\u00a0name.\u00a0<br \/>Put\u00a0shorter\u00a0names in use context in code, look\u00a0at\u00a0the code.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Make a survey to decide\u00a0on\u00a0a winner<\/strong>\u00a0<br \/>The survey can be\u00a0a\u00a0mental experiment\u00a0of physical. For\u00a0the\u00a0mental experiment\u00a0you need to\u00a0compare\u00a0the names as\u00a0other\u00a0code users.\u00a0Both ways have limitations.\u00a0If you will ask\u00a0people,\u00a0they will be unable to identify\u00a0in a short time\u00a0if what you suggest is aligned with your design and purpose.\u00a0Mental experimentation\u00a0requires a skill and usually is very biased by your own opinion.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Sometimes naming analysis leads to redesign,\u00a0do\u00a0it<\/strong>\u00a0<br \/>This is a natural consequence of you getting more knowledge of the system you work with, while finding meaning of entities.\u00a0\u00a0<br \/>Single entity with multiple responsibilities often\u00a0makes it uneasy to name it and maintain it. It might happen if your entity is not logically integral. You may split it into parts in this case.\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Create appropriate comment about complex entities\u00a0<br \/><\/strong>It may happen, that a name cannot hold everything you want to share with a reader. Add a comment\u00a0with\u00a0full description.\u00a0\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>Non-descriptive names should be rare negotiated conventions<br \/><\/strong>You may agree with your colleagues that you will use the name for this entity even if it cannot be understood straight without additional knowledge. For example, abbreviations go here.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"formatting\" id=\"formatting\"><\/a><\/p>\n<h2>Formatting<\/h2>\n<p>As\u00a0code is a text\u00a0which\u00a0is read by people, we may reuse principles of creation of <\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-398459","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398459"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398459\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}