Useless instructions: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 11 users not shown)
Line 1:
{{draft task}}
{{omit from|Pascal}}
 
;Task
Line 12 ⟶ 13:
<code>CMP #0</code> is nearly completely useless, as most 6502 instructions do this implicitly. It can be handy when you need to check equality to zero after a different register was altered, and in that scenario using <code>PHP/PLP</code> to save and restore the condition codes would be impractical, but these situations are few and far between.
 
<langsyntaxhighlight lang="6502asm">myLoop:
;do stuff here
dex
;cpx #0 ;this isn't needed since the zero flag will be set if x=0
bne myLoop ;if x != 0, jump back to myLoop</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
Line 26 ⟶ 27:
=={{header|Action!}}==
The Action! language has four fundamental data types, BYTE, CHAR, INT and CARD. The first two can be used interchangably and are 8-bit unsigned integers. This is presumably for documentation purposes, however the language also has a macro facility that would allow the programmer to <code>DEFINE CHAR="BYTE"</code> or <code>DEFINE BYTE="CHAR"</code> if only one was available.
 
=={{header|Ada}}==
The Ada Language Reference Manual Appendix J explains all the features of the language which are largely redundant and are discouraged in newly developed source code.
The features are:
<pre>
- Renaming of library units
- Allowed Replacement of Characters
- Reduced Accuracy Subtypes
- The Constrained Attribute
- The package ASCII defined within the package Standard
- The exception Numeric_Error
- At Clauses
- Interrupt Entries
- Mod Clauses
- The Storage_Size Attribute
- Specific Suppression of Checks
- The Class Attribute of Untagged Incomplete Types
- Pragma Interface
- Dependence Restriction Identifiers
- Character and Wide_Character Conversion Functions
- Aspect-related Pragmas
</pre>
The full description of these obsolescent features is described in [http://www.ada-auth.org/standards/12rm/html/RM-J.html]
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 23.80.31.win32}}
As well as being able to write <code>IF TRUE THEN SKIP ELSE some code that will never be executed FI</code> and <code>WHILE FALSE DO some code that will never be executed OD</code>, as in other languages shown here, Algol 68 has a feature that was presumably intended to aid portability (though whether it does in practice is another matter...):<br>
Algol 68 has a notionally infinite number of integer, real and comples types. THe standard types INT, REAL and COMPL, can have an arbitrary number of LONG or SHORT prefixes to indicate larger or smaller types. Whether these are actually larger or smaller depends on the implementation - no warning is issued (at least, not in Algol 68G) to indicate that e.g. LONG LONG LONG REAL is not longer than LONG LONG REAL. In addition, there are "environment enquiries" that will reveal how big the types are, so e.g. max int is the largest INT value, long max int is the largest LONG INT value, etc. An arbitrary number of long and short prefixes can be applied.<br>
<br>
As this sample shows, Algol 68G has only three sies of integer. The precission of LONG LONG INT can be specified by a pragmatic comment. The value shown here is the default.<br>
As this sample shows, Algol 68G has only three sies of integer. The precission of LONG LONG INT can be specified by a pragmatic comment. The value shown here is the default.
(Interestingly, I always assumed Algol 68G's LONG INT was 64 bits but I see this isn't so...)
<br>Algol 68G has 64 bit INT and 128 bit LONG LONG INT. In previous versions, INT was 32 bit and LONG INT had a precision of 35 decimal digits.<br>
<lang algol68>print( ( long long long max int, newline ) );
<syntaxhighlight lang="algol68">print( ( long long long max int, newline ) );
print( ( long long max int, newline ) );
print( ( long max int, newline ) );
Line 44 ⟶ 69:
short short short short short short short short short short
short short short short short short short short short short
short short short short short short short short short short max int, newline ) )</langsyntaxhighlight>
{{out}}
<pre>
+999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999
+999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999
+170141183460469231731687303715884105727
+99999999999999999999999999999999999
+9223372036854775807
+2147483647
+9223372036854775807
+2147483647
+9223372036854775807
+2147483647
+9223372036854775807
+2147483647
+9223372036854775807
+2147483647
</pre>
 
=={{header|Applesoft BASIC}}==
Many ways of printing an empty string.
<syntaxhighlight lang="gwbasic">?;:?"";:?"";"";:?;;;;;</syntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f USELESS_INSTRUCTIONS.AWK
BEGIN {
Line 69 ⟶ 98:
delete array
}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Line 78 ⟶ 107:
Another keyword which is teetering on redundancy is 'register' whose purpose is to suggest to the compiler that it should store a variable in a CPU register rather than memory. However, modern optimizing compilers don't need such a suggestion and will use CPU registers anyway if they deem it appropriate. But it may still be relevant in embedded system programming so it can't be said to be completely redundant at the present time.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 110 ⟶ 139:
uselessFunc(0);
printf("Working OK.\n");
}</langsyntaxhighlight>
 
{{out}}
Line 116 ⟶ 145:
Working OK.
</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
printfn "%s" ("Nigel "^"Galloway")
printfn "%s" ("Nigel "+"Galloway")
</syntaxhighlight>
{{out}}
<pre>
main.fs(1,23): warning FS0062: This construct is for ML compatibility. Consider using the '+' operator instead. This may require a type annotation to indicate it acts on strings. This message can be disabled using '--nowarn:62' or '#nowarn "62"'.
> mono main.exe
Nigel Galloway
Nigel Galloway
</pre>
 
=={{header|Factor}}==
Factor uses a data stack to pass arguments to functions. Factor provides at least five ways to arrange the stack, all five of which can make the other four redundant.
 
To get the data stack from <code>1 2 3</code> to <code>1 2 2 3 1</code> we could:
 
Use stack shuffle words:
 
<syntaxhighlight lang="factor">dupd reach</syntaxhighlight>
 
Use data flow combinators:
 
<syntaxhighlight lang="factor">[ ] keepdd [ [ ] [ ] bi ] 2dip</syntaxhighlight>
 
Use local variables:
 
<syntaxhighlight lang="factor">[let :> ( a b c ) a b b c a ]</syntaxhighlight>
 
Use dynamic variables:
 
<syntaxhighlight lang="factor">SYMBOLS: a b c ;
c set b set a set a get b get b get c get a get</syntaxhighlight>
 
Use <code>shuffle(</code> syntax:
 
<syntaxhighlight lang="factor">shuffle( a b c -- a b b c a )</syntaxhighlight>
 
=={{header|Go}}==
Line 121 ⟶ 189:
 
However, nothing's perfect and the analogous redundant constructions which are allowed in Wren are not picked up by the Go compiler either as the following program shows.
<langsyntaxhighlight lang="go">package main
 
import (
Line 167 ⟶ 235:
set[0] = NotCompletelyUseless{} // duplicate key so not added to set
fmt.Println(set)
}</langsyntaxhighlight>
 
{{out}}
Line 173 ⟶ 241:
map[0:{}]
</pre>
 
=={{header|J}}==
 
Here, we will focus on J's verb <code>>:</code>. This has two meanings, and both of them are redundant within the language.
 
First, <code>X &gt;: Y</code> tests whether X is greater than or equal to Y. And, J has several other ways of accomplishing this same task. For example, if X is greater than or equal to Y, then Y is less than or equal to X, so an equivalent expression would be <code>Y &lt;: X</code>. Another approach would be to test that X is not less than Y, which might be expressed as <code> -.X &lt; Y</code>
 
Second, <code>>: Y</code> increments the value obtained from Y (it does not modify Y). And, J also has several other ways of accomplishing this task. For example: <code>1+Y</code>. Or, we could express this in polynomial form as <code>1 1 p. Y</code>, or we could subtract negative 1. Etc.
 
J has many other equally useless instructions, of course.
 
=={{header|jq}}==
'''The following remarks apply to both the C and Go implementations of jq.'''
 
jq is fundamentally a stream-oriented language, but the collection of built-in
functions (in builtin.jq) includes array-oriented definitions for the sake of familiarity and/or convenience. This duality results in what could be considered redundancies.
 
For example, consider the 'any' and 'all' functions, all versions of which have short-circuit semantics. The array-oriented versions of these functions are defined in terms of the more fundamental stream-oriented functions, making the redundancy plain to see:
<syntaxhighlight lang="jq">def all: all(.[]; .);
 
def any: any(.[]; .);</syntaxhighlight>
 
=={{header|Julia}}==
Line 180 ⟶ 269:
arrays of `Bool` type. This makes the `count()` function redundant except to better express
intent to count rather than add.
<langsyntaxhighlight lang="julia">array = [true, true, true, false, false, true]
 
@show count(array) # count(array) = 4
@show sum(array) # sum(array) = 4
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
Line 195 ⟶ 284:
Also, when defining a type with variants, when a branch doesn’t contain any field, we can use the keyword <code>discard</code> (as in a case statement) or the value <code>nil</code>.
 
<langsyntaxhighlight Nimlang="nim">type T = object
case i: 0..2
of 0: value: int
of 1: discard
of 2: nil</langsyntaxhighlight>
 
But, as <code>discard</code> is used as empty statement and <code>nil</code> is used as null value for references and pointers, we cannot say that they are useless even if they are synonyms in one context.
Line 209 ⟶ 298:
The five core builtin types form a natural hierarchy, so you can store an integer in a variable declared as
an integer or a number (aka atom), the latter in an object, and a string in a string or a sequence, and the
latter in an object. StoringSo, theapart contentsfrom oferror anreporting, objectyou indon't (say)actually anneed integerany maydata eithertypes workother just fine, orthan "object".<br>
trigger a fatal runtime crash - you can use pretty obvious "if integer(o) then" tests before doing that.
So, apart from error reporting, you don't actually need any data types other than "object".<br>
Phix supports at least 45 different ways of expressing the same simple integer value, from binary to base 36,
and that is before even considering string versions of it. For instance there is no difference whatsoever
Line 231 ⟶ 318:
One genuinely redundant feature which has since been redacted was "{a, b, c} @= x", where the "@=" was meant to
stand for "all become equal to". Technically it just about worked when x was a literal integer, but was badly
broken in almost every other sense, and from the 0 bug reports clearly had never been used in the real world.
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">i2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span> <span style="color: #000080;font-style:italic;">// same apart from error reporting</span>
<span style="color: #004080;">int</span> <span style="color: #000000;">i_too</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span> <span style="color: #000080;font-style:italic;">// int is just an alias of integer
-- six different ways to set i to 3:</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0b11</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0(2)11</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0(36)3</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#3</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x3</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> '\<span style="color: #000000;">#03</span>'
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">3</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}</span>
<span style="color: #000080;font-style:italic;">-- four ways to print the first two</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]})</span>
<span style="color: #000080;font-style:italic;">-- two ways to append a 3</span>
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}&</span><span style="color: #000000;">3</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (nb same only when appending a single number)</span>
<span style="color: #008080;">if</span> <span style="color: #004600;">true</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">// do something</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Never called\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Never called\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">false</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Never called\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (On Windows, will create a console if one not already attached)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Working OK.\n"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
<small>(In fact, if you run "p -d test.exw" on the above, <code>if true</code> .. <code>end while</code> only emits a single instruction, <code>mov i,3</code> at the start of the for loop, and no other instructions at all for those thirteen lines.)</small><br>
<small>(Also, for the above and this by inspecting the list.asm file created, you would be able to see that it doesn't bother adding 3 to i, but instead simply sets it to 6 then 9.)</small><br>
{{out}}
<pre>
{1,2}
{1,2}
{1,2}
{1,2}
{1,2,3}
{1,2,3}
Working OK.
</pre>
 
=={{header|Quackery}}==
 
Several words in Quackery just put a number on the stack and could be replaced by literals. (Literal numbers ''are'' words in Quackery; their behaviour is to put their value on the stack.) For example, <code>true</code> and <code>false</code> which put <code>1</code> and <code>0</code> on the stack respectively and are arguably redundant, as Quackery programmers know, or really should know, these values.
 
However, they are not useless, unless code legibility is regarded as a useless criterion. It is not.
 
A second way of looking at this task is to understand that a Quackery program is a sequence of words (one or more printable characters) separated by whitespace, and that there are no invalid programs, but there are a great many essentially useless words; they are useless because they are not yet known to Quackery.
 
Your cat could write a valid, but probably useless, Quackery program by walking across the keyboard. Say it types <code>fvefbbfibiu</code>, and you pass this string to the Quackery compiler, <code>build</code>. <code>build</code> will cheerfully return a perfectly valid Quackery program. Executing that program with <code>do</code> will reveal that it prints a string to the console, and that string is <code>Unknown word: fvefbbfibiu</code>, (assuming that you have not previously defined the word <code>fvefbbfibiu</code>.)
 
=={{header|Raku}}==
Line 245 ⟶ 399:
 
Some examples: Say you have a variable $x that holds an integer, and you want to add 1 to it.
<syntaxhighlight lang="raku" perl6line>my $x = 1;
 
$x = $x + 1; # classic
Line 251 ⟶ 405:
++$x; # pre-increment
$x++; # post-increment
$x.=succ; # method, find successor</langsyntaxhighlight>
 
''Raku is by no means unique in that regard.'' Each one of those nominally accomplishes the same thing, though they go about it in slightly different ways, and are intended for slightly different purposes. Redundant on the face of it, but different reasons for each.
Line 257 ⟶ 411:
There may be different operations that essentially perform the same function that have been retained for historical compatibility and / or ease for crossover programmers. the <code>for next</code> construct really only exists in Raku for programmer convenience. Internally it is converted to a map operation. Programmers can use map directly, but if they come from a language that doesn't provide map, and aren't used to it, <code>for next</code> is a easy stepping stone.
 
<syntaxhighlight lang="raku" perl6line>for 1..10 { next if $_ %% 2; say '_' x $_ }; # for next
map { next if $_ %% 2; say '_' x $_ }, 1..10; # equivalent map</langsyntaxhighlight>
 
Syntactic sugar (typically) replaces some longer expression with a shorter, more easily typed one. Raku abounds with them.
 
<syntaxhighlight lang="raku" line>
<lang perl6>
my @a1 = Q:w[one two three]; # list of strings using the Q sublanguage
my @a2 = <one two three>; # syntactic sugar for the same operation
Line 268 ⟶ 422:
say (1,2,3,4,5,6,7).reduce(&infix:<*>); # multiply all of the items in a list together
say [*] (1,2,3,4,5,6,7); # same operation, sweeter syntax
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 274 ⟶ 428:
 
For good measure I've also included something which looks useless but isn't in fact.
<langsyntaxhighlight ecmascriptlang="wren">var uselessFunc = Fn.new { |uselessParam| // required but never used
if (true) {
// do something
Line 307 ⟶ 461:
 
uselessFunc.call(0)
System.print(NotCompletelyUseless) // prints the string representation of the Class object</langsyntaxhighlight>
 
{{out}}
Line 328 ⟶ 482:
Compare the following code snippets, which are functionally identical but the latter is half the size and more than double the speed:
 
<langsyntaxhighlight lang="z80">SET 7,A
SET 6,A
RES 5,A
Line 336 ⟶ 490:
OR %11000000
AND %11001111
;TOTAL 4 BYTES, 14 CPU STATES</langsyntaxhighlight>
9,476

edits