Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Suggestion] Return a Null Object rather than null

Featured Replies

To attack a man, you'd look for the npc object, then interact with it.
 
But there's a chance methods such as "closest" will return null, resulting in an NPE. So you do a null check:

NPC man = getNPCs().closest("Man");
if(man != null)
    man.interact("Attack");

Scripters are basically saying "if man is null, do nothing". This null check does not have any real purpose, other than ensuring you don't trigger an NPE.
 
Scripters should be able to simply do

getNPCs().closest("Man").interact("Attack");

If there is no man, closest should return a Null Object which has the behavior of "doing nothing". You would still be able to call the interact method, it'll just do nothing.
 
Now let's say for some reason you wanted to actually check if closest found an NPC. Rather than interact with the npc, you want to just make sure there's one around you or something. The exists method could simply account for this check.
 
The point is, I see scripts with excessive amount of null checks. People do null checks "just to be safe", which leads to cluttery code. This should not be the case. Do not use null.
 
I believe this will simplify scripting for new people. Null is a trap.

  • Administrator

It's a trap by not using it since scripters would just refuse to check if the target exists and proceed to blame OSBot for their script not working. By doing it this way, it's painfully obvious what the problem is.

  • Author

It's a trap by not using it since scripters would just refuse to check if the target exists and proceed to blame OSBot for their script not working. By doing it this way, it's painfully obvious what the problem is.

They wouldn't need to check if it exists. It would simply be an option.

The idea is that scripters do null checks only to prevent NPE. They do not execute code if the value is actually null (through an else); do nothing on null.

Doing something like

npcs().closest("Man').interact(...)

Requires a null check, when it could just be how I wrote it above (without the check). The null check isn't needed unless you actually wanted to do something in the case that null was returned. If null was returned, the scripter wouldn't do anything, and allow the loop to keep going until it's not null.

Edited by fixthissite

  • Administrator

They wouldn't need to check if it exists. It would simply be an option.

The idea is that scripters do null checks only to prevent NPE. They do not execute code if the value is actually null (through an else); do nothing on null.

Doing something like

npcs().closest("Man').interact(...)

Requires a null check, when it could just be how I wrote it above (without the check). The null check isn't needed unless you actually wanted to do something in the case that null was returned. If null was returned, the scripter wouldn't do anything, and allow the loop to keep going until it's not null.

 

Except in virtually all cases, scripters choose to delay doing anything for around a second. If they had a check, that delay would be pretty much non-existent.

Regardless of whether it's a null check or an empty object check, there should be an if-statement there. Returning null simply makes it more obvious.

  • Author

Except in virtually all cases, scripters choose to delay doing anything for around a second. If they had a check, that delay would be pretty much non-existent.

Regardless of whether it's a null check or an empty object check, there should be an if-statement there. Returning null simply makes it more obvious.

Why should there be an if statement there? That's what this suggestion is attempting to remove. It's not needed, since in the case of null, nothing is executed (most of the time)

If a person wanted to log that the entity doesn't exist, they could optionally perform a check similar to a null check (via the exists method), but it should only be optional, not forced

Edited by fixthissite

  • Administrator

Why should there be an if statement there? That's what this suggestion is attempting to remove. It's not needed. In the case of null, nothing is executed.

If a person wanted to log that the entity doesn't exist, they could optionally perform a check similar to a null check (via the exists method), but it should only be optional, not forced

 

It is if you want a decent script. Otherwise, your script doesn't even know what it's doing until seconds after the fact. Forcing a null return only makes that more obvious to find and fix. It's staying as null.

Guest
This topic is now closed to further replies.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.