How can you find out in which module your code runs?
As Clive mentioned, a constant is your best bet. When I start up a new project, typically one of the first things I do is create an interface that contains constants that are applicable to the entire project. Usually the very first constant is for the module's machine name. I then use these constants, and not strings, throughout the project.

Here's a simplified and obfuscated version from one of my projects: (this file would be located in the folder my_module/src/MyModuleInterface.php)

&lt;?php

namespace Drupal\my_module;

interface MyModuleInterface {

  const MODULE_NAME = 'my_module';
  const SERVICE = 'my_module.foobarbaz';
  const TAX_VOCAB_FOO = 'foo';
  const TAX_VOCAB_BAR = 'bar';
  const TAX_VOCAB_BAZ = 'baz';

  const PERM_ADMIN_FOO = 'administer foo';


  /**
   * Returns TRUE if the specified module is enabled.
   *
   * @param string $module_name
   *  The machine name of the module to check as enabled. Defaults to this
   *  module's machine name.
   *
   * @return boolean
   *  TRUE if the specified module is enabled.
   */
  function isModuleEnabled($module_name = self::MODULE_NAME);
}


The benefits to this approach are:


Consistency through your project as well as for any other projects
that might want to call on your module; they can also utilize the
constant.
Reduction of errors due to typos.
If you use an IDE such as PHPStorm, you can take advantage of auto-completion, which is convenient and speeds code writing.