Useless instructions

From Rosetta Code
Revision as of 11:33, 16 October 2021 by PureFox (talk | contribs) (Added C)
Useless instructions is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Showcase an instruction or function built into the language, that is made redundant by another instruction also built into the language. It can be limited to a specific way that function is used, e.g. printing a null string, but try not to make it that obvious.

NOP and any instructions or functions that are intended to do nothing do not count.



6502 Assembly

CMP #0 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 PHP/PLP to save and restore the condition codes would be impractical, but these situations are few and far between.

<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</lang>

68000 Assembly

CLR.L Dn sets an entire 32-bit register to 0x00000000. However, MOVEQ #0,Dn does the same thing but faster. The only advantage CLR Dn has is that it can directly clear memory locations.

8086 Assembly

xor ax,ax (or any other data register) takes fewer bytes to encode than mov ax,0 and achieves the same result. The only difference is that mov ax,0 doesn't set the flags, which can be used to the programmer's advantage when sequencing operations.

C

C can boast (if that's the right word) analogous redundant constructions to the Go and Wren examples though you might get warnings with some compilers. AFAIK creating an empty struct is also useless and may produce a warning or even an error as it's technically non-standard.

However, C has a more obvious form of redundancy in the shape of the 'auto' keyword. This was inherited from the 'B' language where it was needed to declare a local variable though is never needed in C itself.

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.

<lang c>#include <stdio.h>

  1. include <stdbool.h>

void uselessFunc(int uselessParam) { // parameter required but never used

   auto int i; // auto redundant 
   if (true) {
       // do something
   } else {
       printf("Never called\n");
   }
   for (i = 0; i < 0; ++i) {
       printf("Never called\n");
   }
   while (false) {
       printf("Never called\n");
   }
   printf(""); // no visible effect but gcc 9.3.0 warns against it
   return; // redundant as function would return 'naturally' anyway

}

struct UselessStruct {

   // no fields so effectively useless and apparently non-standard in any case

};

int main() {

   uselessFunc(0);
   printf("Working OK.\n");

}</lang>

Output:
Working OK.

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. <lang go>package main

import (

   "fmt"

)

type any = interface{}

func uselessFunc(uselessParam any) { // parameter required but never used

   if true {
       // do something
   } else {
       fmt.Println("Never called")
   }
   for range []any{} {
       fmt.Println("Never called")
   }
   for false {
       fmt.Println("Never called")
   }
   fmt.Print("") // no visible effect
   return // redundant as function would return 'naturally' anyway

}

type NotCompletelyUseless struct {

   /*
      On the face of it this struct is useless because:
      (1) it has no fields
      (2) there are no associated methods
      (3) there is no inheritance in Go
      However, it's not in fact completely useless because:
      (4) it can be used as a zero length value in a map to emulate a set
   */

}

func main() {

   uselessFunc(0)
   set := make(map[int]NotCompletelyUseless)
   set[0] = NotCompletelyUseless{}
   set[0] = NotCompletelyUseless{} // duplicate key so not added to set
   fmt.Println(set)

}</lang>

Output:
map[0:{}]

Julia

In julia the function `count()` can be used to count the true values in an array. Since in julia a `Bool` boolean is just an `Int` type, with 0 == false and 1 == true, the Julia `sum()` function and the `count()` function are functionally identical when applied to arrays of `Bool` type. This makes the `count()` function redundant except to better express intent to count rather than add. <lang julia>array = [true, true, true, false, false, true]

@show count(array) # count(array) = 4 @show sum(array) # sum(array) = 4 </lang>


Wren

There are no language features or standard library methods in Wren which are completely redundant either by design or due to subsequent developments. However, it's not difficult to think of instances where some of them can be used in a redundant way as in the following script.

For good measure I've also included something which looks useless but isn't in fact. <lang ecmascript>var uselessFunc = Fn.new { |uselessParam| // required but never used

   if (true) {
       // do something
   } else {
       System.print("Never called")
   }
   for (e in []) {
       System.print("Never called")
   }
   while (false) {
       System.print("Never called")
   }
   System.write("")  // no visible effect
   return // redundant as function would return 'naturally' anyway

}

class NotCompletelyUseless {

   /*
      On the face of it this class is useless because:
      (1) it has no methods
      (2) although it inherits from Object, static methods are not inherited
      However, it's not in fact completely useless because:
      (3) a Class object is still created and accessible
      (4) it can be used as an abstract base class for other classes
   */

}

uselessFunc.call(0) System.print(NotCompletelyUseless) // prints the string representation of the Class object</lang>

Output:
NotCompletelyUseless

x86 Assembly

Originally, LOOP label was a one-line version of DEC ECX JNZ label, but even Intel now recommends to use the latter over the former, due to LOOP taking longer to execute than DEC ECX JNZ on the 486 and beyond. Most compilers don't use LOOP anymore either. An explanation can be found here.

Z80 Assembly

xor a is shorter than ld a,0. The latter doesn't clear the zero or carry flags, which is often useful. But if the flags aren't needed for an upcoming branch, call, or return, it's preferred to use xor a.