• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Try to write code that is so clear in purpose that comments are redundant.

If you are considering writing a comment, consider if you could just pull the code out into a well named function, or if you could use variables to give names to the values you use.

Of course, you'll still need some comments, but far fewer. Most functions should not need comments.

Good advice. If you feel you need to comment something to explain why it works, then you are probably doing it wrong.

A very important form of comment though is to leave notes about "why" you changed something and when. That way somebody has a better chance of fixing what you broke (alway use somebody elses name of course).
 
Try to write code that is so clear in purpose that comments are redundant.

If you are considering writing a comment, consider if you could just pull the code out into a well named function, or if you could use variables to give names to the values you use.

Of course, you'll still need some comments, but far fewer. Most functions should not need comments.

Thanks for the tips. Thing is, I rarely need my comments because I pretty much do exactly that, which is why it's funny to me that I sometimes feel compelled to work on commenting my code. I generally only need them when I do things like use an int for the status of something, then I'll comment what each possible value means.

(I wonder how I'll be when I start coding with others...)
 
Created a github account after I had an issue where I lost a lot of work in an assignment earlier in the week after xcode crashed. Doesn't hurt to have backups, and it'll be good for resume to have random projects on there too. Also working my way around the terminal to push to github...

all to avoid studying for midterms, but I guess writing actual examples for the class and commiting them to github helps a bit..
 
Thanks for the tips. Thing is, I rarely need my comments because I pretty much do exactly that, which is why it's funny to me that I sometimes feel compelled to work on commenting my code. I generally only need them when I do things like use an int for the status of something, then I'll comment what each possible value means.

(I wonder how I'll be when I start coding with others...)

After you declare the int, do you also declare final variables to represent each status? Like final int STATUS_ON = 1;

Again this is more or less self documenting.
 

Phoenix

Member
An I a weirdo for over-commenting my code? I spend plenty of time writing Javadocs for my little game that nobody but me will see the source for, making sure they're clear and formatted correctly, and sometimes I think, "Who am I writing these for?" lol

I think I do it as a distraction from doing actual programming sometimes...

Your goal for commenting your code is so that you or someone else can come back 6 months from now and understand what the hell you did and why. Over commenting is far far better than the alternative.
 
An I a weirdo for over-commenting my code? I spend plenty of time writing Javadocs for my little game that nobody but me will see the source for, making sure they're clear and formatted correctly, and sometimes I think, "Who am I writing these for?" lol

I think I do it as a distraction from doing actual programming sometimes...

It's alright, comment as much as you want. Programming languages are first made for humans, not machines (Only a deranged person would think about compiling stuff like for (;P("\n"),R-;P("|"))for(e=3DC;e-;P("_"+(*u++/8)%2))P("|"+(*u/4)%2);)

Also, if you don't understand how your program works just by reading its comments, you might as well throw it away.

Try to write code that is so clear in purpose that comments are redundant.

If you are considering writing a comment, consider if you could just pull the code out into a well named function, or if you could use variables to give names to the values you use.

Of course, you'll still need some comments, but far fewer. Most functions should not need comments.
I hear that all the time, but don't overestimate other people's ability at understanding your code. As the creator of your program, you've spent all your time reading and writing its code. Other people will see the code for the first time so it will be complicated for them to understand it, even with well-named variables and methods.
 
Thanks for the tips. Thing is, I rarely need my comments because I pretty much do exactly that, which is why it's funny to me that I sometimes feel compelled to work on commenting my code. I generally only need them when I do things like use an int for the status of something, then I'll comment what each possible value means.

(I wonder how I'll be when I start coding with others...)

I use enums for that sort of situation.
Atleast that what they teached us to use. Have no idea the real world does that too.
 
Comments do not compile, comments do not run. They grow stale. Code changes, and the comment does not. Never trust a comment to tell you what the code is doing.

If you want readable code that is easy to understand, write readable code that is easy to understand.
 

usea

Member
Comments do not compile, comments do not run. They grow stale. Code changes, and the comment does not. Never trust a comment to tell you what the code is doing.

If you want readable code that is easy to understand, write readable code that is easy to understand.
Agreed. IMO you should treat comments like "this is what the author of this code intended it to do the first time they wrote it." That's often different from what it actually did, not to mention what it actually does a year later after 5 changes.

The main places I put comments are
1) On a line of code where a reader might initially go "why?" Just to explain something that might not be immediately obvious. Ideally you minimize these situations, but sometimes code doesn't do what you think it will at first glance.
2) Public methods in an API/module will get some kind of blurb about arguments, return values, behavior, expectations etc. Usually 1-4 sentences. This is more for intellisense than readability.

You should never, ever read comments instead of code. They're for clarification and documentation. But they should be taken with a grain of salt.
 

Slavik81

Member
I hear that all the time, but don't overestimate other people's ability at understanding your code. As the creator of your program, you've spent all your time reading and writing its code. Other people will see the code for the first time so it will be complicated for them to understand it, even with well-named variables and methods.
I don't work alone. I'm on a team with 5 other programmers and we all follow this philosophy.
 

Certinty

Member
Python an easy programming language for beginners?

One of my exams in the summer is all to do with it and I've barely started with it as I'm too busy with other parts of my studies.
 

Osiris

I permanently banned my 6 year old daughter from using the PS4 for mistakenly sending grief reports as it's too hard to watch or talk to her
I try to live by the rule of avoiding commenting the implementation of my code, only it's intention, and the only time I ever comment the implementation is when its an ugly kludge that I wouldn't in a million years consider as acceptable without a very good reason.

And those "very good reasons" can pop up when dealing with external API's or 3rd party additions to a system, better to comment the reason behind using them than to revisit the code later, recognize the ugly kludge for what it is and waste time trying to engineer a better solution that may not exist, if you've forgotten the reason you did it the way you did in the first place.
 

pompidu

Member
I'm also trying to learn Android to make a game. From what I found, the official developer site is the best resource to learn. There was not a single book that stood out as being better than the official docs, not to mention a lot of books are already outdated.

I found the following helpful:
http://developer.android.com/training/notepad/index.html

Thanks Il check it out. Things I want to do specifically dont show up in any sites or tutorials so i have to kinda feel it out to get things to work.
 

Fantastical

Death Prophet
Ah... I was hoping for an internship with Garmin, but I didn't get it. :( Is it too late to try and find software engineering internships for summer 2012?
 
Is it too late to try and find software engineering internships for summer 2012?

Yeah, like a year too late ;)

I am just installing Visual Studio because I thought that making some small game in C# could be a nice learning experience and I need some points so I can graduate in the summer.
 
i'm just beginning to program, essentially, and can already tell debugging is going to take a huge chunk of my life. are there any tips, guides, or books any of you would recommend to minimize this? maybe some strategies to debug efficiently? planning the program out on paper seems to help, but that's rather common.
 
i'm just beginning to program, essentially, and can already tell debugging is going to take a huge chunk of my life. are there any tips, guides, or books any of you would recommend to minimize this? maybe some strategies to debug efficiently?
Ideally, you should write unit tests to minimize debugging.

Should you need to debug though, experience is all that'll speed up the process as you'll learn what the common causes of whatever exception popped up are.
 

Kalnos

Banned
@that1dude24

Learning to use the debugging features of your IDE (breakpoints, step over, step into, etc) will save you quite a bit of time. If that isn't an option using the console to display expected/actual values helps me a lot.
 
i'm just beginning to program, essentially, and can already tell debugging is going to take a huge chunk of my life. are there any tips, guides, or books any of you would recommend to minimize this? maybe some strategies to debug efficiently? planning the program out on paper seems to help, but that's rather common.

Experience helps like mentioned above. However I also find it helps to printf/cout/print EVERYTHING. Going into a loop? Print before and after. Passing to a function/method? Print before the call and in the function/method.

http://24.media.tumblr.com/575fb8db04082cff97656345f4470d66/tumblr_mjdr666Y4G1s0ykkso1_500.png

Something simple dealing with threading, making sure the thread was initialised and created, that the thread successfully executed in the function, that the value was properly passed with the thread, and that the for loop was doing the correct thing, then making sure the value was set once the thread joined with the regular process.

http://24.media.tumblr.com/37a371fd89efdfe924f67f8635952485/tumblr_mjdr666Y4G1s0ykkso2_400.png

Maybe a bit contrived but it certainly helps spot where things may go awry

[I have the code here somewhere. I use tumblr as a personal miniblog where I post random stuff like this so I figured this was easier and quicker]
 

gcubed

Member
Thanks to the people who pointed me towards edx.org for learning Python. The 6.00x course is fantastic (and a lot more work then I anticipated)
 

Minamu

Member
Is anyone in here familiar with UnrealScript? I'm trying to figure out how to make a flashlight but I'm not sure which environment I'm supposed to work in (youtube suggest visual studio). I can't find any obvious newbie tutorials about the basic stuff.
 
thanks for the input everyone. i already print out values a lot to see if a function works as intended etc, but i can probably get in the habit of doing it in more places. i'll get on working more closely with the codeblocks debugger, too.
 

upandaway

Member
Man I get the intense feeling that I'm picking up bad habits by writing stuff with pure self studying. Not sure if I should continue or just wait until I get some more books.. which would waste a good couple of months.

I (think I) am doing unit tests as well as I can but who knows if I'm being efficient at it, since I basically just test every method immediately anyway. And debugging is still completely alien to me. Also, no comments anywhere and a million assumptions in the code, with no safeguards against worst case scenarios. That can't be healthy.
 
Hi!
I have a question, I'm currently making a C# application for drawing L-Systems (fractals). My initial approach was to have an abstract L-System class that would be implemented by the actual L-Systems (Koch curve, Fractal plant etc.). I've started to dislike that design and my idea now is to have one L-System class that has a set of rules, a current state string and a dictionary that maps each character to a callable action. The last one is where I'm stuck currently. The way it should work is that there is a Draw method which looks at each character of the current state string and calls a method (like draw 5 pixels forward, rotate by 45° or something like that) depending on what the current character is. In semi-pseudo-code it should look like that:
Code:
foreach(char c in currentState)
{
    Action a = CharacterActionMap[c]
    a.Invoke()
}

Now, how do I set up the Dictionary with a char as an index and some sort of callable action as value?
 

tokkun

Member
I (think I) am doing unit tests as well as I can but who knows if I'm being efficient at it, since I basically just test every method immediately anyway.

The fact that you are writing unit tests at all is a pretty good step, absent of the question of how efficient they are. It's probably one of the most best practices, especially in personal projects.
 
Hi!
I have a question, I'm currently making a C# application for drawing L-Systems (fractals). My initial approach was to have an abstract L-System class that would be implemented by the actual L-Systems (Koch curve, Fractal plant etc.). I've started to dislike that design and my idea now is to have one L-System class that has a set of rules, a current state string and a dictionary that maps each character to a callable action. The last one is where I'm stuck currently. The way it should work is that there is a Draw method which looks at each character of the current state string and calls a method (like draw 5 pixels forward, rotate by 45° or something like that) depending on what the current character is. In semi-pseudo-code it should look like that:
Code:
foreach(char c in currentState)
{
    Action a = CharacterActionMap[c]
    a.Invoke()
}

Now, how do I set up the Dictionary with a char as an index and some sort of callable action as value?

I have 0 indepth knowledge of C#, so take this with a grain of salt but it sounds like what you want is a dictionary mapping characters to delegates. A delegate will let you define a method template that a method must meet, and then to pass it around like an object or invoke it when you want. Example:

Code:
using System;
using System.Collections.Generic;

namespace Tester
{
    class Example
    {
        //bolded for emphasis
        [B]delegate void StringMethod(string word); [/B]

        static void Main(string[] args)
        {
            Example example = new Example();

            Dictionary<char, StringMethod> dict = new Dictionary<char, StringMethod>();
            dict.Add('a', example.identityString);
            dict.Add('b', example.reverseString);

            String codedString = "ababa";

            foreach (char c in codedString)
            {
                StringMethod action = dict[c];
                action("Hello");
            }
        }

        void identityString(string word)
        {
            Console.WriteLine(word);
        }

        void reverseString(string word)
        {
            for (int i = word.Length - 1; i >= 0; --i)
            {
                Console.Write(word[i]);
            }
            Console.WriteLine();
        }
    }
}

Will print:

Code:
Hello
olleH
Hello
olleH
Hello

If that isn't exactly what you need, you could look into using an interface that defines a Do() method or similar and use a dict mapping characters to a set of objects that implement that interface. I think the delegate approach is cleaner for what you need though.

Also, L-Systems are sweet, here's an implementation I did for 2d trees in JavaScript to test out the canvas. Don't look at the code it's a mess.
 

Tenck

Member
So what's a good language to help me do flash cards online? Kind of want to do what Anki does, but I'd like to have full control on how everything works and it'll be a starting point to a new programming language.
 
Hi!
I have a question, I'm currently making a C# application for drawing L-Systems (fractals). My initial approach was to have an abstract L-System class that would be implemented by the actual L-Systems (Koch curve, Fractal plant etc.). I've started to dislike that design and my idea now is to have one L-System class that has a set of rules, a current state string and a dictionary that maps each character to a callable action. The last one is where I'm stuck currently. The way it should work is that there is a Draw method which looks at each character of the current state string and calls a method (like draw 5 pixels forward, rotate by 45° or something like that) depending on what the current character is. In semi-pseudo-code it should look like that:
Code:
foreach(char c in currentState)
{
    Action a = CharacterActionMap[c]
    a.Invoke()
}

Now, how do I set up the Dictionary with a char as an index and some sort of callable action as value?

Should just be this:

Code:
Dictionary<char, Action> dic = new Dictionary<char, Action>();

Then when you call, for example. dic["c"], you can do something like this:

Code:
dic[key].Invoke();

Is that what you where going for? You may need to do some casting with the Action after getting it from the dictionary.
 

Milchjon

Member
Is there a reason why the syntax of HTML and CSS are so different from one another? I mean CSS is only used in combination with HTML, right?
 

The Real Abed

Perma-Junior
Is there a reason why the syntax of HTML and CSS are so different from one another? I mean CSS is only used in combination with HTML, right?
CSS is more like code or scripting. HTML is markup.

The HTML creates the basic frame of the page. CSS tells it how it should look.

Or something.
 

Nitsuj23

Member
Do people go back to school for a second Bachelor's degree if they want to transition to software development?

I'll be 23 in April and graduate in May with a BA in Accounting. My school (Michigan State) offers specializations that are similar to minors. Last year I realized I do not want to pursue a career as a CPA, so I added the IT specialization as a way to get something tech related on my degree. I'm starting to think that's not enough. The specialization allowed me to take a Python course, but that's the only real technical class I was exposed to. I really enjoyed it, though. My end goal is mobile software development, specifically iOS or Android.

I guess what I'm asking is should I go back to school for a Bachelors or Masters in CS? Does my undergrad being Accounting put me at a severe disadvantage trying to do my Masters in CS? Bootcamp type of programs are becoming popular, Develop Detroit (Devdet.com) is a three month iOS course that I would love to do but it says "some software dev experience" is required. I should just buckle down and get the degree, I just don't if it's worth it to go to school another four years after just putting in five -- especially since I doubt Accounting credits transfer to CS...

Sorry for being brief it just feels like there's so much I could write. I basically feel like I wasted a lot of time and money the last five years by getting a degree in a subject I care nothing about. Kind of depressing. I just wish some of what I've done over the last five years was applicable to a CS degree. I could go back two years, but four is extremely daunting.
 
So what's a good language to help me do flash cards online? Kind of want to do what Anki does, but I'd like to have full control on how everything works and it'll be a starting point to a new programming language.

You want a programming language that has web application support. I would go with either Java which uses Applets or C# which uses Silverlight. Either one will work great.
 

Kalnos

Banned
I really enjoyed it, though. My end goal is mobile software development, specifically iOS or Android.

You don't need a degree to get a software development job but it certainly won't hurt your chances. Experience is very valuable, so much so that learning on your own and building a resume of mobile applications would be very valuable.

I'm not confident enough to offer you an answer of whether it's worth it or not to get another Bachelor's in CS... there are just too many undefined variables to consider: time, loans, responsibilities, etc. What I can say is that your math background from your accounting degree should be handy.
 
"Hey, what if we made this application 1040/900 resolution and made it impossible to scale in any direction without making it look like shit!" - the developer whose project I took over.
 
I have 0 indepth knowledge of C#, so take this with a grain of salt but it sounds like what you want is a dictionary mapping characters to delegates. A delegate will let you define a method template that a method must meet, and then to pass it around like an object or invoke it when you want. Example:

Code:
<snip>

Will print:

Code:
Hello
olleH
Hello
olleH
Hello

If that isn't exactly what you need, you could look into using an interface that defines a Do() method or similar and use a dict mapping characters to a set of objects that implement that interface. I think the delegate approach is cleaner for what you need though.

Also, L-Systems are sweet, here's an implementation I did for 2d trees in JavaScript to test out the canvas. Don't look at the code it's a mess.

Should just be this:

Code:
Dictionary<char, Action> dic = new Dictionary<char, Action>();

Then when you call, for example. dic["c"], you can do something like this:

Code:
dic[key].Invoke();

Is that what you where going for? You may need to do some casting with the Action after getting it from the dictionary.

Thanks for the answers, I figured it out (with help from a friend) in the meantime. My problem was to fill the Dictionary with suitable function pointers as values, which I first did like that:

Code:
private void moveForward()
{
    turtle.forward(15);
}

[...]

Dictionary<char, Action> map = new Dictionary<char, Action>();
map['F'] = moveForward;
map['-'] = goLeft;
map['+'] = goRight;
With the code for moveForward, goLeft etc. written out as whole methods. I later found out that you can do the same more elegantly with lambdas and added a parameter to the Action as well.
Code:
Dictionary<char, Action<TurtleDrawing>> map = new Dictionary<char, Action<TurtleDrawing>>();
map['F'] = s => s.Forward(15);
map['-'] = s => s.Left();
map['+'] = s => s.Right();

C# is pretty cool.
 

Zoe

Member
Do people go back to school for a second Bachelor's degree if they want to transition to software development?

I'll be 23 in April and graduate in May with a BA in Accounting. My school (Michigan State) offers specializations that are similar to minors. Last year I realized I do not want to pursue a career as a CPA, so I added the IT specialization as a way to get something tech related on my degree. I'm starting to think that's not enough. The specialization allowed me to take a Python course, but that's the only real technical class I was exposed to. I really enjoyed it, though. My end goal is mobile software development, specifically iOS or Android.

I guess what I'm asking is should I go back to school for a Bachelors or Masters in CS? Does my undergrad being Accounting put me at a severe disadvantage trying to do my Masters in CS? Bootcamp type of programs are becoming popular, Develop Detroit (Devdet.com) is a three month iOS course that I would love to do but it says "some software dev experience" is required. I should just buckle down and get the degree, I just don't if it's worth it to go to school another four years after just putting in five -- especially since I doubt Accounting credits transfer to CS...

Sorry for being brief it just feels like there's so much I could write. I basically feel like I wasted a lot of time and money the last five years by getting a degree in a subject I care nothing about. Kind of depressing. I just wish some of what I've done over the last five years was applicable to a CS degree. I could go back two years, but four is extremely daunting.

I don't think any graduate CS program will accept you without a CS or EE bachelors.

It's possible to start a career without a CS degree, but you need a portfolio to back you up.
 
I don't think any graduate CS program will accept you without a CS or EE bachelors.

It's possible to start a career without a CS degree, but you need a portfolio to back you up.

They will. You'll just be taking undergrad cs classes for a year or two.

Mind you, you need a really good gre and undergrad gpa to be accepted in those conditions
 
Top Bottom