Chicken/Egg question in programming practice

This is for all level programmer, especially the vets. Do you put the function declarations on the top or the variables?

I asked this because I was confused for a moment when I looked inside a function I created due to having some DRY code based on it. And what confused me for that moment was a variable being called from within the function. So I was like, “Did I declare that variable?” And I realized I did create the variable under the 2 function declarations way in advance (when I started writing the code) and I’m like “maybe I should put the variable(s) above the function declarations or should I put the functions on the very top?”

What are your thoughts on this?

I don’t understand your question.

Same as @gunhoo93, I don’t understand the questions. Are you talking about JavaScript or some other language?

I’m asking about the language because for example:

  • in Pascal, you would have to put variable definitions before anything else, like function definitions etc;
  • in C, according to now obsolete ANSI and ISO-90 standard, it was an encouraged practice to declare all variables within function first and global variables straight after including libraries;
  • in Haskell, there’s no such thing as a global variable as due to their immutable nature, you can have evaluated constants or a named, unevaluated, placeholder;

In modern programming languages, it’s pretty much freeform but oftentimes you will find style guides that you are expected to follow. Some are accepted ‘worldwide’ (for example PEP-8 for Python, official Oracle guidelines for Java) while others can very well differ from company to company.

Does it touch your question in any way or have I indeed misunderstood you?


Your question is quite confusing, but maybe you mean where function / variable declarations should go? My personal preference is to declare variables when they make sense. Most of the time, I declare them just before I use them. That makes my code easier to read from top to bottom. (I used to declare ALL my variables at the top, and it confused me more often than not.). As for functions, I usually separate them as modules to make my code less cluttered.

Just try to give meaningful names to your declarations, and declare them when it makes sense. Hopefully, that lessens your confusion when tackling your own codebase :smiley:

1 Like

Is it good programming practice to have the variables declared before function declarations or vice versa?

It would seem like it doesn’t matter what is at the very top, but I was wondering.

With what you said, you have given me further insight about my question. By the way, it’s a question based on JavaScript. So I may just look further into some forms of style guides. I’ll even see how Colt executes the program after I’ve created my version.

That is what I meant, and then I gave an example in the second paragraph that may have been the confusion.

I think you are talking about something like this

function foo() {
    return a + b;

// Variables declared after function definition
var a, b;

In general, variables used within a function should stay local, as

function foo(a, b) {
    return a + b;

If you want to use global variable for some reason, what you are asking for doesn’t matter --although, it would be better if you place them to the top. This is because, roughly speaking, Javascript will handle declarations in advance to definitions.

function foo() {
    return a + b;

foo() // undefined + undefined returns NaN

var a = 1, b = 2;

foo() // 1 + 2 returns 3

Above code is treated as if it were

var foo, a, b;

foo  = function() {
    return a + b;

foo() // undefined + undefined returns NaN

a = 1;
b = 2;

foo() // 1 + 2 returns 3
1 Like

@gunhoo93 I see you understood what I was talking about (is that code from YDKJS?). I was about to put my actual code in for the visual perspective, and you beat me to it. Here’s my altered code (don’t want to give away answers to a tutorial I’m going through, read on). This is on top of my .js file (which is half of my code currently). I even realized that I have to create a flowChart to get a better visual step by step. Here’s the part code:

function questionEndUser() {
	endUserPlay = prompt("What level are you on?");

function addLevel() {
	var newTodo = prompt("You are now entering a new level");
	newTodo = nextLevelItem.push();

// empty list item declared
var newLevel;

// empty array
var endUserPlay = [];

Above is last nights coding (variables and functions have been changed to not give away answer to a tutorial). I’m now practicing within Colt Steele’s Web Developer Bootcamp and I don’t want to give away any form of answer to the application I’m working on now. This is great for others to find out for themselves and learn. I’m going through the process of creating the todoList app before Colt gives the answer to it. I first found out how it is executed, which is 1/5 of that current tutorial, and I’m now busy writing the code beforehand, as you can see.

In addition, I’m glad I was able to change the code up as if it is a creation for game programming (a thought to get into when I get better with Web Development).

That sounds like a plan, although I’m not at that level yet. Then again, I’m looking through MDN about that, as I googled modules functions mdn, here:

. Thanks for the heads up on that @alchermd.