HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMajor

C# Program that checks on the status of the Capslock with each character insert

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theinserteachcheckswithprogramcharacterstatusthatcapslock

Problem

The following is my first successful running C# program.

The lessons that I personally learnt through this include:

  • The value of asking for help after having tried several times



  • The advantages of being adaptable about one's methods of reaching an end



  • The (a?) proper placement of functions in relation to a main program



  • How to pass information into and out of such functions



  • How to use a loop to leverage code (doing the same stuff over and over)



  • How to collect character input from the User & I'm sure there's more...



In any case this code appears to work. I know that it doesn't hold a candle to much of the stuff you guys put up but on the bright side its short and kind of readable (both aims of mine).

Could some much brighter spark than me kindly shine a light on the stuff that could have been done better (or simply provide some advice or suggested direction)? :c)

```
using System;
namespace CapsChecker
{
class Program
{
static void Main(string[] args)
{
//Set up an infinite loop for program to run within
while (true)
{
//Collection of user input
Console.Write("Press a key: ");
char input = (Console.ReadKey().KeyChar);
//Check if Capslock is on when key entered. Combined with
//the shift button this can result in CapsCheck returning
//true even if key entered is lower case.
if (CapsCheck() == true)
{
CapsNotify(true);
}
else
{
CapsNotify(false);
}
Console.WriteLine();

switch (input) //Abandoned '.Key' enum
{
case 'Q':
{ break; }
default:
{ continue; }
}
break; //Aftertho

Solution

if true return true else return false antipattern

Unecessary if statements when dealing with booleans is a very common beginner pitfall.

static bool CapsCheck()
    {
            // Check if Capslock is Active
        if (Console.CapsLock == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


Is equivalent to:

static bool CapsCheck()
   {
       return Console.CapsLock;
   }


As a further simplification, you may use Console.CapsLock directly and avoid this function.

Another boolean simplification is

if (CapsCheck() == true)
            {
                CapsNotify(true);   
            }
            else
            {
                CapsNotify(false);
            }


To:

CapsNotify( CapsCheck() );


Or:

CapsNotify( Console.CapsLock );


Ternary simplification

In CapsNotify it is unecessary to use a full if else, a ternary is enough and simpler:

Console.Writeline("CapsLock is" + (caps ? "Active" : "Inactive"));

Code Snippets

static bool CapsCheck()
    {
            // Check if Capslock is Active
        if (Console.CapsLock == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
static bool CapsCheck()
   {
       return Console.CapsLock;
   }
if (CapsCheck() == true)
            {
                CapsNotify(true);   
            }
            else
            {
                CapsNotify(false);
            }
CapsNotify( CapsCheck() );
CapsNotify( Console.CapsLock );

Context

StackExchange Code Review Q#128345, answer score: 23

Revisions (0)

No revisions yet.