Useless instructions: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(8 intermediate revisions by 7 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 28 ⟶ 29:
 
=={{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 Ada compiler produces warnings for many useless constructs. Eliminating all warnings eliminates most useless constructs. If the parameter passing mode for the parameter in procedure Useless is changed to the OUT mode the compiler generates a compiler '''error''' and the actual parameter must be a variable. The value of an OUT parameter must be assigned within the procedure.
The features are:
Ada syntax is case-insensitive, therefore the programmer cannot create a new identifier which differs from another identifier only by letter case.
 
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
 
procedure Main is
procedure Useless (param : in Integer) is -- compiler warning - "param" not referenced
begin
if True then
null;
else
Put_Line ("Never called"); -- Warning: this code can never be executed and has been deleted
end if;
 
for I in 2 .. 1 loop -- Compiler warning - loop will not execute
Put_Line ("Never called");
end loop;
 
while False loop
Put_Line ("Never Called");
end loop;
end Useless;
 
begin
Useless (0);
Put_Line ("Working OK.");
end Main;</lang>
{{out}}
<pre>
- Renaming of library units
Working OK.
- 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}}==
Line 67 ⟶ 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 78 ⟶ 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 91 ⟶ 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 104 ⟶ 98:
delete array
}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Line 113 ⟶ 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 145 ⟶ 139:
uselessFunc(0);
printf("Working OK.\n");
}</langsyntaxhighlight>
 
{{out}}
Line 153 ⟶ 147:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
printfn "%s" ("Nigel "^"Galloway")
printfn "%s" ("Nigel "+"Galloway")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 164 ⟶ 158:
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}}==
I can't recall any language features or standard library methods in Go which are completely redundant either by design or due to subsequent developments. Moreover, the compiler does a lot to prevent unintentional redundancy by flagging unused packages, unused variables and unreachable code.
 
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 214 ⟶ 235:
set[0] = NotCompletelyUseless{} // duplicate key so not added to set
fmt.Println(set)
}</langsyntaxhighlight>
 
{{out}}
Line 220 ⟶ 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 228 ⟶ 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 238 ⟶ 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 253 ⟶ 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 289 ⟶ 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 332 ⟶ 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 345 ⟶ 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 358 ⟶ 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 364 ⟶ 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 370 ⟶ 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 381 ⟶ 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 387 ⟶ 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 420 ⟶ 461:
 
uselessFunc.call(0)
System.print(NotCompletelyUseless) // prints the string representation of the Class object</langsyntaxhighlight>
 
{{out}}
Line 441 ⟶ 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 449 ⟶ 490:
OR %11000000
AND %11001111
;TOTAL 4 BYTES, 14 CPU STATES</langsyntaxhighlight>
9,488

edits