RSS Feed

Rectangles Inline If Statement Code Tips Performance and Readability

The purpose of this example is using what is called an “inline if statement.” Here I am creating a rectangle and the variable potential determines the downward size of the rectangle. For background only, I am using the whole rectangle to determine how much of the screen to use for drawing. The important bit of the statement is this..

int noOfChildren = 10;
…. (potential ? 1 : noOfChildren)

Here is my full sample code..

int noOfChildren = 10;

bounds.set(  topX,
                     topX + applicationPreference.getCircleSymbolSize()+                                applicationPreference.getLineLength() + joinerLineAllowance,
topY + (applicationPreference.getLineHeight() * (potential ? 1 : noOfChildren)) );

What does the code below mean..

(potential ? 1 : noOfChildren)

It means that if the variable potential is true then multiply
applicationPreference.getLineHeight() BY ONE

If the variable potential is false them multiply BY noOfChildren that is 10 (see I set it earlier..
applicationPreference.getLineHeight() BY TEN

This cuts the amount of code. Makes debugging easier. I think it is more likely to improve performance than not..



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: