# Pointless variable declaration in source code

**URL:** <https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923>\
**Category:** Processing\
**Created:** [March 7, 2025, 2:13am UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923 "2025-03-07T02:13:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 2:13am UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/1 "2025-03-07T02:13:11Z")

</div>

```auto

    public static void hideMenuBar() {
        if (platform == 2) {
            try {
                String td = "processing.core.ThinkDifferent";
                Class<?> thinkDifferent = PApplet.class.getClassLoader().loadClass("processing.core.ThinkDifferent");
                thinkDifferent.getMethod("hideMenuBar").invoke((Object)null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

```

What is the point of the variable `td`?

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 2:17am UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/2 "2025-03-07T02:17:26Z")

</div>

Technically, there’s no need to declare any variables here.

Just `PApplet.class.getClassLoader().loadClass("processing.core.ThinkDifferent").getMethod("hideMenuBar").invoke((Object)null);`

Why `(Object) null`?

Seems like bad/band-aid programming.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 7, 2025, 3:08am UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/3 "2025-03-07T03:08:45Z")

</div>

> [@LangdonS](#):
>
> Wouldn’t this make more sense?:
> 
> ```auto
> processing.core.ThinkDifferent.hideMenuBar((Object) null);
> 
> ```

B/c ThinkDifferent is a class, ThinkDifferent.**hideMenuBar()** would only be allowed if if it happens to be a [`static`](https://processing.org/reference/static.html) method; otherwise we’d have to instantiate ThinkDifferent 1st, then invoke **hideMenuBar()** method over its instance object.

> [@LangdonS](#):
>
> Why so many method invocations?

Either the method **hideMenuBar()** has some access restrictions, such as [`private`](https://processing.org/reference/private.html) or; it might not exist for other platforms besides MacOS!

For either case, we’re required to use reflection techniques in order to access members which have limited access permissions or it’s not 100% guaranteed to exist!

> [@LangdonS](#):
>
> Isn’t `null` already an Object?

Keyword `null` represents the absence of an Object.

But still, using (cast) operator to identify it as an Object seems like a silly overzealous non-AI IDE advise which most devs trust too much!

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 1:57pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/4 "2025-03-07T13:57:48Z")

</div>

Ok.

But what does the `String` `td` do?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 2:11pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/5 "2025-03-07T14:11:00Z")

</div>

As @GoToLoop pointed out `null` is a value not an object. An object reference having the value `null` means that it is not referencing _any_ object at that moment.

So the function call `.invoke(null)` passes a `null` value and the invoke method must test for `null` to prevent a NPE (null pointer exception) being thrown.

The alternative `.invoke((Object)null)` passes a Null Object, so I suspect the developer is using the **Null Object Pattern** to avoid the null test and provide some default behaviour in the absence of an object.

The `null` versus `(Object)null` point just interested so I did a little research and discovered the **Null Object Pattern** , I have never used this pattern and do not know how to implement it so don’t ask me for further detail. 😇

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 2:13pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/6 "2025-03-07T14:13:05Z")

</div>

> [@LangdonS](#):
>
> But what does the `String` `td` do?

Nothing since `td` is not used in scope

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 2:17pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/7 "2025-03-07T14:17:25Z")

</div>

Why is `td` dfined when it is never used?

Why is `Class<?>` `thinkDifferent` defined when a one-liner would work?

Why are `td` and `thinkDifferent` not `final`?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 2:18pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/8 "2025-03-07T14:18:57Z")

</div>

> [@LangdonS](#):
>
> Technically, there’s no need to declare any variables here.

True but

> [@LangdonS](#):
>
> PApplet.class.getClassLoader().loadClass(“processing.core.ThinkDifferent”).getMethod(“hideMenuBar”).invoke((Object)null);

chains many method invocations so if one throws an exception or fails to perform as expected then it can be quite difficult to find the problem. Separating them until the final code is proven is generally a good idea. You can always chain them together later but why bother hell would freeze over before you saw any efficiency benefit in this particular instance.

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 2:19pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/9 "2025-03-07T14:19:31Z")

</div>

`processing.core.ThinkDifferent.hideMenuBar` is a `public static native void`.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 2:21pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/10 "2025-03-07T14:21:46Z")

</div>

> [@LangdonS](#):
>
> Why are `td` and `thinkDifferent` not `final`?

because they only exist within the try block code so making them final is rather superfluous.

> [@LangdonS](#):
>
> Why is `td` dfined when it is never used?

Probably a left over during the development and testing phase.

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 6:41pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/11 "2025-03-07T18:41:06Z")

</div>

Because `processing.core.ThinkDifferent.hideMenuBar` is indeed `static` and `public` (for whatever reason), it should be possible to just call `processing.core.ThinkDifferent.hideMenuBar((Object) null)`.

No need for weird wrapper classes!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 7, 2025, 7:05pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/12 "2025-03-07T19:05:22Z")

</div>

> [@LangdonS](#):
>
> No need for weird wrapper classes!

I also dunno why reflection was used to invoke the `static` & `public` method **hideMenuBar()**.

But as I have mentioned before, maybe class ThinkDifferent only exists in MacOS.

And if we try to direct access ThinkDifferent on either Windows or Linux it could crash!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 7:22pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/13 "2025-03-07T19:22:46Z")

</div>

This method has the `native` modifier which is a non-access modifier used to access methods implemented in a language other than Java like C/C++.

So it not simply about invoking this method because if you look at the method definition in [github](https://github.com/benfry/processing4/blob/main/core/src/processing/core/ThinkDifferent.java) you will see that the method has _ **no** _ body so has _ **no** _ implementation rather like an interface or abstract method.

I suspect that the class loader is not just loading the class but linking it to the mac OS so it can invoke the `hideMenuBar` there.

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 9:53pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/14 "2025-03-07T21:53:00Z")

</div>

It only invokes ThinkDifferent when os == 2

(I’m assuming that signifies macOS)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 7, 2025, 9:55pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/15 "2025-03-07T21:55:46Z")

</div>

> [@LangdonS](#):
>
> (I’m assuming that signifies macOS)

Yes Processing has to know whether it is OSX, Windows or Linux so it can interact with it.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 7, 2025, 9:56pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/16 "2025-03-07T21:56:46Z")

</div>

> <https://github.com/processing/processing4/blob/processing-1297-4.3.4/core/src/processing/core/PConstants.java#L79-L92>

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 7, 2025, 10:25pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/17 "2025-03-07T22:25:36Z")

</div>

“And if we try to direct access ThinkDifferent on either Windows or Linux it could crash!”

But it only gets called on macOS.

---

<div class="post-metadata">

**Author:** ![LangdonS](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@LangdonS](https://discourse.processing.org/u/LangdonS)\
**Post date:** [March 12, 2025, 3:12pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/19 "2025-03-12T15:12:46Z")

</div>

So why is this not the code?

`ThinkDifferent.hideMenuBar();`

hideMenuBar is `static` for sure.

_Could_ work.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 12, 2025, 3:58pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/20 "2025-03-12T15:58:58Z")

</div>

> [@LangdonS](#):
>
> _Could_ work.

> <https://github.com/processing/processing4/blob/processing-1297-4.3.4/core/src/processing/core/ThinkDifferent.java#L123-L133>

Given ThinkDifferent.**hideMenuBar()** is declared as `native`, what if its actual implementation is missing on both Windows & Linux?

However, maybe it’s possible to use a try/catch block to directly invoke those `native` methods w/o resorting to reflection techniques.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 12, 2025, 4:58pm UTC](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923/21 "2025-03-12T16:58:58Z")

</div>

> [@GoToLoop](#):
>
> However, maybe it’s possible to use a try/catch block to directly invoke those `native` methods w/o resorting to reflection techniques.

Seems the only thing to do is to try it 🥳

The sketch below has four methods that use the original Processing code and the **alt** ernative of a simple call to the methods in `ThinkDifferent` class. (Obviously this will only work on osx)

Keys ‘h’ & ‘s’ will hide / show using the original methods  
Keys ‘q’ & ‘w’ will hide / show using direct calls to the `ThinkDifferent` class methods

If you try it they both work with notable feature, once the `showMenuBar()` method is invoked / called it is no longer possible to hide the menu bar a second time.

```Processing
import processing.core.ThinkDifferent;

void setup() {
  size(400, 300);
}

void draw() {
  background(255, 200, 200);
}

void keyTyped() {
  if (key == 'h') hideBar();
  if (key == 's') showBar();
  if (key == 'q') hideBarAlt();
  if (key == 'w') showBarAlt();
}

void hideBarAlt() {
  if (platform == 2) {
    try {
      ThinkDifferent.hideMenuBar();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

void showBarAlt() {
  if (platform == 2) {
    try {
      ThinkDifferent.showMenuBar();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

void hideBar() {
  if (platform == 2) {
    try {
      String td = "processing.core.ThinkDifferent";
      Class<?> thinkDifferent = PApplet.class.getClassLoader().loadClass(td);
      thinkDifferent.getMethod("hideMenuBar").invoke((Object)null);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

void showBar() {
  if (platform == 2) {
    try {
      String td = "processing.core.ThinkDifferent";
      Class<?> thinkDifferent = PApplet.class.getClassLoader().loadClass(td);
      thinkDifferent.getMethod("showMenuBar").invoke((Object)null);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

```

[Next page](https://discourse.processing.org/t/pointless-variable-declaration-in-source-code/45923.md?page=2)
