I feel like this is bad practice but I'm not sure why

So this code below works, but I feel like maybe I should be doing something else to be more proper.

  if (s<=9){
    secs = "0" + s;
  } else {
    secs = "" + s;
  }

Really simply, all I’m doing is adding a 0 to the front of the int called from the second() function when its below or equal to 9 so it displays in right when I call to the canvas. However, when I want to set the else function I can’t call secs = s; because I get an error that you can’t convert an int to a string. To get around this I did secs = “” + s; and my question is, is this just automatically doing the conversion for me when I append an int to a string? Should I be doing secs = str(s); instead or is that basically what’s happening here?

I just feel like I’m using a weird syntax and maybe someone more experienced can explain if if this is cool or I should avoid doing something like this?

Thanks!

Although i couldn’t say exactly what’s going on because im not looking at the reference, as you say it seems that the conversion is happening automatically because you are including a string type by including “0”, and my guess is the compiler treats everything else after in the same way is a string type. Whereas without the inclusion of the quotation marks all it sees is your s variable which is an int, which needs to be cast to a string first and it has no other instruction to tell it otherwise. So your code looks adequate. Now I’m not sure about performance related issues with this approach, but as you’re only dealing with one var it should be fine.

So alternatively you can cast to string or you can pass it through another variable which itself is the cast version of the s variable. Though that seems a bit redundant if you have the cast to string method.

1 Like

See https://processing.org/reference/nf_.html

To replace the if

Also https://processing.org/reference/strconvert_.html

2 Likes

Hello,

See the reference:

There is an example in there similar to what you did.

:)

1 Like

secs = (s <= 9? "0" : "") + s;
secs = nf(s, 2);

" …and converts from primitive datatypes into the String datatype if necessary."

Nice! I wouldn’t have expected that to be on the addition page, but that answers my questions spot on and makes me feel more okay about not using str()

1 Like

I think I’ll still need the if statement being I only want to add the zero when the second value is less than 10, but I totally forgot about nf() until you mentioned this. That would also work in this situation. :slight_smile:

1 Like

Hello,

It is a good exercise to be able to write your own code for these simple tasks.

I have often written routines for the “helper” functions on my own; a habit from a time when I had to write all the code and these were not available.

If you want more detail look here in the source code for what is under the hood:
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java

Always include a bracket to make search simple such as:

dist(

:)

Can you clarify what you mean by this?

Is that what @GoToLoop was doing here as well with the colon? I wasn’t super sure with what was going on here either but assumed it was just another syntax for using a for loop that maybe was more optimized, but what do you mean by “making searching simple”?

I was referring to search here:

https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java

If I am looking for color() function for example:

A search for

color

finds 304 occurrences.

A search for

color(

finds 37 occurrences.

I often go into source code for stuff and have little tricks for searching quickly.

That is just one.

@GoToLoop was using the ?: (conditional) operator:

image

You will find it in the Processing references.

:)

1 Like