> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codeant.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Go

<AccordionGroup>
  <Accordion title="Type names should comply with a naming convention">
    <div class="paragraph">
      <p>Shared naming conventions allow teams to collaborate efficiently.</p>
    </div>

    <div class="paragraph">
      <p>This rule raises an issue when a struct name does not match a provided regular expression.</p>
    </div>

    <div class="paragraph">
      <p>The convention in Go is to use mixedCaps rather than underscores. See <a href="https://golang.org/doc/effective_go.html#names">Go documentation</a> for the complete naming conventions.</p>
    </div>

    <div class="paragraph">
      <p>Note that the casing of the first character determines if the type is exported or not.</p>
    </div>

    <div class="paragraph">
      <p>For example, with the default provided regular expression <code>^\[a-zA-Z]\[a-zA-Z0-9]\*\$</code>, the struct:</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      type my_struct struct {...}
      ```

      ```go Fix theme={null}
      type myStruct struct {...}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Collection sizes and array length comparisons should make sense">
    <div class="paragraph">
      <p>The size of a collection and the length of an array are always greater than or equal to zero. Testing it doesn’t make sense, since the result is always <code>true</code>.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if len(myArr) >= 0 { ... } // Noncompliant: always true

      var result = len(myArr) >= 0 // Noncompliant: always true
      ```

      ```go Fix theme={null}
      if len(myArr) < 0 { ... } // Noncompliant: always false
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Function names should comply with a naming convention">
    <div class="paragraph">
      <p>For example, with the default provided regular expression <code>^(\_|\[a-zA-Z0-9]+)\$</code>, the function:</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func execute_all() {
      ...
      }
      ```

      ```go Fix theme={null}
      func executeAll() {
      ...
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Unary prefix operators should not be repeated">
    <div class="paragraph">
      <p>The repetition of a unary operator is usually a typo. The second operator invalidates the first one in most cases:</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      var a int = 1
      var flag bool = true

      var a1 int = ^^^a // Noncompliant: equivalent to "^a"
      var flag2 bool  = !!!flag  // Noncompliant: equivalent to "!flag"
      ```

      ```go Fix theme={null}
      var i int = 1
      var j int = ++ ++i  // Noncompliant
      var k int = -- --i  // Noncompliant
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="for loop increment clauses should modify variables from loop conditions">
    <div class="paragraph">
      <p>It can be extremely confusing when a <code>for condition tests a variable which is not updated inside the for</code> post statement.</p>
    </div>

    <div class="paragraph" />

    <CodeGroup>
      ```go Bad theme={null}
      for i := 1; i <= 10; j++ { // Noncompliant
      // ...
      }
      ```

      ```go Fix theme={null}
      for i := 1; i <= 10; i++ {
      // ...
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Statements should be on separate lines">
    <div class="paragraph">
      <p>Putting multiple statements on a single line lowers the code readability and makes debugging the code more complex.</p>
    </div>

    <div class="paragraph">
      <p>Unresolved directive in \<stdin> - include::\{noncompliant}\[]</p>
    </div>

    <div class="paragraph">
      <p>Write one statement per line to improve readability.</p>
    </div>

    <div class="paragraph">
      <p>Unresolved directive in \<stdin> - include::\{compliant}\[]</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if condition { doSomething() } // Compliant by exception
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Expressions should not be too complex">
    <div class="paragraph">
      <p>The complexity of an expression is defined by the number of <code>&&, || and condition ? ifTrue : ifFalse</code> operators it contains.</p>
    </div>

    <div class="paragraph">
      <p>A single expression’s complexity should not become too high to keep the code readable.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if (((condition1 && condition2) || (condition3 && condition4)) && condition5) { ... }
      ```

      ```go Fix theme={null}
      if ( (myFirstCondition() || mySecondCondition()) && myLastCondition()) { ... }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Map values should not be replaced unconditionally">
    <CodeGroup>
      ```go Bad theme={null}
      var letters = make(map[string]string)

      letters["a"] = "Apple"
      letters["a"] = "Boy" // Noncompliant

      var towns = make(map[int]string)

      towns[i] = "London"
      towns[i] = "Chicago" // Noncompliant
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Loops with at most one iteration should be refactored">
    <div class="paragraph">
      <p>A loop with at most one iteration is equivalent to the use of an \`if statement to conditionally execute one piece of code. No developer expects to find such a use of a loop statement. If the initial intention of the author was really to conditionally execute one piece of code, an if statement should be used instead.</p>
    </div>

    <div class="paragraph">
      <p>At worst that was not the initial intention of the author and so the body of the loop should be fixed to use the nested return, break or throw\` statements in a more appropriate way.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      for i := 0; i < 10; i++ { // noncompliant, loop only executes once
      fmt.Println(i)
      break
      }
      ```

      ```go Fix theme={null}
      for i := 0; i < 10; i++ {
      fmt.Println(i)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Delivering code in production with debug features activated is security-sensitive">
    <div class="paragraph">
      <p>Development tools and frameworks usually have options to make debugging easier for developers. Although these features are useful during development, they should never be enabled for applications deployed in production. Debug instructions or error messages can leak detailed information about the system, like the application’s path or file names.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      import "runtime/debug"

      _, err := funcThatFails()
      if err != nil {
      fmt.Printf("Error calling funcThatFails: %v\n", err)
      debug.PrintStack() // Sensitive
      return
      }
      ```

      ```go Fix theme={null}
      func main() {
      pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) // Sensitive
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Boolean literals should not be redundant">
    <div class="paragraph">
      <p>A boolean literal can be represented in two different ways: \{true} or \{false}.
      They can be combined with logical operators (\{ops}) to produce logical expressions that represent truth values.
      However, comparing a boolean literal to a variable or expression that evaluates to a boolean value is unnecessary and can make the code harder to read and understand.
      The more complex a boolean expression is, the harder it will be for developers to understand its meaning and expected behavior, and it will favour the introduction of new bugs.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if boolFunc() || false { 
      // ...
      }

      flag := x && true
      ```

      ```go Fix theme={null}
      if boolFunc() {
      // ...
      }

      flag := x
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Octal values should not be used">
    <div class="paragraph">
      <p>Integer literals starting with a zero are octal rather than decimal values. While using octal values is fully supported, most developers do not have experience with them. They may not recognize octal values as such, mistaking them instead for decimal values.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func printTen() {                                                                                                                                
      myNumber := 010 // Noncompliant. myNumber will hold 8, not 10 - was this really expected?
      fmt.Println(myNumber)
      }
      ```

      ```go Fix theme={null}
      func printTen() {
      myNumber := 10
      fmt.Println(myNumber)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Using pseudorandom number generators (PRNGs) is security-sensitive">
    <div class="paragraph">
      <p>Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p><a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386">CVE-2013-6386</a></p>
        </li>

        <li>
          <p><a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419">CVE-2006-3419</a></p>
        </li>

        <li>
          <p><a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102">CVE-2008-4102</a></p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      import "math/rand"

      a := make([]byte, 10)
      rand.Read(a) // Sensitive
      ```

      ```go Fix theme={null}
      import "math/rand"

      num := rand.Intn(100) // Sensitive
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Mergeable if statements should be combined">
    <div class="paragraph">
      <p>Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as possible, by avoiding unnecessary nesting, is considered a good practice.</p>
    </div>

    <div class="paragraph">
      <p>Merging if statements when possible will decrease the nesting of the code and improve its readability.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if condition1 {
      if condition2 {            // Noncompliant
      	fmt.Println("Hello World")
      }
      }
      ```

      ```go Fix theme={null}
      if condition1 && condition2 {  // Compliant
      fmt.Println("Hello World")
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Functions should not be empty">
    <div class="paragraph">
      <p>An empty \{operationName} is generally considered bad practice and can lead to confusion, readability, and maintenance issues.
      Empty \{operationName}s bring no functionality and are misleading to others as they might think the \{operationName} implementation fulfills a specific and identified requirement.</p>
    </div>

    <div class="paragraph">
      <p>There are several reasons for a \{operationName} not to have a body:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>It is an unintentional omission, and should be fixed to prevent an unexpected behavior in production.</p>
        </li>

        <li>
          <p>It is not yet, or never will be, supported. In this case an exception should be thrown.</p>
        </li>

        <li>
          <p>The method is an intentionally-blank override. In this case a nested comment should explain the reason for the blank override.</p>
        </li>
      </ul>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func shouldNotBeEmpty() {  // Noncompliant - method is empty
      }

      func notImplemented() {  // Noncompliant - method is empty
      }

      func emptyOnPurpose() {  // Noncompliant - method is empty
      }
      ```

      ```go Fix theme={null}
      func shouldNotBeEmpty() {
      doSomething();
      }

      func notImplemented() {
      return "", errors.New("notImplemented() cannot be performed because ...")
      }

      func emptyOnPurpose() {
      // comment explaining why the method is empty
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Using clear-text protocols is security-sensitive">
    <div class="paragraph">
      <p>Clear-text protocols such as \`ftp, telnet, or http lack
      encryption of transported data, as well as the capability to build an
      authenticated connection. It means that an attacker able to sniff traffic from
      the network can read, modify, or corrupt the transported content. These
      protocols are not secure as they expose applications to an extensive range of
      risks:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>sensitive data exposure</p>
        </li>

        <li>
          <p>traffic redirected  to a malicious endpoint</p>
        </li>

        <li>
          <p>malware-infected software update or installer</p>
        </li>

        <li>
          <p>execution of client-side code</p>
        </li>

        <li>
          <p>corruption of critical information</p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>Even in the context of isolated networks like offline environments or segmented
      cloud environments, the insider threat exists. Thus, attacks involving
      communications being sniffed or tampered with can still happen.</p>
    </div>

    <div class="paragraph">
      <p>For example, attackers could successfully compromise prior security layers by:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>bypassing isolation mechanisms</p>
        </li>

        <li>
          <p>compromising a component of the network</p>
        </li>

        <li>
          <p>getting the credentials of an internal IAM account (either from a service
          account or an actual person)</p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>In such cases, encrypting communications would decrease the chances of attackers
      to successfully leak data or steal credentials from other network components.
      By layering various security practices (segmentation and encryption, for
      example), the application will follow the <em>defense-in-depth</em> principle.</p>
    </div>

    <div class="paragraph">
      <p>Note that using the http\` protocol is being deprecated by
      <a href="https://blog.mozilla.org/security/2015/04/30/deprecating-non-secure-http">major web browsers</a>.</p>
    </div>

    <div class="paragraph">
      <p>In the past, it has led to the following vulnerabilities:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p><a href="https://nvd.nist.gov/vuln/detail/CVE-2019-6169">CVE-2019-6169</a></p>
        </li>

        <li>
          <p><a href="https://nvd.nist.gov/vuln/detail/CVE-2019-12327">CVE-2019-12327</a></p>
        </li>

        <li>
          <p><a href="https://nvd.nist.gov/vuln/detail/CVE-2019-11065">CVE-2019-11065</a></p>
        </li>
      </ul>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      import "net/http"

      response, err := http.Get("http://www.example.com/") // Sensitive
      ```

      ```go Fix theme={null}
      import "net/smtp"

      connection, err := smtp.Dial("mail.example.com:25") // Sensitive
      connection.Hello("my-sending-server.example.com")
      // authenticate and send email
      connection.Quit()
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Redundant pairs of parentheses should be removed">
    <div class="paragraph">
      <p>The use of parentheses, even those not required to enforce a desired order of operations, can clarify the intent behind a piece of code. However, redundant pairs of parentheses could be misleading and should be removed.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func foo(a bool, y int) int {
      x := ((y / 2 + 1)) // Noncompliant

      if a && ((x+y > 0)) {  // Noncompliant
      return ((x + 1))  // Noncompliant
      }
      }
      ```

      ```go Fix theme={null}
      func foo(a bool, y int) int {
      x := (y / 2 + 1)

      if a && (x+y > 0) {
      return (x + 1)
      }
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Identical expressions should not be used on both sides of a binary operator">
    <div class="paragraph">
      <p>Using the same value on both sides of a binary operator is a code defect. In the case of logical operators, it is either a copy/paste error and, therefore, a bug, or it is simply duplicated code and should be simplified. In the case of bitwise operators and most binary mathematical operators, having the same value on both sides of an operator yields predictable results and should be simplified as well.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func main() {
      v1 := (true && false) && (true && false) // Noncompliant
      }
      ```

      ```go Fix theme={null}
      func main() {
      v1 := (true && false) // Compliant
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Related if/else if statements should not have the same condition">
    <div class="paragraph">
      <p>A chain of if/else if statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to true.</p>
    </div>

    <div class="paragraph">
      <p>Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it’s simply dead code and at worst, it’s a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func example(condition1, condition2 bool) {
      if condition1 {
      } else if condition1 { // Noncompliant
      }
      }
      ```

      ```go Fix theme={null}
      func SwitchWithMultipleConditions(param int) {
      switch param {
      case 1, 2, 3:
      fmt.Println(">1")
      case 3, 4, 5: // Noncompliant; 3 is duplicated
      fmt.Println("<1")
      }
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Formatting SQL queries is security-sensitive">
    <div class="paragraph">
      <p>Formatted SQL queries can be difficult to maintain, debug and can increase the risk of SQL injection when concatenating untrusted values into the query. However, this rule doesn’t detect SQL injections (unlike rule S3649), the goal is only to highlight complex/formatted queries.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func getName(db *sql.DB, id string) (string, error) {
      var name string
      row := db.QueryRow("SELECT name FROM users WHERE id = " + id) // Sensitive

      if err := row.Scan(&name); err != nil {
          if err == sql.ErrNoRows {
              return name, fmt.Errorf("No name found for id %s", id)
          }
      }

      return name, nil
      }
      ```

      ```go Fix theme={null}
      func getName(db *sql.DB, id string) (string, error) {
      var name string
      row := db.QueryRow("SELECT name FROM users WHERE id = ?", id)

      if err := row.Scan(&name); err != nil {
          if err == sql.ErrNoRows {
              return name, fmt.Errorf("No name found for id %s", id)
          }
      }

      return name, nil
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Unused function parameters should be removed">
    <div class="paragraph">
      <p>A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body.
      While this might seem harmless at first glance, it can lead to confusion and potential errors in your code.
      Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore.
      Therefore, removing function parameters that are not being utilized is considered best practice.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func compute(start int) { // Noncompliant; start is not used
      sum := 0
      for i := 0; i < 10; i++ {
      	sum += i
      }
      fmt.Println("Result:", sum)
      }
      ```

      ```go Fix theme={null}
      func compute() {
      sum := 0
      for i := 0; i < 10; i++ {
      	sum += i
      }
      fmt.Println("Result:", sum)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="All code should be reachable">
    <div class="paragraph">
      <p>Some statements (<code>return, break, continue, goto, switch) and throw</code> expressions move control flow out of the current code block. So any unlabeled statements that come after such a jump are unreachable, and either this dead code should be removed, or the logic should be corrected.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func add(x, y int) int {                   
      return x + y // Noncompliant
      z := x + y // dead code
      }
      ```

      ```go Fix theme={null}
      func add(x, y int) int {
      return x + y // Compliant
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="URIs should not be hardcoded">
    <div class="paragraph">
      <p>Hard-coding a URI makes it difficult to test a program for a variety of reasons:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>path literals are not always portable across operating systems</p>
        </li>

        <li>
          <p>a given absolute path may not exist in a specific test environment</p>
        </li>

        <li>
          <p>a specified Internet URL may not be available when executing the tests</p>
        </li>

        <li>
          <p>production environment filesystems usually differ from the development environment</p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>In addition, hard-coded URIs can contain sensitive information, like IP addresses, and they should not be stored in the code.</p>
    </div>

    <div class="paragraph">
      <p>For all those reasons, a URI should never be hard coded. Instead, it should be replaced by a customizable parameter.</p>
    </div>

    <div class="paragraph">
      <p>Further, even if the elements of a URI are obtained dynamically, portability can still be limited if the path delimiters are hard-coded.</p>
    </div>

    <div class="paragraph">
      <p>This rule raises an issue when URIs or path delimiters are hard-coded.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      file, err := os.Open("accounts.txt") // Noncompliant
      if err != nil {
      log.Fatal(err)
      }

      bs, err := ioutil.ReadFile("accounts.txt") // Noncompliant
      if err != nil {
      return
      }
      ```

      ```go Fix theme={null}
      var location string = prop.Read("myApplication.mySpecificFile")
      file, err := os.Open(location)
      if err != nil {
      log.Fatal(err)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Variables should not be self-assigned">
    <div class="paragraph">
      <p>There is no reason to re-assign a variable to itself. Either this statement is redundant and should be removed, or the re-assignment is a mistake and some other value or variable was intended for the assignment instead.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func (user *User) rename(name string) {
      name = name  // Noncompliant
      }
      ```

      ```go Fix theme={null}
      func (user *User) rename(name string) {
      user.name = name
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="if ... else if constructs should end with else clauses">
    <div class="paragraph">
      <p>This rule applies whenever an \`if statement is followed by one or more else if statements; the final else if should be followed by an else statement.</p>
    </div>

    <div class="paragraph">
      <p>The requirement for a final else statement is defensive programming.</p>
    </div>

    <div class="paragraph">
      <p>The else statement should either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final default clause in a switch\` statement.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if x == 0 {                                        
      doSomething()
      } else if x == 1 {
      doSomethingElse()
      }
      ```

      ```go Fix theme={null}
      if x == 0 {
      doSomething()
      } else if x == 1 {
      doSomethingElse()
      } else {
      return errors.New("unsupported int")
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Control flow statements if, for and switch should not be nested too deeply">
    <div class="paragraph">
      <p>Nested control flow statements such as <code>if, for, while, switch, and try</code> are often key ingredients in creating
      what’s known as "Spaghetti code". This code smell can make your program difficult to understand and maintain.</p>
    </div>

    <div class="paragraph">
      <p>When numerous control structures are placed inside one another, the code becomes a tangled, complex web.
      This significantly reduces the code’s readability and maintainability, and it also complicates the testing process.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if condition1 { // Compliant - depth = 1
      /* ... */
      if condition2 { // Compliant - depth = 2
      	/* ... */
      	for i := 1; i <= 10; i++ { // Compliant - depth = 3, not exceeding the limit
      		/* ... */
      		if condition4 { // Noncompliant - depth = 4
      			if condition5 { // Depth = 5, exceeding the limit, but issues are only reported on depth = 4
      				/* ... */
      			}
      			return
      		}
      	}
      }
      }
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="String literals should not be duplicated">
    <div class="paragraph">
      <p>Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func run() {                                                                                      
      prepare("This should be a constant")  // Noncompliant; 'This should ...' is duplicated 3 times
      execute("This should be a constant")
      release("This should be a constant")
      }
      ```

      ```go Fix theme={null}
      const ACTION = "This should be a constant"

      func run() {
      prepare(ACTION)
      execute(ACTION)
      release(ACTION)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="switch case clauses should not have too many lines">
    <div class="paragraph">
      <p>The <code>switch statement should be used only to clearly define some new branches in the control flow. As soon as a case clause contains too many statements this highly decreases the readability of the overall control flow statement. In such case, the content of the case</code> clause should be extracted into a dedicated method.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func foo(tag int) {
      switch tag {
      case 0:
      	methodCall1()
      	methodCall2()
      	methodCall3()
      	methodCall4()
                  methodCall5()
                  methodCall6()
      case 1:
      	bar()
      }
      }
      ```

      ```go Fix theme={null}
      func foo(tag int) {
      switch tag {
      case 0:
      	executeAll()
      case 1:
      	bar()
      }
      }

      func executeAll() {
      methodCall1()
      methodCall2()
      methodCall3()
      methodCall4()
          methodCall5()
          methodCall6()
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Functions should not have identical implementations">
    <div class="paragraph">
      <p>Two \{func\_name}s having the same implementation are suspicious.
      It might be that something else was intended. Or the duplication is intentional, which becomes a maintenance burden.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func fun1() (x, y int) {
      a, b := 1, 2
      b, a = a, b
      return a, b
      }

      func fun2() (x, y int) {  // Noncompliant; duplicates fun1
      a, b := 1, 2
      b, a = a, b
      return a, b
      }
      ```

      ```go Fix theme={null}
      func fun1() (x, y int) {
      a, b := 1, 2
      b, a = a, b
      return a, b
      }

      func fun2() (x, y int) {  // Intent is clear
      return fun1()
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Using hardcoded IP addresses is security-sensitive">
    <div class="paragraph">
      <p>Hardcoding IP addresses is security-sensitive. It has led in the past to the following vulnerabilities:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p><a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-5901">CVE-2006-5901</a></p>
        </li>

        <li>
          <p><a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2005-3725">CVE-2005-3725</a></p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>Today’s services have an ever-changing architecture due to their scaling and redundancy needs. It is a mistake to think that a service will always have the same IP address. When it does change, the hardcoded IP will have to be modified too. This will have an impact on the product development, delivery, and deployment:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>The developers will have to do a rapid fix every time this happens, instead of having an operation team change a configuration file.</p>
        </li>

        <li>
          <p>It misleads to use the same address in every environment (dev, sys, qa, prod).</p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>Last but not least it has an effect on application security. Attackers might be able to decompile the code and thereby discover a potentially sensitive address. They can perform a Denial of Service attack on the service, try to get access to the system, or try to spoof the IP address to bypass security checks. Such attacks can always be possible, but in the case of a hardcoded IP address solving the issue will take more time, which will increase an attack’s impact.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      config, err := ReadConfig("properties.ini")

      ip := config["ip"]
      port := config["ip"]

      SocketClient(ip, port)
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Track uses of TODO tags">
    <div class="paragraph">
      <p>Developers often use TODO tags to mark areas in the code where additional work or improvements are needed but are not implemented immediately.
      However, these TODO tags sometimes get overlooked or forgotten, leading to incomplete or unfinished code.
      This rule aims to identify and address unattended TODO tags to ensure a clean and maintainable codebase.
      This description explores why this is a problem and how it can be fixed to improve the overall code quality.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func foo() {
      // TODO
      }
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="All branches in a conditional structure should not have exactly the same implementation">
    <div class="paragraph">
      <p>Having all branches of a switch or if chain with the same implementation indicates a problem.</p>
    </div>

    <div class="paragraph">
      <p>In the following code:</p>
    </div>

    <div class="paragraph">
      <p>Unresolved directive in \<stdin> - include::\{example}\[]</p>
    </div>

    <div class="paragraph">
      <p>Either there is a copy-paste error that needs fixing or an unnecessary switch or if chain that should be removed.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if b == 0 {    //no issue, this could have been done on purpose to make the code more readable
      doSomething()
      } else if b == 1 {
      doSomething()
      }
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Setting loose POSIX file permissions is security-sensitive">
    <div class="paragraph">
      <p>In Unix file system permissions, the "`others`" category refers to all
      users except the owner of the file system resource and the members of the group
      assigned to this resource.</p>
    </div>

    <div class="paragraph">
      <p>Granting permissions to this category can lead to unintended access to files or
      directories that could allow attackers to obtain sensitive information, disrupt
      services or elevate privileges.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      import (
      "os"
      )

      func main() {
      err := os.Chmod("/tmp/fs", 0777) // Sensitive
      if err != nil {
          panic(err)
      }
      }
      ```

      ```go Fix theme={null}
      import (
      "golang.org/x/sys/unix"
      )

      func main() {
      oldMask := unix.Umask(0) // Sensitive
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Useless if(true) {...} and if(false){...} blocks should be removed">
    <div class="paragraph">
      <p>\`if statements with conditions that are always false have the effect of making blocks of code non-functional. if statements with conditions that are always true are completely redundant, and make the code less readable.</p>
    </div>

    <div class="paragraph">
      <p>There are three possible causes for the presence of such code:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p>An if statement was changed during debugging and that debug code has been committed.</p>
        </li>

        <li>
          <p>Some value was left unset.</p>
        </li>

        <li>
          <p>Some logic is not doing what the programmer thought it did.</p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>In any of these cases, unconditional if\` statements should be removed.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      if true {
      doSomething()
      }

      if false {
      doSomething()
      }
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Empty statements should be removed">
    <div class="paragraph">
      <p>Empty statements represented by a semicolon ; are statements that do not perform any operation. They are often the result of a typo or a misunderstanding of the language syntax.
      It is a good practice to remove empty statements since they don’t add value and lead to confusion and errors.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func doSomething() {
      ;                                                       // Noncompliant
      }

      func doSomethingElse() {
      fmt.Println("doSomethingElse");;     // Noncompliant - double useless ;
      ...
      }
      ```

      ```go Fix theme={null}
      func doSomething() {
      }

      func doSomethingElse() {
      fmt.Println("doSomethingElse")
      ...
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Using weak hashing algorithms is security-sensitive">
    <div class="paragraph">
      <p>Cryptographic hash algorithms such as <code>MD2, MD4, MD5, MD6, HAVAL-128, HMAC-MD5, DSA (which uses SHA-1), RIPEMD, RIPEMD-128, RIPEMD-160, HMACRIPEMD160 and SHA-1 are no longer considered secure, because it is possible to have collisions</code> (little computational effort is enough to find two or more different inputs that produce the same hash).</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      import (
      "crypto"
      "fmt"
      )

      func calculateHash(data []byte) string {
      hashInstance := crypto.Hash.New(crypto.MD5) // Sensitive
      hashInstance.Write(data)
      hash := hashInstance.Sum(nil)
      return fmt.Sprintf("%x", hash)
      }
      ```

      ```go Fix theme={null}
      import (
      "crypto/sha1"
      "fmt"
      )

      func calculateHash(data []byte) string {
      hash := sha1.Sum(data) // Sensitive
      return fmt.Sprintf("%x", hash)
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="switch statements should have default clauses">
    <div class="paragraph">
      <p>The requirement for a final <code>default</code> clause is defensive programming. The clause should either take appropriate action, or contain a suitable comment as to why no action is taken.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      switch tag { // Noncompliant - default case is missing
      case 0, 1, 2, 3:
      foo()
      case 4, 5, 6, 7:
      bar()
      }
      ```

      ```go Fix theme={null}
      switch tag {
      case 0, 1, 2, 3:
      foo()
      case 4, 5, 6, 7:
      bar()
      default:
      qix()
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Hard-coded credentials are security-sensitive">
    <div class="paragraph">
      <p>Because it is easy to extract strings from an application source code or binary, credentials should not be hard-coded. This is particularly true for applications that are distributed or that are open-source.</p>
    </div>

    <div class="paragraph">
      <p>In the past, it has led to the following vulnerabilities:</p>
    </div>

    <div class="ulist">
      <ul>
        <li>
          <p><a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-13466">CVE-2019-13466</a></p>
        </li>

        <li>
          <p><a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-15389">CVE-2018-15389</a></p>
        </li>
      </ul>
    </div>

    <div class="paragraph">
      <p>Credentials should be stored outside of the code in a configuration file, a database, or a management service for secrets.</p>
    </div>

    <div class="paragraph">
      <p>This rule flags instances of hard-coded credentials used in database and LDAP connections. It looks for hard-coded credentials in connection strings, and for variable names that match any of the patterns from the provided list.</p>
    </div>

    <div class="paragraph">
      <p>It’s recommended to customize the configuration of this rule with additional credential words such as "oauthToken", "secret", …​</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func connect()  {
      user := getEncryptedUser()
      password:= getEncryptedPass() // Compliant

      url := "login=" + user + "&passwd=" + password
      }
      ```

      ```go Fix theme={null}
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="switch statements should not be nested">
    <div class="paragraph">
      <p>Nested \`switch structures are difficult to understand because you can easily confuse the cases of an inner switch as belonging to an outer statement. Therefore nested switch statements should be avoided.</p>
    </div>

    <div class="paragraph">
      <p>Specifically, you should structure your code to avoid the need for nested switch statements, but if you cannot, then consider moving the inner switch\` to another function.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func foo(x,y int) {
      switch x {
      case 0:
      	switch y { // Noncompliant; nested switch
      	// ...
      	}
      case 1:
      	// ...
      default:
      	// ...
      }
      }
      ```

      ```go Fix theme={null}
      func foo(x,y int) {
      switch x {
      case 0:
      	bar(y)
      case 1:
      	// ...
      default:
      	// ...
      }
      }

      func bar(y int) {
      switch y {
      // ...
      }
      }
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Local variable and function parameter names should comply with a naming convention">
    <div class="paragraph">
      <p>A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
      \{identifier\_capital\_plural} hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable pattern.
      Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and debug.
      It also ensures consistency in the code, especially when multiple developers are working on the same project.</p>
    </div>

    <div class="paragraph">
      <p>This rule checks that \{identifier} names match a provided regular expression.</p>
    </div>

    <CodeGroup>
      ```go Bad theme={null}
      func doSomething(my_param int) { // Noncompliant
      var local_ int; // Noncompliant
      ...
      }
      ```

      ```go Fix theme={null}
      func doSomething(myParam int) { 
      var local int;  
      ...
      }
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>
