How to exit an apex function? - salesforce

I have an apex function which has a void return type. I want to exit the function at a specific position. Is this possible in apex without changing return type pagereference?

In apex, the return keyword signals to immediately stop processing statements in that function.
public void doNothing() {
return;
}

I'm not sure that I correctly understood your question (more detailed description will be useful), but it seems that you can use a return statement by the following way:
public PageReference method() {
//some your code ....
if (some specific condition) {
return null;
}
PageReference pageRef = new PageReference('someURL');
return pageRef;
}
Please show your code for a more target help

Related

Is there something in C that is analogous to out/out keyword of C#?

void extract_left_subtree(node *right_child)
{
while(right_child->right)
{
right_child = right_child->right;
}
printf("rightmost inside the funtion is %d\n",right_child->data);
}
in this function the last line is printing the correct value.
node *right_child=root;
extract_left_subtree(right_child);
printf("rightmost child is %d\n",right_child->data);
But here I'm getting some garbage value.
I know what the problem is, I know why it's happening, the only thing I don't know is how to rectify this?
There are keywords like ref and out in C# which can be used to achieve the same but the issue is, how can we do the same in C language?
I don't want to return values from the method please
I don't want to return values from the method please
If you don't want to return a value you can do:
void extract_left_subtree(node **right_child)
{
while((*right_child)->right)
{
(*right_child) = (*right_child)->right;
}
printf("rightmost inside the funtion is %d\n", (*right_child)->data);
}
and call it like:
extract_left_subtree(&right_child);
This passes the address of right_child to the function and then the function can directly update the value of right_child

cydia-substrate tweak development - giving errors on make

I am new to making mobile substrate tweaks and I wanted to hook this function but having a bit of trouble.
%hook classname
- (void)function:(BOOL) {
%orig;
return TRUE;
}
%end
but when i try to MAKE this it gives me an error. void function should not return a value.
I just want to change the BOOL to always return true.
Thanks.
Your aim is vague a bit. The error is because your function header is
- (void)function:(BOOL) foo {
%orig;
return YES;
}
This means function function has a boolean argument and RETURNS NOTHING. So you can't write return YES in your function. This line causes the error.
If your goal is to return the value of the original function with YES argument no matter what foo is, you have to rewrite your function like below:
- (void)function:(BOOL) foo {
%orig(YES);
}

Defining setter return value

I just had a problem in one of my projects. Maybe I got the wrong concept about encapsulation.
Encapsulation protects member variables from classes, by defining getters and setters methods, now, i was reading that setters must be void, but in that case, how can I know if the function really set the value passed by argument. For example
void setArea(int a) {
if(a>0)
Area = a;
}
How can I be sure that argument "a" was a correct value, wouldnt be better defining the function like this
bool setArea(int a) {
if(a>0) {
Area = a;
return true;
}
return false;
}
Is that ok? that way i can know if a change really happened.
I think what you're looking for is a guard clause that throws an exception if invalid values are set:
void setArea(int a) {
if (a <= 0) throw new InvalidArgumentException(...);
Area = a;
}
But if you want client code to test for invalid values before setting them, you could have this:
bool isAreaValid(int a) {
return a > 0;
}
void setArea(int a) {
if (!isAreaValid(a)) throw new InvalidArgumentException(...);
Area = a;
}
Clients could be coded like this:
if (obj.isAreaValid(myArea)) {
obj.setArea(myArea);
}
But don't stop there. If the concept of area is important, spring it into existence into its own value object to make your design clearer.

Is it a proper way to access a private function in cakePHP

In a controller, I got two functions that one is made to be private:
function toavail(){
$this->autoRender=false;
$result2=$this->__avail();
if($result2==0){return "OK";}
else{return 0;}
}
function __avail(){
$result1=$this->Site1->findByusername('1');
if($result1){
return 1;
}
else{
return 0;
}
}
I am not sure if it is a proper way to access the private function in this case.
You're accessing it correctly (assuming that both methods are in the same controller class), but in case you're not aware, your __avail() method isn't really private. The double underscore (__) prefix is something of a convention, but it's only a convention. Your "private" method is really public in actuality. To make it private you need to specify it as such in the signature:
private function __avail() { ... }
you are accessing it correctly, but, you are not declaring correctly the function.
You should declare it as protected -> protected function __avail()
Source: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

Return Value for "should cancel"

I have a method DoCleanUp(), which will ask user to proceed and then clear current workspace. It will return if user choose to cancel this process.
My question is, which signature is best to indicate a "cancel"?
bool DoCleanUp(); // return false to indicate canceled.
bool DoCleanUp(); // return true to indicate this method should be canceled.
void DoCleanUp(bool& cancel); // check parameter 'cancel' to see if this method was canceled.
UPDATE: As for the language, it's C++\CLI or C#.
UPDATE2: Now suppose I have to save a file in the DoCleanUp method. I'll prompt a dialog ask user whether to save/not save/cancel the file. Based on the answers, here is what I came up:
void DoCleanUp();
DialogResult AskToSaveFile(); // return yes/no/cancel
void DoCleanUp( bool saveFile );
Usage:
void DoCleanUp()
{
DialogResult result = AskToSaveFile();
if( result == DialogResult::Cancel ) return;
bool saveFile = (result == DialogResult::Yes) ? true : false;
DoCleanUp( saveFile );
}
Then by calling DoCleanUp(), you know user will have the opportunity to cancel;
By calling DoCleanUp(bool saveFile), you can control whether to save file without asking user.
Is that looks better?
This is a classic single responsibility problem.
The reason that you are unsure about the signature is that the method is doing 2 things.
I would create 2 methods:
bool CheckIfTheUserWantsToCancel()
void DoCleanUp()
EDIT
Based on the comments and edits to the question I would create a 3rd method:
void SaveFile()
The DoCleanUp would then first call CheckIfTheUserWantsToCancel, and then if not cancelled would call SaveFile.
IMHO this is much better than trying to remember that DoCleanUp with parameter false will save the file without asking the user, or was it the other way around?
Without more details I would say answer 1 is the best IMHO. Third is rather ugly since it requires more code for calling.
But maybe consider rewriting code to this
void CleanUp() {
switch (AskUser()) {
case ButtonOk: CleanUpDesk(); break;
case ButtonNo: break;
default:
case ButtonCancel: CancelCleanUpDesk(); break;
}
}
This seems to in the spirit of single responsibility. My code somehow breaks your problem into two steps: asking user and performing action.
I would use your 1 version.
bool DoCleanUp(); // return false to indicate canceled.
The assumption is, that it returns true when the cleanup is done. Returning false would indicate a 'Error' state. It might even make sense to return an int. In this case the convention usually is that 0 represents success and everything else is an error code.
Regardless of what you decide, document what your return values mean!
The confusing bit is the calling it DoSomething(), when it might not do anything. How about
if (QueryCleanup()) // boolean
DoCleanup(); // void
More verbose but clearer, even without seeing the declaration.
You should not use a boolean for statuses (or status messages). Create an Enum:
public Enum CleanupStatus
{
Ok = 0,
Cancel
}
This way it is more clear what the return value is ... and if you need to add more statuses, you can.
(This is all from Code Complete 2, you should read it if you haven't yet.)
You have two requests basically. The outer request is to create a new workspace. The inner request is to save the current workspace. You want to return true if the outer request continues and false if the outer request is aborted. The action of the inner request is not important to the outer request and so should be some kind of delegate/functor/closure.
Make a class to genericize this:
class YesNoCancel {
string question; // question to ask the user about the inner state
delegate doit; // function to call to
delegate dontdoit;
public:
YesNoCancel(string question, delegate doit, delegate dontdoit = null) {...}
bool run() {
switch (AskUser(question)) {
case ANSWER_YES: doit(); return true;
case ANSWER_NO: return true;
case ANSWER_CANCEL: if (dontdoit) dontdoit(); return false;
};
//usage
void NewWorkspace() {
if (m_workspace) {
YesNoCancel ync("Save current workspace?", saveworkspace);
if (!ync.run()) return;
}
// new workspace code
}
void CloseApp() {
YesNoCancel ync("Save current workspace?", saveworkspace);
if (ync.run()) ExitApplication();
}
I believe option three gives the most clarity. When you have the bool as a return type it is not immediately clear what it is used for.
I usually go with
bool DoCleanUp(); // Returns true if cancel
but mostly it depends on whether the calling code looks like this:
if (DoCleanUp()) {
// Do cancel up code
}
or:
if (DoCleanUp()) {
// Do non-cancel post clean up code
}
Basically I try to make my tests not have to use a ! or language equivilent as I find it hard to see.
I definitely would not do number 3.
I prefer the third signature, only because by looking at it (without any extra documentation), I can tell more about what the method does. I would call the argument something more explicit, like processCancelled, though.

Resources