🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Do you write pseudo-code/comments before coding?

Started by
20 comments, last by Waterlimon 8 years, 4 months ago

Lately I've been reading a lot of advice regarding coding structure that suggested to draw class interactions and write pseudo-code or comments explaining what you're going to do before starting actual coding. Since it's something I've never personally found useful, I'm kind of curious to know if there's a common preference here.

To explain better what I mean, I once worked with a guy who wrote whole functions in comments before writing the actual code, like in the sample below:


int fibonacci(int x) {
    // If x < 0 throw an exception
    // If x is 0 or 1 return 1
    // Return sum of fibonacci(x-1) and fibonacci(x-2)
}

His comments were probably less code-like than mine, though.

I understand that we people are wired differently from one another and for some it's easier to think first in a more natural language and then translate it to code and others think better in code than in natural language, but I want to ask, do you guys use this kind of techniques? Why do you find them useful or not useful?

Advertisement

I do this sometimes when scaffolding code and looking for the right abstractions on which to base my components, where the action to be performed is too complicated, or requires objects and state that I haven't defined yet. If I just need to increment a counter then I'll just increment it there, no need to write the entire function in comments.

I find that approaching design with just diagrams often leaves me with one or two major architectural issues that were dependent on the implementation, underlying hardware or something similar that the diagrams could not reveal, so it's often good to give your ideas a test run by trying to implement them at increasingly deep levels of detail, and seeing if you reach a point that makes you go "crap, I hadn't thought of that".

In my experience, no software design survives contact with the real world, so I may as well look for what I overlooked as soon as possible.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

No, i don't write pseudo code.
However I implement something that baerly runs very quickly, and after that I reason about the API and the code that I've just wrote.
This is my philosophy.

No, i don't write pseudo code.
However I implement something that baerly runs very quickly, and after that I reason about the API and the code that I've just wrote.
This is my philosophy.

I can only support this method. Doing a quick and dirty solution first gives you a better feel of the problem to solve. You'll see what exceptions may be coming up and you can easily break down your problem into smaller ones.

Then you can start to define your API and refactor your code.

Follow my hobby projects:

Ognarion Commander (Java/LIBGDX): https://github.com/OlafVanSchlacht/ognarion-commander

I use is quite often when doing more complex stuff. Either pseudocode refinement (start with rough blocking out, then going more in-depth and finally converting part for part into real code) or in-advance pseudocode of the whole code I want to develop.

The advantage is, that you can quickly identify issues. Modifying pseudo code is better than refactoring real code due to some issues you identify too late.

This type of precursory action helps me in no way. I have to think about things in my own mental language to imagine and write clean correct code.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


I once worked with a guy who wrote whole functions in comments before writing the actual code, like in the sample below

I've seen it, and there is only one thing I dislike about it when I see it. Usually I'm grateful for it.

I've seen many times where it makes sense to document the algorithm being used, or to provide an easy-to-read description of something that may not be obvious, or occasionally to state something like "This is an implementation of such-and-such paper from SIGGRAPH, a copy should be in this folder."

That last one is particularly nice. I may look at a function and have no idea initially what it is doing, but the link to the research paper with a good description of the algorithm and the reason for the choice, that helps a ton.

I've seen comments like that noting that the code was like that as a hacky fix to bug 4723. I've seen comments that apologized to everyone in the future for the implementation but the designer and producer is firm that it must be a certain way.

I've seen comments with a warning that doing it some simpler way doesn't work for certain reasons. I've seen comments warning that before changes were made to a function developers should check with the team lead (or former team lead) and to profile before and after due to performance concerns.

And I've written a comments like all of those above. When the comment relates to other parts of code, often I'll put the date on the comment so the maintainer can figure out how fresh or stale the comment is.

Many times the comments are useful. Remember that while the code needs to work for the computer, it needs to be understood to everyone who comes by later (including your future self) who maintains or extends the code. What may seem obvious to you when you are writing the code may not be obvious to a maintainer who is trying to change it months later. Sometimes that person is yourself, thinking "I remember this was needed for a good reason, but I don't remember exactly why."

I don't have a problem with it as long as the comment matches the code.

The moment they get out of sync, that's when I don't like it. And unfortunately, it is easy for the two to fall out of sync.

I'll often spell out a set of tricky if conditions as comments first, then fill in the code.

// If we foo has bar, then we need to do this

// else we need to add bizzbuzz

I then go and replace those with code, sometimes I keep a comment on the if, if it's not readily apparent. If it's some chained clauses, it can be easier to read the comment to understand what is being tested for.

It depends on the complexity of the function/ method (in my case).
Sometimes I write out/ draw out or pre-calculate stuff using notepad and or excel.
All my function implementations contain a "comment header" describing always 2 things: what does the function do and what does it return.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I rarely use pseudocode for writing code. Typically, I diagram things out at a coarse level and jot down requirements, both project-driven and personal, for the code. Then I write a minimal working solution that fits the requirements and uses the diagram and iterate from there.

Where I do use pseudocode a lot is in debugging, or changing the way existing code works. I'll often reduce the code down to pseudocode to try to reason about the flow of a program, particularly if that flow is really complicated.

This topic is closed to new replies.

Advertisement