Examining Coding Style Conventions

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 3305 Views - Last Post: 29 July 2010 - 02:09 PM

#1 Guest_Statement*


Reputation:

Examining Coding Style Conventions

Posted 27 July 2010 - 11:19 AM

Hello world!

First of all, I have read your rules. I have a request that seems to conflict with;
"We will not do your homework for you! Do not ask us to give you code!"

I actually want to do just this. I want to ask someone to give me a bit of code.

Before I actually write the request for this suggested simple project, I want to see if the moderators and community members approve this, since it's a borderline rule break. I certainly don't want to break a rule on my first post.

It's a bit special, the code that I am looking for is not meant to be used commercially, home school assignments, job interview etc. I have a blog idea where I dissect some piece of code and analyze its software design for public discussion to allow people to think deeper about software design and reflect on code. The poster can be anonymous if they like. (More info about this in a moment).

The reason I come to a community such as dreamincode.com is because I want to get in touch with other people, preferably people who don't stamp labels even on a request as this, even if it seems to contradict the rules of that very forum. I hope you understand that this request is not about being lazy, finding shortcuts or sucking off your work. It is about further educating yourself, seeing code with another perspective and discuss strengths/weaknesses from a design perspective.

The reason I am not writing the code myself is that I have been learning programming and software design, and such I see code differently than when I first started coding many years ago. I want to obtain code from a scholars point of view, like I would have written it those years back. Clearly, I now design my code very differently than back then and I am curious to the differences from these two mindsets. I have tried searching for some sample code that would suit my needs but it's quite hard to find specific code.

So, what I would like then is someone to exchange ideas with. I would propose a small project which you then would deliver a solution for. The project shouldn't take long to complete, most likely you'd be done in an hour or so. No special case has to be considered about the way you write your code. In fact, it would be best if you paid no special attention to your style at all. Just write code with your personal style. Then I would review the code and analyze it with consideration to certain aspects of the code. We can discuss these aspects privately, so that hopefully we learn a bit from each others vantage points. Some content will be shared publicly on my blog so other people can think about the problem, but you can remain anonymous if you like.

So, what do you think? Ridiculous suggestion? Awesome chance to exchange ideas?
If moderators and community seem to approve, I'll follow up a post in this thread about details of the project.

Thank you very much for taking your time to read through this. :)

Is This A Good Question/Topic? 0

Replies To: Examining Coding Style Conventions

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 11:25 AM

I think I'll move this into the Corner Cubicle.

It's fine if your purpose is to analyze and discuss stylistic conventions (title renamed to reflect such), but please keep all discussion via the forums. That way, everyone can benefit from the discussion. :)
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Statement*


Reputation:

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 11:33 AM

View Postmacosxnerd101, on 27 July 2010 - 10:25 AM, said:

I think I'll move this into the Corner Cubicle.

It's fine if your purpose is to analyze and discuss stylistic conventions (title renamed to reflect such), but please keep all discussion via the forums. That way, everyone can benefit from the discussion. :)

But I don't have 10 posts yet. And I am not really looking especially for "trained" coders. I actually want coders who are just starting out and have basic programming skills, or people who recently completed education. Of course, this project isn't closed to anyone but my interest lie in people who haven't yet thought too much about the way code could be structured.

It's fine, we can keep discussion here. I do however want to post a blog about the result of it.
It seems I posted as a guest, I'll setup an account ASAP.

Thanks for understanding my request! :)
Was This Post Helpful? 0

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 11:34 AM

This sounds somewhat interesting, but you haven't said anything concrete yet. Can you expand on your idea?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 11:37 AM

If you're referring to the use of the PM system, you only need one post. While you may be interested in new programmers, there are a lot of members with varying levels of experience that can provide well-thought out insights and comments that will add to the topic. You are welcome to analyze the code posted here constructively, but I think this could make for a very interesting discussion rather than just as an experiment/project. If you would prefer, I can move this into Share Your Project, but I think you'll get more discussion here. :)
Was This Post Helpful? 0
  • +
  • -

#6 Statement  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 27-July 10

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 12:09 PM

Project idea: Simple console application-based menu in C#.
Other languages are accepted for discussion among this community but I handle C# in my blog and as such, I would prefer C# solutions.

Consider you have been given the task being a member of a small development team. The goal of the team is to create a fun text based game for retro-loving gamers. Your task is to create a functional console-based menu, as instructed by your team manager.

The rest of the team is well underway of creating other parts of the game and you had the roll of the dice to implement the menu system for this game. The game is designed so that you will have a clear console window with no other code running while your menu has control (i.e. there are no threads or anything that can influence your code behavior). This sets an ideal starting ground for you, as you don't really have to pay much attention to the other components in the game.

Your task is to create a menu class, that is traversable with up and down arrows. Enter key is used to perform the action of the selected command in the menu. If the user press up while being at the topmost selection, the bottom selection is marked (it's a cyclic menu) There are three known cases for this menu;

"Play", "Credits", "Exit".

The menu text should be positioned to start at position 0, 0 in the console, for now. The layout of the menu is so that the currently selected command is prefixed with " > ". An example of what the menu should look like in action follows (plaintext):

   Play
 > Credits
   Exit


Each of the commands should be implemented by the game class. For the purpose of this example, feel free to create a class named Game, that handle the actions of menu commands. You're free to design it any way you see fit, but you don't need to do more than to implement basic methods that prints "Playing Game...", "Created by ME!", "Exiting game...", to verify that the menu actually work as intended.

Your work might be yet another text menu, but it is after all the first impression the user get. In the application, make sure that your menu is run first, and that after printing the credits control is resumed to the menu. In the cases of Play or Exit, it is ok if the program terminate.

You set in motion to create this basic menu with aspirations that your manager will be pleased.

Quick summary of features:
Text console application.
Three options; "Play", "Credits", "Exit".
Game should respond to each command with expected results.
Menu should resume control after printing credits.
Menu is navigable with up and down arrow, wrapping around edges.
Menu command is activated by enter key.
Menu is displayed starting at 0, 0.
Menu should prefix " > " to the selected item (indent the other choices appropriately).
Menu is started as application start.

Good luck!

This post has been edited by Statement: 27 July 2010 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 12:10 PM

I'd probably be willing to participate in this.

Edit: My background may not fit your study. I'm mostly self educated, but I've been professionally developing for a while now.

It is a good idea to keep the discussion public though. The whole idea behind forums is so that everyone who wants to can gain benefit from the ongoing discussion. I wouldn't want to participate if you were just recruiting volunteers to give you something to post on your blog. But if you're willing to give the community something, I'd be willing to give my time.

This post has been edited by insertAlias: 27 July 2010 - 12:13 PM

Was This Post Helpful? 2
  • +
  • -

#8 Statement  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 27-July 10

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 12:29 PM

You're welcome to join the discussion and contribute with your code!

You're right about forums, they are intended to be a public discussion and should be kept as such. The original idea for private conversations was that I wanted each participant to get feedback without "leaking" solutions too early so other people adopt those in their designs if they didn't plan to in the first place. I'd really wish that participating members try to solve the problem on their own hand before turning to the board to discuss their implementation. The reason is simply that I wish to avoid improvement influence this early in the discussion.

The next phase would be then to open up the implementation discussion in general, with multiple reviewers and contributors working together to analyze the produced code if they want, and this is where people can reflect over their code, to discuss improvements/benefits/drawbacks. These discussions tend to be "no one is right for every case", so it's nice to see different points of view.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 12:34 PM

Agree about the solution, but those who wish to participate in good faith will do so. :)
Was This Post Helpful? 0
  • +
  • -

#10 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 08:46 PM

Is there a specific version of .Net we should be targeting if we decide to attempt this? 3.5? 4.0?
Was This Post Helpful? 0
  • +
  • -

#11 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Examining Coding Style Conventions

Posted 27 July 2010 - 10:00 PM

Well, here's my go at this. Don't be too harsh! :gun_bandana:

The Main Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Menu_System
{
    class Program
    {
        static void Main(string[] args)
        {
            Start();
        }

        private static void Start()
        {
            Menu menu = new Menu();
            bool isRunning = true;

            do
            {
                Console.Clear();
                Console.WriteLine(menu.ToString());
                ConsoleKeyInfo input = Console.ReadKey();

                if (input.Key.ToString() == "DownArrow")
                {
                    menu.Increment();
                }
                else if (input.Key.ToString() == "UpArrow")
                {
                    menu.Decrement();
                }
                else if (input.Key.ToString() == "Enter")
                {
                    Console.WriteLine(menu.runCommand(menu.Choice));
                    Console.WriteLine("\nPress <Enter> To Continue\n");
                    Console.Read();

                    if (menu.Choice == 0 || menu.Choice == 2)
                    {
                        isRunning = false;
                    }
                }

            } while (isRunning);
        }
    }
}



The Menu Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Menu_System
{
    class Menu
    {
        private List<string> Options = new List<string>();
        public int Choice { get; private set; }

        public Menu()
        {
            this.Choice = 0;

            Options.Add("PLAY");
            Options.Add("CREDITS");
            Options.Add("EXIT");
        }

        public void Increment()
        {
            if (this.Choice < 2) this.Choice++;
            else this.Choice = 0;
        }

        public void Decrement()
        {
            if (this.Choice > 0) this.Choice--;
            else this.Choice = 2;
        }

        public string showCredits()
        {
            return "Created By Nakor!";
        }

        public string PlayGame()
        {
            return "Enjoy the Game!";
        }

        public string Exit()
        {
            return "Please Play Again!";
        }

        public string runCommand(int choice)
        {
            switch (choice)
            {
                case 0: return PlayGame();
                case 1: return showCredits();
                case 2: return Exit();
                default: return "An Error Occurred";
            }
        }

        public override string ToString()
        {
            string temp = "";

            this.Options.ForEach(item =>
            {
                if (this.Options.ElementAt(this.Choice) == item)
                {
                    temp += " >>" + item + "\n";
                }
                else
                {
                    temp += "   " + item + "\n";
                }
            });

            return temp;
        }
    }
}


Environment: VS2010

This post has been edited by Nakor: 27 July 2010 - 10:06 PM

Was This Post Helpful? 1
  • +
  • -

#12 Statement  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 27-July 10

Re: Examining Coding Style Conventions

Posted 28 July 2010 - 12:13 AM

View PostNakor, on 27 July 2010 - 09:00 PM, said:

Well, here's my go at this. Don't be too harsh! :gun_bandana:

Your targeted version is fine. There was no explicit requirement for this, so any preferable version goes :)

Interesting approach, it looks pretty clean. One thing I'd love to see would be to have a dummy "Game" class and see how the system communicates between different objects!
I'm holding off with further comments just a bit longer to see if we get other results as well. Also I'm at work and can't engage in lengthy discussions :)

Fun to see some involvement!
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Examining Coding Style Conventions

Posted 28 July 2010 - 09:37 AM

Well, my first approach without reading any of the posted code or feedback:
Menu.cs
Program.cs

The idea was that the menu is self-contained. It handles the up/down, the increment/decrement, and it handles the actions. I made a separate method for DoSelectedAction rather than just appending that to the end of ShowMenu because I assumed you may one day want to do something before immediately going straight to the action.

More information to follow in the next post.

I read some feedback and was interested in the idea of the Game class.
Program.cs
Menu.cs
Game.cs
Was This Post Helpful? 1
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,463
  • Joined: 08-June 10

Re: Examining Coding Style Conventions

Posted 28 July 2010 - 09:52 AM

So I thought about my implementation, and realized it wasn't generic enough. The enum makes it nice user friendly, but difficult to change. Also, this would assume that each game would have the same menu. So I changed it a bit to use a string[] instead of an enum:

Program.cs
Menu.cs
Game.cs
Was This Post Helpful? 1
  • +
  • -

#15 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Examining Coding Style Conventions

Posted 28 July 2010 - 09:55 AM

No offense to anyone but this person did a great job of getting others to write their code for them. Now others will be like Hey I dont want you to do this for me but it sure would be nice if I could look at the code you would write for this, then I can make my decision on how to do it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2