Useless instructions: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 8 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
</langpre>
The full description of these obsolescent features is described in [http://www.ada-auth.org/standards/12rm/html/RM-J.html]
 
=={{header|ALGOL 68}}==
Line 34 ⟶ 58:
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>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>
<langsyntaxhighlight lang="algol68">print( ( long long long max int, newline ) );
print( ( long long max int, newline ) );
print( ( long max int, newline ) );
Line 45 ⟶ 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>
Line 58 ⟶ 82:
</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 71 ⟶ 98:
delete array
}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Line 80 ⟶ 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 112 ⟶ 139:
uselessFunc(0);
printf("Working OK.\n");
}</langsyntaxhighlight>
 
{{out}}
Line 118 ⟶ 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
</langpre>
 
=={{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 123 ⟶ 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 169 ⟶ 235:
set[0] = NotCompletelyUseless{} // duplicate key so not added to set
fmt.Println(set)
}</langsyntaxhighlight>
 
{{out}}
Line 175 ⟶ 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}}==
Line 183 ⟶ 259:
 
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:
<langsyntaxhighlight lang="jq">def all: all(.[]; .);
 
def any: any(.[]; .);</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 193 ⟶ 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 208 ⟶ 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 244 ⟶ 320:
broken in almost every other sense, and from the 0 bug reports clearly had never been used in the real world.
 
<!--<langsyntaxhighlight Phixlang="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>
Line 287 ⟶ 363:
<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>
<!--</langsyntaxhighlight>-->
<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>
Line 300 ⟶ 376:
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 313 ⟶ 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 319 ⟶ 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 325 ⟶ 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 336 ⟶ 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 342 ⟶ 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 375 ⟶ 461:
 
uselessFunc.call(0)
System.print(NotCompletelyUseless) // prints the string representation of the Class object</langsyntaxhighlight>
 
{{out}}
Line 396 ⟶ 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 404 ⟶ 490:
OR %11000000
AND %11001111
;TOTAL 4 BYTES, 14 CPU STATES</langsyntaxhighlight>
9,476

edits