“Compliment giver” mini-program

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
30
down vote

favorite
4












I'm fairly new to programming and I decided to make a small C# console program for practice. The program asks for user input and gives a random "compliment", it also has a small chance to give a bonus "compliment". How can I improve my code and also what whould you recomend me to try adding? -ps. the "compliments" are mostly a joke.



My code:



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

namespace Compliment_giver

class Program

static void Main(string args)

Greeting();
start:
string answer = Console.ReadLine();
if(answer != "yes" && answer != "no")

Console.WriteLine("Unexpected input, please type either "yes" or "no"");
goto start;

while(answer == "yes")

Console.Clear();
string result = RandomCompliment();
Console.WriteLine("n0n",result);
if (BonusCompliment())
Console.WriteLine("BONUS COMPLIMENT!nYou are Very Luckyn");
Console.Write("Would you like another compliment? : ");
goto start;



public static string RandomCompliment()

string compliment = new string[10];
compliment[0] = "Your hair smells nice";
compliment[1] = "Your face is ok I guess";
compliment[2] = "You're probably smarter than a penguin";
compliment[3] = "Your body appears to be human";
compliment[4] = "You seem pleasant";
compliment[5] = "You likely aren't the most annoying person in the world";
compliment[6] = "You are talanted... at something possibly";
compliment[7] = "Your eyes are of good color";
compliment[8] = "Your are the best looking person in a room by yourself";
compliment[9] = "You are right all the time, exept only when you are not";

Random number = new Random();
int rng = number.Next(0, 10);

string _result = compliment[rng];

return _result;


public static void Greeting()

Console.WriteLine("Welcome to the Compliment Giver 3000!");
Console.WriteLine("This program is designed to give free compliments to make you feel better");
Console.Write("Do you want a compliment? yes/no: ");


public static bool BonusCompliment()

Random percentage = new Random();
int bonus = percentage.Next(1, 101);

if (bonus <= 3)
return true;
else
return false;










share|improve this question

















  • 7




    Best c# beginner program I've seen yet. Keep studying!
    – Hosch250
    Feb 26 at 3:03







  • 3




    Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
    – user673679
    Feb 26 at 17:09






  • 2




    Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Mast
    Feb 26 at 20:14






  • 3




    @Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
    – Mast
    Feb 26 at 20:16
















up vote
30
down vote

favorite
4












I'm fairly new to programming and I decided to make a small C# console program for practice. The program asks for user input and gives a random "compliment", it also has a small chance to give a bonus "compliment". How can I improve my code and also what whould you recomend me to try adding? -ps. the "compliments" are mostly a joke.



My code:



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

namespace Compliment_giver

class Program

static void Main(string args)

Greeting();
start:
string answer = Console.ReadLine();
if(answer != "yes" && answer != "no")

Console.WriteLine("Unexpected input, please type either "yes" or "no"");
goto start;

while(answer == "yes")

Console.Clear();
string result = RandomCompliment();
Console.WriteLine("n0n",result);
if (BonusCompliment())
Console.WriteLine("BONUS COMPLIMENT!nYou are Very Luckyn");
Console.Write("Would you like another compliment? : ");
goto start;



public static string RandomCompliment()

string compliment = new string[10];
compliment[0] = "Your hair smells nice";
compliment[1] = "Your face is ok I guess";
compliment[2] = "You're probably smarter than a penguin";
compliment[3] = "Your body appears to be human";
compliment[4] = "You seem pleasant";
compliment[5] = "You likely aren't the most annoying person in the world";
compliment[6] = "You are talanted... at something possibly";
compliment[7] = "Your eyes are of good color";
compliment[8] = "Your are the best looking person in a room by yourself";
compliment[9] = "You are right all the time, exept only when you are not";

Random number = new Random();
int rng = number.Next(0, 10);

string _result = compliment[rng];

return _result;


public static void Greeting()

Console.WriteLine("Welcome to the Compliment Giver 3000!");
Console.WriteLine("This program is designed to give free compliments to make you feel better");
Console.Write("Do you want a compliment? yes/no: ");


public static bool BonusCompliment()

Random percentage = new Random();
int bonus = percentage.Next(1, 101);

if (bonus <= 3)
return true;
else
return false;










share|improve this question

















  • 7




    Best c# beginner program I've seen yet. Keep studying!
    – Hosch250
    Feb 26 at 3:03







  • 3




    Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
    – user673679
    Feb 26 at 17:09






  • 2




    Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Mast
    Feb 26 at 20:14






  • 3




    @Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
    – Mast
    Feb 26 at 20:16












up vote
30
down vote

favorite
4









up vote
30
down vote

favorite
4






4





I'm fairly new to programming and I decided to make a small C# console program for practice. The program asks for user input and gives a random "compliment", it also has a small chance to give a bonus "compliment". How can I improve my code and also what whould you recomend me to try adding? -ps. the "compliments" are mostly a joke.



My code:



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

namespace Compliment_giver

class Program

static void Main(string args)

Greeting();
start:
string answer = Console.ReadLine();
if(answer != "yes" && answer != "no")

Console.WriteLine("Unexpected input, please type either "yes" or "no"");
goto start;

while(answer == "yes")

Console.Clear();
string result = RandomCompliment();
Console.WriteLine("n0n",result);
if (BonusCompliment())
Console.WriteLine("BONUS COMPLIMENT!nYou are Very Luckyn");
Console.Write("Would you like another compliment? : ");
goto start;



public static string RandomCompliment()

string compliment = new string[10];
compliment[0] = "Your hair smells nice";
compliment[1] = "Your face is ok I guess";
compliment[2] = "You're probably smarter than a penguin";
compliment[3] = "Your body appears to be human";
compliment[4] = "You seem pleasant";
compliment[5] = "You likely aren't the most annoying person in the world";
compliment[6] = "You are talanted... at something possibly";
compliment[7] = "Your eyes are of good color";
compliment[8] = "Your are the best looking person in a room by yourself";
compliment[9] = "You are right all the time, exept only when you are not";

Random number = new Random();
int rng = number.Next(0, 10);

string _result = compliment[rng];

return _result;


public static void Greeting()

Console.WriteLine("Welcome to the Compliment Giver 3000!");
Console.WriteLine("This program is designed to give free compliments to make you feel better");
Console.Write("Do you want a compliment? yes/no: ");


public static bool BonusCompliment()

Random percentage = new Random();
int bonus = percentage.Next(1, 101);

if (bonus <= 3)
return true;
else
return false;










share|improve this question













I'm fairly new to programming and I decided to make a small C# console program for practice. The program asks for user input and gives a random "compliment", it also has a small chance to give a bonus "compliment". How can I improve my code and also what whould you recomend me to try adding? -ps. the "compliments" are mostly a joke.



My code:



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

namespace Compliment_giver

class Program

static void Main(string args)

Greeting();
start:
string answer = Console.ReadLine();
if(answer != "yes" && answer != "no")

Console.WriteLine("Unexpected input, please type either "yes" or "no"");
goto start;

while(answer == "yes")

Console.Clear();
string result = RandomCompliment();
Console.WriteLine("n0n",result);
if (BonusCompliment())
Console.WriteLine("BONUS COMPLIMENT!nYou are Very Luckyn");
Console.Write("Would you like another compliment? : ");
goto start;



public static string RandomCompliment()

string compliment = new string[10];
compliment[0] = "Your hair smells nice";
compliment[1] = "Your face is ok I guess";
compliment[2] = "You're probably smarter than a penguin";
compliment[3] = "Your body appears to be human";
compliment[4] = "You seem pleasant";
compliment[5] = "You likely aren't the most annoying person in the world";
compliment[6] = "You are talanted... at something possibly";
compliment[7] = "Your eyes are of good color";
compliment[8] = "Your are the best looking person in a room by yourself";
compliment[9] = "You are right all the time, exept only when you are not";

Random number = new Random();
int rng = number.Next(0, 10);

string _result = compliment[rng];

return _result;


public static void Greeting()

Console.WriteLine("Welcome to the Compliment Giver 3000!");
Console.WriteLine("This program is designed to give free compliments to make you feel better");
Console.Write("Do you want a compliment? yes/no: ");


public static bool BonusCompliment()

Random percentage = new Random();
int bonus = percentage.Next(1, 101);

if (bonus <= 3)
return true;
else
return false;












share|improve this question












share|improve this question




share|improve this question








edited Feb 26 at 20:13









Mast

7,33663484




7,33663484









asked Feb 25 at 17:09









BlackBox

340311




340311







  • 7




    Best c# beginner program I've seen yet. Keep studying!
    – Hosch250
    Feb 26 at 3:03







  • 3




    Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
    – user673679
    Feb 26 at 17:09






  • 2




    Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Mast
    Feb 26 at 20:14






  • 3




    @Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
    – Mast
    Feb 26 at 20:16












  • 7




    Best c# beginner program I've seen yet. Keep studying!
    – Hosch250
    Feb 26 at 3:03







  • 3




    Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
    – user673679
    Feb 26 at 17:09






  • 2




    Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
    – Mast
    Feb 26 at 20:14






  • 3




    @Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
    – Mast
    Feb 26 at 20:16







7




7




Best c# beginner program I've seen yet. Keep studying!
– Hosch250
Feb 26 at 3:03





Best c# beginner program I've seen yet. Keep studying!
– Hosch250
Feb 26 at 3:03





3




3




Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
– user673679
Feb 26 at 17:09




Spelling / grammar in the compliments (they're hard-coded, so it's technically code, right?): talanted -> talented, Your are -> You are or You're.
– user673679
Feb 26 at 17:09




2




2




Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Mast
Feb 26 at 20:14




Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Mast
Feb 26 at 20:14




3




3




@Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
– Mast
Feb 26 at 20:16




@Hosch250 Not to pop your bubble or anything, but it's not often I encounter C# code using arcane constructs like goto in multiple places. More studying is indeed a good idea.
– Mast
Feb 26 at 20:16










3 Answers
3






active

oldest

votes

















up vote
32
down vote



accepted










Don't be discouraged by how much I have written. I'm a professional software developer and my colleagues make fun of me because of how pedantic my code reviews are. I think what you've done is actually pretty neat for a first program, and I like that you've obviously made a conscious effort to break things into useful functions (a lot of beginners don't do this well).



I always think people shouldn't pull punches early on in code reviews because it saves agro later on. Some of my points (particularly the formatting/style/naming points) will feel like pedantry, but the issue is that tiny projects become small projects become moderately sized projects and before you know it you have got a beast on your hands. So its worth getting the little stuff right early.



Formatting



  • Typically the class should be indented inside the namespace. having two braces at the same indentation level as you have done at the end of the snippet looks strange.

  • the second line of bool BonusCompliment() is not indented correctly

Naming



Choose a naming convention and stick to it religiously. This will make your code easier to read.




  • compliment is a sequence of compliments so should be plural

  • Don't abbreviate names unless its really obvious (int rng) as it makes your code harder to read. I had to guess that this stands for "random number generator", but even unabbreviated its not named correctly - it is the value returned by the generator, not the generator itself. Personally I'd probably have called it index

  • Why the underscore in string _result? its a common convention in some codebases to prefix class variables with an underscore as a way of distinguishing them, but this is a local variable.

  • C# code doesn't typically use underscores to separate words in identifier names, so your namespace looked a little out of place to me

Style



Style points are usually controversial so others might not agree with the observations I've made. Look into the point, research the opinions that are out there and form your own opinion. Once you have done this make sure you are always consistent.



  • You don't brace single-line if statements. Many programmers (including me) consider this to be poor practice - have a look at this


  • Number literals within code blocks are known as magic numbers which is a practise which is generally discouraged because they make the code harder to read. For example with (bonus <= 3) it is not really clear to the casual reader what the significance of the number '3' is. Is it the age of mum's recently purchased second hand car? The temperature that day? Give a magic number meaning by defining it as a class constant outside the method. This also means that if this number changes at a later date, the change only needs to be made in one place, reducing the possibility of errors.


public class Program 
private const int MumsBirthday = 3;

public bool IsItMumsBirthday(int dayOfMonth)
return dayOfMonth == MumsBirthday;




Simplifications




  • public static string RandomCompliment() can just return compliment[rng]; rather than saving it into a variable and returning that


  • BonusCompliment() doesn't need the if/else. Just use return bonus <= 3

Misc



  • Using labels and the goto keyword is generally frowned upon in most programming languages that implement it - you can google for a fair bit of discussion on this point. As with all these kind of 'rules of thumb', if you think you have found a genuine use case that can't be better represented with other language constructs, by all means use goto. Typically though, the behaviour you are trying to implement is done with do/while like this

bool wantsMoreCompliments = true;
while(wantsMoreCompliments)
string answer = null;
do
if(answer != null)
// Write error message to console

answer = Console.ReadLine()
while(answer != "yes" && answer != "no")
// Do something with answer
// Decide if wantsMoreCompliments



  • Its also worth noting that the use of goto here effectively turns your while loop into an if block - there is never going to be an instance where it loops more than once, since it gets to the end and simply jumps out of the loop.

  • in public static string RandomCompliment() you create an array of compliment strings. In most situations you will handle as a beginner, sequences of objects should be represented by a List<T> data structure rather than an array. For some more detailed discussion on this topic see here.

  • Consider using a collection initialiser for the compliments list as this is much less verbose than the way you have done it:

List<string> compliments = new List<string> 
"Your hair smells nice",
"Your face is ok I guess"
// More compliments
;


Edit



In answer to your question about how to use the list with the random number generator, you would do it like this:



assuming compliments is the list we defined above:



int index = number.Next(compliments.Count);
return compliments[index];





share|improve this answer























  • I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
    – BlackBox
    Feb 25 at 21:38










  • Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
    – Ben Wainwright
    Feb 25 at 21:47







  • 1




    ""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
    – Finn O'leary
    Feb 26 at 9:47










  • I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
    – Raimund Krämer
    Feb 26 at 10:47






  • 3




    On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
    – Rob
    Feb 26 at 11:28


















up vote
7
down vote













All of Ben Wainwright's pointers are fantastic.



There are, however, two things I believe are important, which were omitted.




  1. Your use of Random may cause significant headaches in the future. Granted, in your use case, it's extremely unlikely to be an issue, it's a good thing to understand thoroughly, as it's not something you're likely to discover on your own. Here's a detailed explanation of the problem.

    In essence, you should not create new Random instances every time you want to generate a random number. You should share instances (but not across threads), to ensure you don't continually generate the same 'random' number. In this case, I'd re-work your code to be:



    class Program

    private static readonly Random _random = new Random();

    public static string RandomCompliment()

    // ...
    int rng = _random.Next(0, 10);

    string _result = compliment[rng];

    return _result;


    public static bool BonusCompliment()

    int bonus = _random.Next(1, 101);
    if (bonus <= 3)
    return true;
    else
    return false;




    Note that we can mark the field as readonly* to better clarify our intent that _random should be a single instance.



  2. This is somewhat minor in comparison, but you're creating a new array every time you ask for a random compliment. You don't really need to do all that work each time, you need only do it once. Again, I'd refactor this out of the method, so it's only created once, and have the method read from it.


* Thanks to Jesse C. Slicer for the suggestion!






share|improve this answer



















  • 2




    I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
    – Jesse C. Slicer
    Feb 26 at 14:51










  • @JesseC.Slicer Good suggestion, thanks!
    – Rob
    Feb 27 at 0:09

















up vote
6
down vote













In addition to Ben Wainwright's excellent and comprehensive review, I would add:



  1. When accepting user input, you should always clean it up and normalise it. Trim white spaces, and convert to either uppercase or lowercase. If the user entered "YES", your program wouldn't match to "yes", for example.


  2. When returning the result of a boolean expression it is clearer and less error prone to simply return the expression. For example,


Instead of:



if (bonus <= 3)
return true;
else
return false;


simply do:



return bonus <= 3;





share|improve this answer

















  • 1




    (Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
    – greybeard
    Feb 25 at 23:33










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188329%2fcompliment-giver-mini-program%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
32
down vote



accepted










Don't be discouraged by how much I have written. I'm a professional software developer and my colleagues make fun of me because of how pedantic my code reviews are. I think what you've done is actually pretty neat for a first program, and I like that you've obviously made a conscious effort to break things into useful functions (a lot of beginners don't do this well).



I always think people shouldn't pull punches early on in code reviews because it saves agro later on. Some of my points (particularly the formatting/style/naming points) will feel like pedantry, but the issue is that tiny projects become small projects become moderately sized projects and before you know it you have got a beast on your hands. So its worth getting the little stuff right early.



Formatting



  • Typically the class should be indented inside the namespace. having two braces at the same indentation level as you have done at the end of the snippet looks strange.

  • the second line of bool BonusCompliment() is not indented correctly

Naming



Choose a naming convention and stick to it religiously. This will make your code easier to read.




  • compliment is a sequence of compliments so should be plural

  • Don't abbreviate names unless its really obvious (int rng) as it makes your code harder to read. I had to guess that this stands for "random number generator", but even unabbreviated its not named correctly - it is the value returned by the generator, not the generator itself. Personally I'd probably have called it index

  • Why the underscore in string _result? its a common convention in some codebases to prefix class variables with an underscore as a way of distinguishing them, but this is a local variable.

  • C# code doesn't typically use underscores to separate words in identifier names, so your namespace looked a little out of place to me

Style



Style points are usually controversial so others might not agree with the observations I've made. Look into the point, research the opinions that are out there and form your own opinion. Once you have done this make sure you are always consistent.



  • You don't brace single-line if statements. Many programmers (including me) consider this to be poor practice - have a look at this


  • Number literals within code blocks are known as magic numbers which is a practise which is generally discouraged because they make the code harder to read. For example with (bonus <= 3) it is not really clear to the casual reader what the significance of the number '3' is. Is it the age of mum's recently purchased second hand car? The temperature that day? Give a magic number meaning by defining it as a class constant outside the method. This also means that if this number changes at a later date, the change only needs to be made in one place, reducing the possibility of errors.


public class Program 
private const int MumsBirthday = 3;

public bool IsItMumsBirthday(int dayOfMonth)
return dayOfMonth == MumsBirthday;




Simplifications




  • public static string RandomCompliment() can just return compliment[rng]; rather than saving it into a variable and returning that


  • BonusCompliment() doesn't need the if/else. Just use return bonus <= 3

Misc



  • Using labels and the goto keyword is generally frowned upon in most programming languages that implement it - you can google for a fair bit of discussion on this point. As with all these kind of 'rules of thumb', if you think you have found a genuine use case that can't be better represented with other language constructs, by all means use goto. Typically though, the behaviour you are trying to implement is done with do/while like this

bool wantsMoreCompliments = true;
while(wantsMoreCompliments)
string answer = null;
do
if(answer != null)
// Write error message to console

answer = Console.ReadLine()
while(answer != "yes" && answer != "no")
// Do something with answer
// Decide if wantsMoreCompliments



  • Its also worth noting that the use of goto here effectively turns your while loop into an if block - there is never going to be an instance where it loops more than once, since it gets to the end and simply jumps out of the loop.

  • in public static string RandomCompliment() you create an array of compliment strings. In most situations you will handle as a beginner, sequences of objects should be represented by a List<T> data structure rather than an array. For some more detailed discussion on this topic see here.

  • Consider using a collection initialiser for the compliments list as this is much less verbose than the way you have done it:

List<string> compliments = new List<string> 
"Your hair smells nice",
"Your face is ok I guess"
// More compliments
;


Edit



In answer to your question about how to use the list with the random number generator, you would do it like this:



assuming compliments is the list we defined above:



int index = number.Next(compliments.Count);
return compliments[index];





share|improve this answer























  • I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
    – BlackBox
    Feb 25 at 21:38










  • Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
    – Ben Wainwright
    Feb 25 at 21:47







  • 1




    ""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
    – Finn O'leary
    Feb 26 at 9:47










  • I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
    – Raimund Krämer
    Feb 26 at 10:47






  • 3




    On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
    – Rob
    Feb 26 at 11:28















up vote
32
down vote



accepted










Don't be discouraged by how much I have written. I'm a professional software developer and my colleagues make fun of me because of how pedantic my code reviews are. I think what you've done is actually pretty neat for a first program, and I like that you've obviously made a conscious effort to break things into useful functions (a lot of beginners don't do this well).



I always think people shouldn't pull punches early on in code reviews because it saves agro later on. Some of my points (particularly the formatting/style/naming points) will feel like pedantry, but the issue is that tiny projects become small projects become moderately sized projects and before you know it you have got a beast on your hands. So its worth getting the little stuff right early.



Formatting



  • Typically the class should be indented inside the namespace. having two braces at the same indentation level as you have done at the end of the snippet looks strange.

  • the second line of bool BonusCompliment() is not indented correctly

Naming



Choose a naming convention and stick to it religiously. This will make your code easier to read.




  • compliment is a sequence of compliments so should be plural

  • Don't abbreviate names unless its really obvious (int rng) as it makes your code harder to read. I had to guess that this stands for "random number generator", but even unabbreviated its not named correctly - it is the value returned by the generator, not the generator itself. Personally I'd probably have called it index

  • Why the underscore in string _result? its a common convention in some codebases to prefix class variables with an underscore as a way of distinguishing them, but this is a local variable.

  • C# code doesn't typically use underscores to separate words in identifier names, so your namespace looked a little out of place to me

Style



Style points are usually controversial so others might not agree with the observations I've made. Look into the point, research the opinions that are out there and form your own opinion. Once you have done this make sure you are always consistent.



  • You don't brace single-line if statements. Many programmers (including me) consider this to be poor practice - have a look at this


  • Number literals within code blocks are known as magic numbers which is a practise which is generally discouraged because they make the code harder to read. For example with (bonus <= 3) it is not really clear to the casual reader what the significance of the number '3' is. Is it the age of mum's recently purchased second hand car? The temperature that day? Give a magic number meaning by defining it as a class constant outside the method. This also means that if this number changes at a later date, the change only needs to be made in one place, reducing the possibility of errors.


public class Program 
private const int MumsBirthday = 3;

public bool IsItMumsBirthday(int dayOfMonth)
return dayOfMonth == MumsBirthday;




Simplifications




  • public static string RandomCompliment() can just return compliment[rng]; rather than saving it into a variable and returning that


  • BonusCompliment() doesn't need the if/else. Just use return bonus <= 3

Misc



  • Using labels and the goto keyword is generally frowned upon in most programming languages that implement it - you can google for a fair bit of discussion on this point. As with all these kind of 'rules of thumb', if you think you have found a genuine use case that can't be better represented with other language constructs, by all means use goto. Typically though, the behaviour you are trying to implement is done with do/while like this

bool wantsMoreCompliments = true;
while(wantsMoreCompliments)
string answer = null;
do
if(answer != null)
// Write error message to console

answer = Console.ReadLine()
while(answer != "yes" && answer != "no")
// Do something with answer
// Decide if wantsMoreCompliments



  • Its also worth noting that the use of goto here effectively turns your while loop into an if block - there is never going to be an instance where it loops more than once, since it gets to the end and simply jumps out of the loop.

  • in public static string RandomCompliment() you create an array of compliment strings. In most situations you will handle as a beginner, sequences of objects should be represented by a List<T> data structure rather than an array. For some more detailed discussion on this topic see here.

  • Consider using a collection initialiser for the compliments list as this is much less verbose than the way you have done it:

List<string> compliments = new List<string> 
"Your hair smells nice",
"Your face is ok I guess"
// More compliments
;


Edit



In answer to your question about how to use the list with the random number generator, you would do it like this:



assuming compliments is the list we defined above:



int index = number.Next(compliments.Count);
return compliments[index];





share|improve this answer























  • I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
    – BlackBox
    Feb 25 at 21:38










  • Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
    – Ben Wainwright
    Feb 25 at 21:47







  • 1




    ""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
    – Finn O'leary
    Feb 26 at 9:47










  • I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
    – Raimund Krämer
    Feb 26 at 10:47






  • 3




    On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
    – Rob
    Feb 26 at 11:28













up vote
32
down vote



accepted







up vote
32
down vote



accepted






Don't be discouraged by how much I have written. I'm a professional software developer and my colleagues make fun of me because of how pedantic my code reviews are. I think what you've done is actually pretty neat for a first program, and I like that you've obviously made a conscious effort to break things into useful functions (a lot of beginners don't do this well).



I always think people shouldn't pull punches early on in code reviews because it saves agro later on. Some of my points (particularly the formatting/style/naming points) will feel like pedantry, but the issue is that tiny projects become small projects become moderately sized projects and before you know it you have got a beast on your hands. So its worth getting the little stuff right early.



Formatting



  • Typically the class should be indented inside the namespace. having two braces at the same indentation level as you have done at the end of the snippet looks strange.

  • the second line of bool BonusCompliment() is not indented correctly

Naming



Choose a naming convention and stick to it religiously. This will make your code easier to read.




  • compliment is a sequence of compliments so should be plural

  • Don't abbreviate names unless its really obvious (int rng) as it makes your code harder to read. I had to guess that this stands for "random number generator", but even unabbreviated its not named correctly - it is the value returned by the generator, not the generator itself. Personally I'd probably have called it index

  • Why the underscore in string _result? its a common convention in some codebases to prefix class variables with an underscore as a way of distinguishing them, but this is a local variable.

  • C# code doesn't typically use underscores to separate words in identifier names, so your namespace looked a little out of place to me

Style



Style points are usually controversial so others might not agree with the observations I've made. Look into the point, research the opinions that are out there and form your own opinion. Once you have done this make sure you are always consistent.



  • You don't brace single-line if statements. Many programmers (including me) consider this to be poor practice - have a look at this


  • Number literals within code blocks are known as magic numbers which is a practise which is generally discouraged because they make the code harder to read. For example with (bonus <= 3) it is not really clear to the casual reader what the significance of the number '3' is. Is it the age of mum's recently purchased second hand car? The temperature that day? Give a magic number meaning by defining it as a class constant outside the method. This also means that if this number changes at a later date, the change only needs to be made in one place, reducing the possibility of errors.


public class Program 
private const int MumsBirthday = 3;

public bool IsItMumsBirthday(int dayOfMonth)
return dayOfMonth == MumsBirthday;




Simplifications




  • public static string RandomCompliment() can just return compliment[rng]; rather than saving it into a variable and returning that


  • BonusCompliment() doesn't need the if/else. Just use return bonus <= 3

Misc



  • Using labels and the goto keyword is generally frowned upon in most programming languages that implement it - you can google for a fair bit of discussion on this point. As with all these kind of 'rules of thumb', if you think you have found a genuine use case that can't be better represented with other language constructs, by all means use goto. Typically though, the behaviour you are trying to implement is done with do/while like this

bool wantsMoreCompliments = true;
while(wantsMoreCompliments)
string answer = null;
do
if(answer != null)
// Write error message to console

answer = Console.ReadLine()
while(answer != "yes" && answer != "no")
// Do something with answer
// Decide if wantsMoreCompliments



  • Its also worth noting that the use of goto here effectively turns your while loop into an if block - there is never going to be an instance where it loops more than once, since it gets to the end and simply jumps out of the loop.

  • in public static string RandomCompliment() you create an array of compliment strings. In most situations you will handle as a beginner, sequences of objects should be represented by a List<T> data structure rather than an array. For some more detailed discussion on this topic see here.

  • Consider using a collection initialiser for the compliments list as this is much less verbose than the way you have done it:

List<string> compliments = new List<string> 
"Your hair smells nice",
"Your face is ok I guess"
// More compliments
;


Edit



In answer to your question about how to use the list with the random number generator, you would do it like this:



assuming compliments is the list we defined above:



int index = number.Next(compliments.Count);
return compliments[index];





share|improve this answer















Don't be discouraged by how much I have written. I'm a professional software developer and my colleagues make fun of me because of how pedantic my code reviews are. I think what you've done is actually pretty neat for a first program, and I like that you've obviously made a conscious effort to break things into useful functions (a lot of beginners don't do this well).



I always think people shouldn't pull punches early on in code reviews because it saves agro later on. Some of my points (particularly the formatting/style/naming points) will feel like pedantry, but the issue is that tiny projects become small projects become moderately sized projects and before you know it you have got a beast on your hands. So its worth getting the little stuff right early.



Formatting



  • Typically the class should be indented inside the namespace. having two braces at the same indentation level as you have done at the end of the snippet looks strange.

  • the second line of bool BonusCompliment() is not indented correctly

Naming



Choose a naming convention and stick to it religiously. This will make your code easier to read.




  • compliment is a sequence of compliments so should be plural

  • Don't abbreviate names unless its really obvious (int rng) as it makes your code harder to read. I had to guess that this stands for "random number generator", but even unabbreviated its not named correctly - it is the value returned by the generator, not the generator itself. Personally I'd probably have called it index

  • Why the underscore in string _result? its a common convention in some codebases to prefix class variables with an underscore as a way of distinguishing them, but this is a local variable.

  • C# code doesn't typically use underscores to separate words in identifier names, so your namespace looked a little out of place to me

Style



Style points are usually controversial so others might not agree with the observations I've made. Look into the point, research the opinions that are out there and form your own opinion. Once you have done this make sure you are always consistent.



  • You don't brace single-line if statements. Many programmers (including me) consider this to be poor practice - have a look at this


  • Number literals within code blocks are known as magic numbers which is a practise which is generally discouraged because they make the code harder to read. For example with (bonus <= 3) it is not really clear to the casual reader what the significance of the number '3' is. Is it the age of mum's recently purchased second hand car? The temperature that day? Give a magic number meaning by defining it as a class constant outside the method. This also means that if this number changes at a later date, the change only needs to be made in one place, reducing the possibility of errors.


public class Program 
private const int MumsBirthday = 3;

public bool IsItMumsBirthday(int dayOfMonth)
return dayOfMonth == MumsBirthday;




Simplifications




  • public static string RandomCompliment() can just return compliment[rng]; rather than saving it into a variable and returning that


  • BonusCompliment() doesn't need the if/else. Just use return bonus <= 3

Misc



  • Using labels and the goto keyword is generally frowned upon in most programming languages that implement it - you can google for a fair bit of discussion on this point. As with all these kind of 'rules of thumb', if you think you have found a genuine use case that can't be better represented with other language constructs, by all means use goto. Typically though, the behaviour you are trying to implement is done with do/while like this

bool wantsMoreCompliments = true;
while(wantsMoreCompliments)
string answer = null;
do
if(answer != null)
// Write error message to console

answer = Console.ReadLine()
while(answer != "yes" && answer != "no")
// Do something with answer
// Decide if wantsMoreCompliments



  • Its also worth noting that the use of goto here effectively turns your while loop into an if block - there is never going to be an instance where it loops more than once, since it gets to the end and simply jumps out of the loop.

  • in public static string RandomCompliment() you create an array of compliment strings. In most situations you will handle as a beginner, sequences of objects should be represented by a List<T> data structure rather than an array. For some more detailed discussion on this topic see here.

  • Consider using a collection initialiser for the compliments list as this is much less verbose than the way you have done it:

List<string> compliments = new List<string> 
"Your hair smells nice",
"Your face is ok I guess"
// More compliments
;


Edit



In answer to your question about how to use the list with the random number generator, you would do it like this:



assuming compliments is the list we defined above:



int index = number.Next(compliments.Count);
return compliments[index];






share|improve this answer















share|improve this answer



share|improve this answer








edited Feb 27 at 9:09


























answered Feb 25 at 21:10









Ben Wainwright

41648




41648











  • I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
    – BlackBox
    Feb 25 at 21:38










  • Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
    – Ben Wainwright
    Feb 25 at 21:47







  • 1




    ""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
    – Finn O'leary
    Feb 26 at 9:47










  • I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
    – Raimund Krämer
    Feb 26 at 10:47






  • 3




    On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
    – Rob
    Feb 26 at 11:28

















  • I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
    – BlackBox
    Feb 25 at 21:38










  • Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
    – Ben Wainwright
    Feb 25 at 21:47







  • 1




    ""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
    – Finn O'leary
    Feb 26 at 9:47










  • I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
    – Raimund Krämer
    Feb 26 at 10:47






  • 3




    On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
    – Rob
    Feb 26 at 11:28
















I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
– BlackBox
Feb 25 at 21:38




I apreciate the feedback, originally I tried to put the compliments into a List (and even in a class) but I couldn't find a way to have them return an integer so I can make the result random (or another way to do the same).
– BlackBox
Feb 25 at 21:38












Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
– Ben Wainwright
Feb 25 at 21:47





Hey @Stav_K - I've just edited my answer to include an explanation of how that would be achieved (at the bottom). Hope that helps!
– Ben Wainwright
Feb 25 at 21:47





1




1




""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
– Finn O'leary
Feb 26 at 9:47




""I always think people shouldn't pull punches early on in code reviews because it saves agro later on."". 100% this. +1
– Finn O'leary
Feb 26 at 9:47












I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
– Raimund Krämer
Feb 26 at 10:47




I would like to add that the random number generator (instance of Random) should not be called number, which it still is after your suggestions. In the part Random number = new Random(); int rng = number.Next(0, 10);, besides the abbreviation which should be avoided, the names should logically be the other way round.
– Raimund Krämer
Feb 26 at 10:47




3




3




On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
– Rob
Feb 26 at 11:28





On your point "One of the useful side effects of following this advice is that you will then be able to declare the list with a collection initialiser which is much less verbose" - This isn't a benefit of using a List<T> over an array. An array also allows this. var compliments = new "a", "b" ;
– Rob
Feb 26 at 11:28













up vote
7
down vote













All of Ben Wainwright's pointers are fantastic.



There are, however, two things I believe are important, which were omitted.




  1. Your use of Random may cause significant headaches in the future. Granted, in your use case, it's extremely unlikely to be an issue, it's a good thing to understand thoroughly, as it's not something you're likely to discover on your own. Here's a detailed explanation of the problem.

    In essence, you should not create new Random instances every time you want to generate a random number. You should share instances (but not across threads), to ensure you don't continually generate the same 'random' number. In this case, I'd re-work your code to be:



    class Program

    private static readonly Random _random = new Random();

    public static string RandomCompliment()

    // ...
    int rng = _random.Next(0, 10);

    string _result = compliment[rng];

    return _result;


    public static bool BonusCompliment()

    int bonus = _random.Next(1, 101);
    if (bonus <= 3)
    return true;
    else
    return false;




    Note that we can mark the field as readonly* to better clarify our intent that _random should be a single instance.



  2. This is somewhat minor in comparison, but you're creating a new array every time you ask for a random compliment. You don't really need to do all that work each time, you need only do it once. Again, I'd refactor this out of the method, so it's only created once, and have the method read from it.


* Thanks to Jesse C. Slicer for the suggestion!






share|improve this answer



















  • 2




    I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
    – Jesse C. Slicer
    Feb 26 at 14:51










  • @JesseC.Slicer Good suggestion, thanks!
    – Rob
    Feb 27 at 0:09














up vote
7
down vote













All of Ben Wainwright's pointers are fantastic.



There are, however, two things I believe are important, which were omitted.




  1. Your use of Random may cause significant headaches in the future. Granted, in your use case, it's extremely unlikely to be an issue, it's a good thing to understand thoroughly, as it's not something you're likely to discover on your own. Here's a detailed explanation of the problem.

    In essence, you should not create new Random instances every time you want to generate a random number. You should share instances (but not across threads), to ensure you don't continually generate the same 'random' number. In this case, I'd re-work your code to be:



    class Program

    private static readonly Random _random = new Random();

    public static string RandomCompliment()

    // ...
    int rng = _random.Next(0, 10);

    string _result = compliment[rng];

    return _result;


    public static bool BonusCompliment()

    int bonus = _random.Next(1, 101);
    if (bonus <= 3)
    return true;
    else
    return false;




    Note that we can mark the field as readonly* to better clarify our intent that _random should be a single instance.



  2. This is somewhat minor in comparison, but you're creating a new array every time you ask for a random compliment. You don't really need to do all that work each time, you need only do it once. Again, I'd refactor this out of the method, so it's only created once, and have the method read from it.


* Thanks to Jesse C. Slicer for the suggestion!






share|improve this answer



















  • 2




    I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
    – Jesse C. Slicer
    Feb 26 at 14:51










  • @JesseC.Slicer Good suggestion, thanks!
    – Rob
    Feb 27 at 0:09












up vote
7
down vote










up vote
7
down vote









All of Ben Wainwright's pointers are fantastic.



There are, however, two things I believe are important, which were omitted.




  1. Your use of Random may cause significant headaches in the future. Granted, in your use case, it's extremely unlikely to be an issue, it's a good thing to understand thoroughly, as it's not something you're likely to discover on your own. Here's a detailed explanation of the problem.

    In essence, you should not create new Random instances every time you want to generate a random number. You should share instances (but not across threads), to ensure you don't continually generate the same 'random' number. In this case, I'd re-work your code to be:



    class Program

    private static readonly Random _random = new Random();

    public static string RandomCompliment()

    // ...
    int rng = _random.Next(0, 10);

    string _result = compliment[rng];

    return _result;


    public static bool BonusCompliment()

    int bonus = _random.Next(1, 101);
    if (bonus <= 3)
    return true;
    else
    return false;




    Note that we can mark the field as readonly* to better clarify our intent that _random should be a single instance.



  2. This is somewhat minor in comparison, but you're creating a new array every time you ask for a random compliment. You don't really need to do all that work each time, you need only do it once. Again, I'd refactor this out of the method, so it's only created once, and have the method read from it.


* Thanks to Jesse C. Slicer for the suggestion!






share|improve this answer















All of Ben Wainwright's pointers are fantastic.



There are, however, two things I believe are important, which were omitted.




  1. Your use of Random may cause significant headaches in the future. Granted, in your use case, it's extremely unlikely to be an issue, it's a good thing to understand thoroughly, as it's not something you're likely to discover on your own. Here's a detailed explanation of the problem.

    In essence, you should not create new Random instances every time you want to generate a random number. You should share instances (but not across threads), to ensure you don't continually generate the same 'random' number. In this case, I'd re-work your code to be:



    class Program

    private static readonly Random _random = new Random();

    public static string RandomCompliment()

    // ...
    int rng = _random.Next(0, 10);

    string _result = compliment[rng];

    return _result;


    public static bool BonusCompliment()

    int bonus = _random.Next(1, 101);
    if (bonus <= 3)
    return true;
    else
    return false;




    Note that we can mark the field as readonly* to better clarify our intent that _random should be a single instance.



  2. This is somewhat minor in comparison, but you're creating a new array every time you ask for a random compliment. You don't really need to do all that work each time, you need only do it once. Again, I'd refactor this out of the method, so it's only created once, and have the method read from it.


* Thanks to Jesse C. Slicer for the suggestion!







share|improve this answer















share|improve this answer



share|improve this answer








edited Feb 27 at 0:09


























answered Feb 26 at 11:38









Rob

578412




578412







  • 2




    I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
    – Jesse C. Slicer
    Feb 26 at 14:51










  • @JesseC.Slicer Good suggestion, thanks!
    – Rob
    Feb 27 at 0:09












  • 2




    I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
    – Jesse C. Slicer
    Feb 26 at 14:51










  • @JesseC.Slicer Good suggestion, thanks!
    – Rob
    Feb 27 at 0:09







2




2




I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
– Jesse C. Slicer
Feb 26 at 14:51




I would go so far as to mark the _random instance as readonly to signify intent, prevent it from being reassigned elsewhere, and, potentially, allow the compiler/JITter to enact optimizations.
– Jesse C. Slicer
Feb 26 at 14:51












@JesseC.Slicer Good suggestion, thanks!
– Rob
Feb 27 at 0:09




@JesseC.Slicer Good suggestion, thanks!
– Rob
Feb 27 at 0:09










up vote
6
down vote













In addition to Ben Wainwright's excellent and comprehensive review, I would add:



  1. When accepting user input, you should always clean it up and normalise it. Trim white spaces, and convert to either uppercase or lowercase. If the user entered "YES", your program wouldn't match to "yes", for example.


  2. When returning the result of a boolean expression it is clearer and less error prone to simply return the expression. For example,


Instead of:



if (bonus <= 3)
return true;
else
return false;


simply do:



return bonus <= 3;





share|improve this answer

















  • 1




    (Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
    – greybeard
    Feb 25 at 23:33














up vote
6
down vote













In addition to Ben Wainwright's excellent and comprehensive review, I would add:



  1. When accepting user input, you should always clean it up and normalise it. Trim white spaces, and convert to either uppercase or lowercase. If the user entered "YES", your program wouldn't match to "yes", for example.


  2. When returning the result of a boolean expression it is clearer and less error prone to simply return the expression. For example,


Instead of:



if (bonus <= 3)
return true;
else
return false;


simply do:



return bonus <= 3;





share|improve this answer

















  • 1




    (Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
    – greybeard
    Feb 25 at 23:33












up vote
6
down vote










up vote
6
down vote









In addition to Ben Wainwright's excellent and comprehensive review, I would add:



  1. When accepting user input, you should always clean it up and normalise it. Trim white spaces, and convert to either uppercase or lowercase. If the user entered "YES", your program wouldn't match to "yes", for example.


  2. When returning the result of a boolean expression it is clearer and less error prone to simply return the expression. For example,


Instead of:



if (bonus <= 3)
return true;
else
return false;


simply do:



return bonus <= 3;





share|improve this answer













In addition to Ben Wainwright's excellent and comprehensive review, I would add:



  1. When accepting user input, you should always clean it up and normalise it. Trim white spaces, and convert to either uppercase or lowercase. If the user entered "YES", your program wouldn't match to "yes", for example.


  2. When returning the result of a boolean expression it is clearer and less error prone to simply return the expression. For example,


Instead of:



if (bonus <= 3)
return true;
else
return false;


simply do:



return bonus <= 3;






share|improve this answer













share|improve this answer



share|improve this answer











answered Feb 25 at 22:48









T33C

1613




1613







  • 1




    (Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
    – greybeard
    Feb 25 at 23:33












  • 1




    (Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
    – greybeard
    Feb 25 at 23:33







1




1




(Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
– greybeard
Feb 25 at 23:33




(Welcome to CR!) (You seem to have missed Ben Wainwright's second Simplification.)
– greybeard
Feb 25 at 23:33












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188329%2fcompliment-giver-mini-program%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation