Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Beautiful code - part 2 - top down design

Sign in to follow this  
Norman Barrows


Beautiful code - part 2 - top down design

an example of beautiful code.

this was a result of top down design. IE high level routines fist, lowest level routines last.

first, the algo was written in comments in english.

then the highest level code was written, defining the lower level routines and their APIs as it was written, before ever actually defining the lower level code. so the high level code determined that there would be a camera_can_see_target function, and that it would take the index of a target as a parameter. the actual camera_can_see_target function was written later. the high level code defined what the lower level APIs would be. the highest level code was written to be as english-like as possible.

this is the high level code that started this process (from SIMSpace 8.0):void draw_targets(){// for each target, if in vis rng and pass frustum cull, add to render queue// vis rng = 100x tgt size = 200x tgt.radint a;for (a=0; a
clarity, simplicity, and read-ability are hallmarks of beautiful code.
Sign in to follow this  


Recommended Comments

I'm not convinced this is beautiful code, and here's why:


int a;
for (a=0


Declaration and initialization on two separate lines is unnecessary in this case.  In fact, declaring "int a" there is completely unnecessary as that value will always be (MAXTGTS - 1) following the end of that loop unless one of those methods secretly modifies it.


a=0; a<MAXTGTS


Writing code like this becomes unreadable quick.


sstgt[a].active == 0


I'm not sure what sstgt means.  Also, you're iterating through an entire array of objects despite some of them (likely) being inactive, and thus, ignored.


if (sstgt[a].active == 0)
if (camera_can_see_tgt(a) == 0)


There's some duplicate code here.



I'm hoping that this method is nested inside a class.  Otherwise it means you've throwing around globals.  Though perhaps I'm missing something here.  Can you elaborate on your methods?

Share this comment

Link to comment

the code you see is output from Rockland's Cscript macro processor code generator.   the Cscript "source code" is much cleaner looking.


declaration and initialization the same line seems to be largely a matter of personal preference. however, declaring and init-ing things together (RAII type stuff) can help ensure you don't miss anything.


sstgt is a memory pool of entities - thus the check for active.

camera_can_see_tgt is the far clip and frustum cull of active entities before adding them to the render queue.

so you must check for both active and visible before adding to the render queue.


this particular example is procedural code using PODs, but could just as easily be done with OO syntax.


RE: globals

i started programming before the PC was even invented. back then, globals were common. but misuse of globals can cause problems. So globals are now frowned upon, in favor of explicitly passing all parameters to a function. using a global is the same as passing it by reference. so in every routine that references a global, you pretend its being passed in by reference, and code accordingly. but the dependency of that function on the global is not explicitly stated in the function declaration, which can lead to coders who are unaware of the hidden dependency making errors - thus the problem with globals - IE: that type of coding is not sufficiently self-documenting for coders unfamiliar with the code base. Having started out using globals, and having learned long ago how to deal with them, i still use them. I'm the only coder on the team, so i can get away with it. but its definitely not for everyone. the common practice today is to explicitly pass everything. That way everybody from the lead developer down to the newest programmer intern knows whats going on.

Share this comment

Link to comment

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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!