call methods of a number literal [duplicate]

  • Last Update :
  • Techknowledgy :

Python treats 1. as a beginning of a float number, but fails to parse the rest of the line. Change it to

(1).bit_length()

Python defines floating point literal like this

floatnumber:: = pointfloat | exponentfloat
pointfloat:: = [intpart] fraction | intpart "."
exponentfloat:: = (intpart | pointfloat) exponent
intpart:: = digit +
   fraction:: = "."
digit +
   exponent:: = ("e" | "E")["+" | "-"] digit +

Suggestion : 2

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.,On the other hand, constants can be referenced from many places, but only need to be updated in a single place.,Back references in regular expressions should only refer to capturing groups that are matched before the reference Bug,Products In-IDEIDE extension that lets you fix coding issues before they exist!Discover SonarLint SaaSSetup is effortless and analysis is automatic for most languagesDiscover SonarCloud Self-HostedFast, accurate analysis; enterprise scalabilityDiscover SonarQube

With the default threshold of 3:

public void run() {
   prepare("action1"); // Noncompliant - "action1" is duplicated 3 times
   execute("action1");
   release("action1");
}

@SuppressWarning("all") // Compliant - annotations are excluded
private void method1() {
   /* ... */ }
@SuppressWarning("all")
private void method2() {
   /* ... */ }

public String method3(String a) {
   System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded
   return ""; // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1"; // Compliant

public void run() {
   prepare(ACTION_1); // Compliant
   execute(ACTION_1);
   release(ACTION_1);
}

Suggestion : 3

Last modified: Aug 12, 2022, by MDN contributors

// Untagged, these create strings:
`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

// Tagged, this calls the function "tagFunction" with the template as the
// first argument and substitution values as subsequent arguments:
tagFunction`string text ${expression} string text`
`\`` === '`' // --> true
`\${1}` === '${1}' // --> true
console.log('string text line 1\n' +
   'string text line 2');
// "string text line 1
// string text line 2"
console.log(`string text line 1
string text line 2`);
// "string text line 1
// string text line 2"
const a = 5;
const b = 10;
console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
// "Fifteen is 15 and
// not 20."

Suggestion : 4

If the copied elements are literal values, such as strings, numbers, and Booleans, they’re copied by value—changing the value in the old array has no impact on the same values in the new array, and vice versa.,If the array elements contain strings that could be converted to numbers, then the compareNumbers sort function still works, as number conversion is automatic:,First, the element’s value is tested to see if it matches a given string, ab. If matched, the array element’s index is used to modify the array element’s value with the replacement string, **.,The Array object’s sort method sorts the array elements alphabetically if no optional compare function parameter is provided. To facilitate the sort, all data types are converted to their string equivalent before sorting:

var arrObject = new Array("val1", "val2"); // array as object
var arrLiteral = ["val1", "val2"]; // array literal
var mammals = new Array("cat", "dog", "human", "whale", "seal");
var animalString = "";
for (var i = 0; i < mammals.length; i++) {
   animalString += mammals[i] + " ";
}
alert(animalString);
var numArray = new Array(1, 4, 66, 123, 240, 444, 555);
var i = 0;

while (numArray[i] < 100) {
   alert(numArray[i++]);
}
// set array length
var arrayLength = 3;

// create array
var multiArray = new Array(arrayLength);
for (var i = 0; i < multiArray.length; i++) {
   multiArray[i] = new Array(arrayLength);
}

// add items to first array index
multiArray[0][0] = "apple";
multiArray[0][1] = "banana";
multiArray[0][2] = "cherry";

// second
multiArray[1][0] = 2;
multiArray[1][1] = 56;
multiArray[1][2] = 83;

// third
multiArray[2][0] = ['test', 'again'];
multiArray[2][1] = ['Java', 'script'];
multiArray[2][2] = ['read', 'books'];

alert(multiArray); // printed out in first index order
alert(multiArray[2]); // prints out subarray
alert(multiArray[2][2][0]); // individual item
alert(multiArray[2]); // prints out test,again,Java,script,read,books
alert(multiArray[2][2]); // prints out read,books
alert(multiArray[2][2][1]); // prints out books
var fruitArray = ['apple', 'peach', 'lemon', 'lime'];
var resultString = fruitArray.join('-'); // apple-peach-lemon-lime

Suggestion : 5

Literal Values: The values New Ticket and ` by ` are text literals and return in the expression output as written.,Functions are suggested when you use fn!, a!, or no domain.,Functions and rules are defined by their logic and parameters. These parameters accept arguments that determine how the function will evaluate.,When called multiple times with the same parameters in the same expression, plug-in functions and functions that call external services are only called once and the value is reused.

1._
1
a!map(label: "Item", value: "Entry")
2._
1
3._
a!map(label: "Item", value: "Entry")
1
a!map(label: "Item", value: "Entry")
1._
1 {
   a!map(label: "Item one", value: "Entry one"), a!map(label: "Item two", value: "Entry two")
}
2._
1
3._
{
   a!map(label: "Item one", value: "Entry one"), a!map(label: "Item two", value: "Entry two")
}
{
   a!map(label: "Item one", value: "Entry one"), a!map(label: "Item two", value: "Entry two")
}
1._
1 {
   label: "Item",
   value: "Entry"
}
2._
1
3._
{
   label: "Item",
   value: "Entry"
}

Suggestion : 6

Compound data types: Arrays Character sequences Pointers Dynamic memory Data structures Other data types ,Program structure: Statements and flow control Functions Overloads and templates Name visibility ,Basics of C++: Structure of a program Variables and types Constants Operators Basic Input/Output

1234567891011121314151617
// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
}
The result is 8
return r;
cout << "The result is " << z;

Suggestion : 7

This non-final class defines a clone() method that does not call super.clone(). If this class ("A") is extended by a subclass ("B"), and the subclass B calls super.clone(), then it is likely that B's clone() method will return an object of type A, which violates the standard contract for clone()., If all clone() methods call super.clone(), then they are guaranteed to use Object.clone(), which always returns an object of the correct type.,Calling this.getClass().getResource(...) could give results other than expected if this class is extended by a class in another package., The constructor starts a thread. This is likely to be wrong if the class is ever extended/subclassed, since the thread will be started before the subclass constructor is started.

This equals method is checking to see if the argument is some incompatible type (i.e., a class that is neither a supertype nor subtype of the class that defines the equals method). For example, the Foo class might have an equals method that looks like:

public boolean equals(Object o) {
   if (o instanceof Foo)
      return name.equals(((Foo) o).name);
   else if (o instanceof String)
      return name.equals(o);
   else return false;
}

If you don't think instances of this class will ever be inserted into a HashMap/HashTable, the recommended hashCode implementation to use is:

public int hashCode() {
   assert false: "hashCode not designed";
   return 42; // any arbitrary constant will do
}

If you don't think instances of this class will ever be inserted into a HashMap/HashTable, the recommended hashCode implementation to use is:

public int hashCode() {
   assert false: "hashCode not designed";
   return 42; // any arbitrary constant will do
}

During the initialization of a class, the class makes an active use of a subclass. That subclass will not yet be initialized at the time of this use. For example, in the following code, foo will be null.

public class CircularClassInitialization {
   static class InnerClassSingleton extends CircularClassInitialization {
      static InnerClassSingleton singleton = new InnerClassSingleton();
   }

   static CircularClassInitialization foo = InnerClassSingleton.singleton;
}

The method in the subclass doesn't override a similar method in a superclass because the type of a parameter doesn't exactly match the type of the corresponding parameter in the superclass. For example, if you have:

import alpha.Foo;

public class A {
   public int f(Foo x) {
      return 17;
   }
}
-- --
import beta.Foo;

public class B extends A {
   public int f(Foo x) {
      return 42;
   }
   public int f(alpha.Foo x) {
      return 27;
   }
}

This code opens a file in append mode and then wraps the result in an object output stream like as follows:

OutputStream out = new FileOutputStream(anyFile, true);
new ObjectOutputStream(out);

Suggestion : 8

Reduce duplicate use of a number or string in the code. This is especially important when the value is complicated and long (such as 3.14159 or 0xCAFEBABE)., Reduce duplicate use of a number or string in the code. This is especially important when the value is complicated and long (such as 3.14159 or 0xCAFEBABE). ,A magic number is a numeric value that’s encountered in the source but has no obvious meaning. This “anti-pattern” makes it harder to understand the program and refactor the code.,Sometimes a magic number can be replaced with method calls. For example, if you have a magic number that signifies the number of elements in a collection, you don’t need to use it for checking the last element of the collection. Instead, use the standard method for getting the collection length.

double potentialEnergy(double mass, double height) {
   return mass * height * 9.81;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;

double potentialEnergy(double mass, double height) {
   return mass * height * GRAVITATIONAL_CONSTANT;
}
double PotentialEnergy(double mass, double height) {
   return mass * height * 9.81;
}
const double GRAVITATIONAL_CONSTANT = 9.81;

double PotentialEnergy(double mass, double height) {
   return mass * height * GRAVITATIONAL_CONSTANT;
}
function potentialEnergy($mass, $height) {
   return $mass * $height * 9.81;
}
define("GRAVITATIONAL_CONSTANT", 9.81);

function potentialEnergy($mass, $height) {
   return $mass * $height * GRAVITATIONAL_CONSTANT;
}