• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sliders_alpha

Static is evil, supposedly

16 posts in this topic

Hi,

So i'm a student in information systems, or to put it more simply java based website for corporation.
Recently I've taken quite some interest in making a minecraft clone, because I like learning new stuff and I want a better minecraft.

I'm working on the graphical engine and I noticed that using a static class containing[list]
[*]view distance
[*]Sector size
[*]installation paths
[*]an array of Texture (slick object)
[/list]
Could be really usefull, instead of having to pass down and down those data.

Now, what do you think? is this a good idea to do this? in my line of work static attributes or classes are considered as evil.
It messes up with our unit testing and mocking.

As my teacher say, if you want to do static classes go back to procedural languages, you are doing objects now.

But this is not a information system, so does these rules still apply?

thanks.
1

Share this post


Link to post
Share on other sites
"~=" denotes 'approximately':

static class ~= global instance of non-static class ~= #include ~= using ~= DLLImport

All of those give one or more functions another namespace. Clearly, none of those are inherently evil. In fact, they are all very useful. Edited by EJH
0

Share this post


Link to post
Share on other sites
There is nothing inherently evil about statics or globals. What is evil about them is letting static or global variables be changed anywhere in the code. The reason people run into problem is because they allow them to be changed anywhere. This leads to a debugging nightmare. If you have a bug you have no idea where it is coming from. So, allow the static to be read from anywhere, but only changed from one location and you will be fine.

I don't use statics that often or globals but there are times in which it is more effective and easier to read if you do.
2

Share this post


Link to post
Share on other sites
[quote name='TheTroll' timestamp='1342578715' post='4960299']
There is nothing inherently evil about statics or globals. What is evil about them is letting static or global variables be changed anywhere in the code. The reason people run into problem is because they allow them to be changed anywhere. This leads to a debugging nightmare. If you have a bug you have no idea where it is coming from. So, allow the static to be read from anywhere, but only changed from one location and you will be fine.

I don't use statics that often or globals but there are times in which it is more effective and easier to read if you do.
[/quote]That's one problem with global scope.

Another is that having global state means you can't have to copies of it in the same program. An well object should reasonably be usable in two isolated contexts without interference. That doesn't apply in every case, but it should be the default; don't limit the user of the code unnecessarily even if that user is you and you have everything planned out.
0

Share this post


Link to post
Share on other sites
[quote]why not have that as part of your current sector's information?[/quote]
because their is 5626 sector in RAM at all time, this would only add 5.626MB but I'm trying to reduce memory usage, every pieces count.


from what I read using statics the way I do it is "ok" since they are all defined as final, and thus never changing
.
[code]
public class Global {
//Sector size
public static final int CHUNK_Z = 16;
public static final int CHUNK_HEIGHT = 256;
public static final int CHUNK_X = 16;
//nb de display list par chunk
public static final int DL_Y = 16;
//windows parameter
public static final int WINDOW_HEIGHT = 480;
public static final int WINDOW_WIDTH = 640;
public static final int WINDOW_BITPERPIX = 32;
//paths
public static final String ROOT_DIRECTORIE = "C:\\MEngCl\\";
public static final String TEXTURES_PATH = ROOT_DIRECTORIE+"Textures\\";
public static final String WORLD_PATH = ROOT_DIRECTORIE+"World\\";
//display area
public static final short DISPLAY_AREA = 30;
}
[/code]

thanks for your awnsers ;) Edited by sliders_alpha
0

Share this post


Link to post
Share on other sites
[url="http://gbracha.blogspot.com/2008/02/cutting-out-static.html"]http://gbracha.blogspot.com/2008/02/cutting-out-static.html[/url]

Some relevant info, though mostly I agree with Frob's first answer.
0

Share this post


Link to post
Share on other sites
I've accidently this [url="http://beust.com/weblog/2011/03/10/rehabilitating-the-singleton-pattern/"]blog post[/url] and I think its awesome because it explains [static] Singletons, why they're considered evil and how to use them properly. Edited by demonkoryu
0

Share this post


Link to post
Share on other sites
[quote name='demonkoryu' timestamp='1343044300' post='4962209']
I've accidently this [url="http://beust.com/weblog/2011/03/10/rehabilitating-the-singleton-pattern/"]blog post[/url] and I think its awesome because it explains [static] Singletons, why they're considered evil and how to use them properly.
[/quote]

Shared state is shared state, even if it's private and scoped to a single class.
0

Share this post


Link to post
Share on other sites
[quote name='sliders_alpha' timestamp='1342592710' post='4960347']
[quote]why not have that as part of your current sector's information?[/quote]
because their is 5626 sector in RAM at all time, this would only add 5.626MB but I'm trying to reduce memory usage, every pieces count.


from what I read using statics the way I do it is "ok" since they are all defined as final, and thus never changing
.
[code]
public class Global {
//Sector size
public static final int CHUNK_Z = 16;
public static final int CHUNK_HEIGHT = 256;
public static final int CHUNK_X = 16;
//nb de display list par chunk
public static final int DL_Y = 16;
//windows parameter
public static final int WINDOW_HEIGHT = 480;
public static final int WINDOW_WIDTH = 640;
public static final int WINDOW_BITPERPIX = 32;
//paths
public static final String ROOT_DIRECTORIE = "C:\\MEngCl\\";
public static final String TEXTURES_PATH = ROOT_DIRECTORIE+"Textures\\";
public static final String WORLD_PATH = ROOT_DIRECTORIE+"World\\";
//display area
public static final short DISPLAY_AREA = 30;
}
[/code]

thanks for your awnsers ;)
[/quote]
I would agree that this use of static is ok, given the..... static nature of the data. I wouldn't complain about it. I couldn't see frob or telastyn really having a problem with it either.
0

Share this post


Link to post
Share on other sites
I would agree with Telastyn that shared state in the form of mutable static variables is best avoided unless it really makes sense to do things this way. On the other hand, I would advocate the use of static functions (that is, static behaviour as opposed to static state) wherever appropriate, especially with helper functions. If a method doesn't actually need anything that you need to access through the "this" pointer of the class in which the method resides, then you should make it a static method (or free function, if you were using C++, which it seems you aren't) until it does. The "this" pointer is an implicit dependency - making your method an instance method basically states, "I need an instance of this class in order to function correctly." If that statement does not apply to your method, then it should be static. Don't add dependencies to your code unless it's really needed. Of course, if you really need to access instance variables later on, then you should use the method with that in mind. Throwing around a static method in code that's unlikely to ever see an instance of its owner class opens you up to the same kinds of problems that singletons have, most importantly that doing this will increase the amount of interdependencies in your program. Edited by Oberon_Command
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0