# Should functions go before or after setup() and draw()?

**URL:** <https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793>\
**Category:** Coding Questions\
**Created:** [August 20, 2018, 3:51pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793 "2018-08-20T15:51:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [August 20, 2018, 3:51pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/1 "2018-08-20T15:51:54Z")

</div>

I know that it doesn’t make any difference as to how the program is run, but is it good practice to put functions you have declared before or after setup() and draw(), or is it just personal preference?

- Does it make a difference if you are using other processing functions like keyPressed()? (and where should they go)

- Does it depend on where the function is called from?

Up until now I have been putting all my functions after setup() and draw() as I feel that it can make it easier to read setup() and draw() without having to scroll past loads of functions, is this approach right?  
Even if it is just opinion, I would like to hear what other people do, and why.

Thank you in advance,

The Wizard Bear

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 20, 2018, 4:57pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/2 "2018-08-20T16:57:59Z")

</div>

This is going to come down to personal preference. Do whichever one makes the most sense to you.

If you’re working on a team, then you should come up with a standard that everybody follows. But it sounds like you’re just working alone, so stick with whatever you prefer.

You could find somebody else’s convention and adopt that. Googling something like “java conventions private function placement” returns a ton of results, including:

- [Are there any Java method ordering conventions?](https://stackoverflow.com/q/4668218/873165)
- [Best practice: ordering of public/protected/private within the class definition?](https://stackoverflow.com/q/1760850/873165)
- [Official Java Code Conventions Doc](https://www.oracle.com/technetwork/java/codeconventions-150003.pdf)

Long story short, grouping the Processing functions like `setup()`, `draw()`, and `mousePressed()` first is fine. Another approach would be to group related functions together, for example if you called a function from inside `setup()`, it might make sense to put that right beneath `setup()` before `draw()`.

But in the end it’s completely up to you.

There are other conventions that are much more important to follow, such as giving your functions and variables good names and using proper capitalization.

---

<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:** [August 20, 2018, 4:58pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/3 "2018-08-20T16:58:54Z")

</div>

> [@TheWizardBear](#):
>
> Up until now I have been putting all my functions after setup() and draw() as I feel that it can make it easier to read setup() and draw() without having to scroll past loads of functions, is this approach right?

There is no “right” approach but I tend to do this and most contributors to the forum seem do the same. I also tend to group common functions together e.g. event handling functions.

For larger programs you might consider having multiple tabs with common functionality in each, just name the tabs to describe the functionality in the tab.

---

<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:** [August 20, 2018, 5:00pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/4 "2018-08-20T17:00:52Z")

</div>

> [@Kevin](#):
>
> There are other conventions that are much more important to follow, such as giving your functions and variables good names and using proper capitalization.

Also white space (tabs and blank lines) to highlight the program structure

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [August 20, 2018, 5:06pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/5 "2018-08-20T17:06:41Z")

</div>

Thanks @Kevin for the advice.  
I shall definitely check out some of those links and look into it further.

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [August 20, 2018, 5:08pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/6 "2018-08-20T17:08:02Z")

</div>

Thank you @quark for the reply and helping me out!

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 20, 2018, 10:27pm UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/7 "2018-08-20T22:27:33Z")

</div>

Both @Kevin and @quark both gave awesome responses, and I’d like to just add, it does have lots to do with preference and background. In C++ there is “forward declaration” meaning you have to put your functions first, just to keep the compiler happy. In most languages though, I would be bold enough to say that where you put it doesn’t necessarily matter 😁

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [August 21, 2018, 7:25am UTC](https://discourse.processing.org/t/should-functions-go-before-or-after-setup-and-draw/2793/8 "2018-08-21T07:25:10Z")

</div>

Thanks @EnhancedLoop7! 😁
