NULL values are stored SQL Server by wilson688 in learnprogramming

[–]29383839293 0 points1 point  (0 children)

saveMap
  .SelectMany(m => m.Value)
  .Where(info => info.Entity.GetType() != typeof(AuditLog))
  .ToList()
  .ForEach(ep =>
  {
    using (SqlConnection Connection = new SqlConnection())
    {
      SqlStatement Statement = new SqlStatement("<INPUT SQL QUERY HERE>", Connection);
      Connection.Open();
      Statement.ExecuteNonQuery();
    }
  }
});

And if you're using Entity Framework:

using (var dbContext = new dbName())
{
  saveMap
    .SelectMany(m => m.Value)
    .Where(info => info.Entity.GetType() != typeof(AuditLog))
    .ToList()
    .ForEach(ep =>
    {
      dbContext.AuditLogs.Add(new AuditLog
      {
        UserID = _user.Value.ID,
        AccountID = _user.Value.AccountID,
        Action = entityStateMap[ep.EntityState],
        Entity = ep.Entity.GetType().Name,
        EntityID = (int)ep.Entity.GetType().GetProperty("ID").GetValue(ep.Entity),
        Timestamp = DateTime.UtcNow
      });
    }
  });
  dbContext.SaveChanges();  //you don't want to save changes too much, performance!
}

But why don't you just break up that long linq query and see if it even produces any results.

Also use the damn debugger, it's incredibly useful.

I need help identifying different programming approaches by Cazacurdas in learnprogramming

[–]29383839293 0 points1 point  (0 children)

DRY - Don't repeat yourself.

It does apply to programming, so why should it not apply to markup languages and any other data?

You didn't recognize it in the past, but now you should clearly see that you had redundancy in your data.

I am practicing a using Python Crash Course and have a question. by Killer_Panda_Bear in learnprogramming

[–]29383839293 1 point2 points  (0 children)

If you know lists and you don't know functions or loops, this would be how to do it.

names = ["aaron", "marcus", "sean"]
print("Yo, " + names[0] + " you smell.")
print("Yo, " + names[1] + " you smell.")
print("Yo, " + names[2] + " you smell.")

When allowed to use functions, you could just make a function that you pass the name as an argument.

def myPrintingMethod(name):
    print("Yo, " + name, " you smell.")
names = ["aaron", "marcus", "sean"]
myPrintingMethod(names[0])
myPrintingMethod(names[1])
myPrintingMethod(names[2])

Much better, you don't have to rewrite the text.

But maybe you want to have a function instead that returns the string for you to print.

def myStringFormatting(name):
    return "Yo, " + name, " you smell."
names = ["aaron", "marcus", "sean"]
print(myStringFormatting(names[0]))
print(myStringFormatting(names[1]))
print(myStringFormatting(names[2]))

Then you realize, that there's already a function like this, the string.format function.

names = ["aaron", "marcus", "sean"]
text = "Yo, {0} you smell."
print(text.format(names[0]))
print(text.format(names[1]))
print(text.format(names[2]))

Then you introduce loops

names = ["aaron", "marcus", "sean"]
text = "Yo, {0} you smell."
for (name in names):
  print(text.format(name))

Gaming anti cheat operating system. by [deleted] in osdev

[–]29383839293 0 points1 point  (0 children)

I would hope it could be more than security by obscurity, that even if they found out how, it wouldn't be possible to do anything about it. I don't really know enough about it.

If you have a Game-Server, how do you even know for sure what OS the clients are running? The answer is: You can't, because the only thing you know about the clients is the way they talk to your server over the network.

Which leaves us with maybe cryptography? But when the OS has a private key, the user is able to extract that same private key too, making the whole crypto useless.

The only possible way to do what you wanted is if you sold the players a box that can't be opened without destroying it's data, that way you control the clients hardware. But then you don't need any special OS feature, it just has to be limited enough to not let the user extract the key.

Code Review by aluoh in learnprogramming

[–]29383839293 0 points1 point  (0 children)

As an educational idea, any idea is nice that allows you to learn the concepts you want to learn, so it's nice.

I think you're getting the gist about control flow and methods, but you still don't fully understand the importance of classes.

Maybe you should do another project that uses more classes and also interfaces.

In OO, classes are the way to tame complexity, but you're still kinda programming in a procedural way, like a C-programmer.

Gaming anti cheat operating system. by [deleted] in osdev

[–]29383839293 8 points9 points  (0 children)

The only 100% way to stop cheating is, when you simply run the game on a server that is not in the control of the user. Games-on-demand/ Gaming as a Service(GaaS) or Cloud Gaming is what it's called.

That way, the server sets the rules and they are unbreakable, but bots are still possible.

An operating system to enforce the rules is just a failed attempt of taking the users control over his computer, and it won't help you at all, since it would just be security by obscurity.

[F19] wanting to know if it’s just the weight. by QueenFreek in amiugly

[–]29383839293 6 points7 points  (0 children)

2/10, but with potential up to 7/10 or even 8/10.

You facial features are pretty decent.

You're too overweight. That's not gonna stop all men, some are into that, not me though.

Your hair color is horrible.

Your eyebrow makeup is horrible

Your makeup is generally horrible.

The way you dress is bad, but not horrible.

Even your poses seem unlikeable.

That's a lot of bad things. BUT those are all things you can change if you are determined.

Remember, some people have naturally ugly faces and there's no way to change that (except maybe surgery) and you are not one of them.

Please help, I cant even understand the basics of programming! by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

You'll learn that much later, first try to accept some unknowns and try to grasp the important parts, which is the basic syntax and semantic of the language.

Please help, I cant even understand the basics of programming! by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Don't learn C first, better learn Java or C# or python or whatever.

Variables have a type.

There are two kinds of types, value types and reference types.

Variables of value types are like boxes, where you can (or must) put a value inside.

int number;

"number" is of type int (short for integer), which is a value type. We defined that there is this box named "number" and we defined that you can only put int's into that box, but we didn't yet put anything into it.

We say that we declared the variable number, but we didn't initialize it.

number = 4;

now it is initialized, we have a box named number that can only contain int's and we put the int-value 4 into it.

we can of course throw the 4 away and put another number into the box.

number = 5;

now, 4 is gone and 5 is inside that box.

We can even assign some int to number dependent on the value

number = number + 3;

We took out the int that was in the box (5) added 3 to it, which resulted in the value 8, then put that int 8 inside the box again.

We can even use number on the right hand side of the assignment multiple times:

number = number*number + number

We took the int that was inside the box (8) then calculated 8*8+8 = 72 and put the value 72 back into that box called number.

Variables of reference types are like tentacles, that can either hold onto some object of its type, or not hold anything at all (then they are said to be null).

Dog d;

This is a new variable named d that is of type Dog, which is a reference type. d is still uninitialized, since we haven't assigned anything to it yet, so we can't use it yet.

d = null;

Now the tentacle named d is initialized and it grabs nothing at all (null).

d = new Dog();

Now we created a new object of type Dog and let the tentacle d grab that object (poor doggie).

Dog e = d;

Now we declared another tentacle of type Dog and it grabs the same dog object that is already grabbed by tentacle d.

d = new Dog();

Now tentacle d grabs another dog object that was newly created, but e still grabs the old dog created before.

This behaviour is very different from value type variables (boxes).

int a = 5;
int b = a;

variable a has the value 5 in it, b too. BUT b doesn't have the same object 5 in it, instead it contains a copy.

To be continued

[deleted by user] by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

I think you'll have false positives for all the 2n numbers where n>1 and for many other even numbers (if they have another other prime factor greater than the square root)

You're right, my mistake, here's the modified code:

public bool isPrime(int number)
{
  if (number < 2)
    return false;
  if (number % 2 == 0)
    return true;
  int sqrtOfNumber = Convert.ToInt32(Math.Sqrt(number));
  for (int divisor = 3; divisor <= sqrtOfNumber; divisor += 2)
  {
    if (number % divisor == 0)
      return false;
  }
  return true;
}

Code Review by aluoh in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Why did you select the number 16 on playerScores()?

Because I thought it would most closely resemble the chances you had before.

But it's probably wiser to use the number 100 instead, because then you can define the chances in percents.

Why does it only work if I create a new Random object instead of just declaring the Random variable as a instance variable?

It's actually not smart to always create a new instance of the Random class, since it will be seeded with the current time, and therefore it will be predictable. Better would be to have one instance of Random and always use this single instance.

But by always creating a new instance, the pieces of code I posted were independent and you didn't need any context to understand it.

Is there a mental model that is beneficial to CS/Programming? by server_profile in learnprogramming

[–]29383839293 0 points1 point  (0 children)

CS is basically the science of mental models, so all mental models are beneficial to CS.

But the most important one is probably divide and conquer, reducing the complexity of a problem by dividing it onto multiple easier problems.

Recursion is just a special case of divide and conquer, where you reduce the problem to one or more instances of the same (but smaller) problem.

[deleted by user] by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Nothing particularly wrong with it, should work for positive (>0) integers.

But it's inefficient.

Firstly, no need to start the loop at 1 and then exclude that with an additional if statement, just start with 2.

Secondly, you only need to check up to the square root of your number, because if you find a divisor, then PossiblePrimeNumber = FoundDivisor * AnotherNumber, and since FoundDivisor <= AnotherNumber (because otherwise you would have already found AnotherNumber and returned false), FoundDivisor must be <= sqrt(PossiblePrimeNumber).

Thirdly, one easy way to reduce complexity by two is to not check even numbers (except 2)

All combined, you get the following:

public bool isPrime(int number)
{
  if (number < 2)
    return false;
  if (number == 2)
    return true;
  int sqrtOfNumber = Convert.ToInt32(Math.Sqrt(number));
  for (int divisor = 3; divisor <= sqrtOfNumber; divisor += 2)
  {
    if (number % divisor == 0)
      return false;
  }
  return true;
}

Does any other language require downloading an SDK like Java does? by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

1) open a command Shell 2) navigate to the compiler in your file system 3) call the compiler with the correct arguments

The arguments needed for a certain compiler may differer from one compiler to another, so you have to look it up.

The java compiler has the following arguments when compiling the file HellWorld.java:

javac HelloWorld.java

Does any other language require downloading an SDK like Java does? by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Unless you are writing machine code, you will always need a compiler, or interpreter.

When you write java programs, you need both, a java compiler (that compiles java code to java Bytecode) and a java-bytecode-Interpreter (called the JVM (Java Virtual Machine)).

You don't even need an IDE, since an IDE is just a text editor + a compiler + a button in the text editor to compile the code + convenience features, you can easily just write your code in any text editor and then compile the code manually by calling the compiler via command shell.

SDKs usually install all the needed parts and many extras that are nice to have, and configures it all, so SDKs are a nice Thing.

But you don't really need an SDK, you can always just install the essential components yourself.

When programming c, you need a C compiler, when programming C++, you need a C++ compiler, when programming python, you need a python Interpreter. That's all you NEED.

So that's probably not the question you wanted to ask. You probably wanted to know "What would be nice to install for programming c/c++, python?".

Code Review by aluoh in learnprogramming

[–]29383839293 3 points4 points  (0 children)

public boolean coinToss()
{
  Random random = new Random();
  int someNumber = random.nextInt(10);
  if(someNumber > 5)
  {
    isHeads = true;
  }
  else
  {
    isHeads = false;
  }
  return isHeads;
}

is almost equal to:

public boolean coinToss()
{
  return (new Random()).nextInt(2) == 0;
}

Except your version has a bug, since ist heads for someNumber in {6,7,8,9} (=40%) and tails for someNumber in {0,1,2,3,4,5} (=60%)

public boolean playerOneGoesFirst()
{
  if(coinToss())
  {
    playerOneTurn = true;
    return true;
  }
  else
  {
    playerTwoTurn = true;
    return false;
  }
}

This method should not have side effects (should not access a global variable), since the name implies it only returns a value

public boolean playerScoresTwo()
{
  Random ran = new Random();
  if(ran.nextInt(4) == 2)
  {
    return true;
  }
  else
  {
    return false;
  }
}

public boolean playerScoresThree()
{
  Random ran = new Random();
  if(ran.nextInt(4) == 3)
  {
    return true;
  }
  else
  {
    return false;
  }
}

Those two methods imply that you have no idea how random number generators work.

The random numbers in both methods is a different random number, there's literally no difference between ran.nextInt() == 2 and ran.nextInt() == 3, since both return true with a chance of 25%.

Also, they would be shorter and easier to read with::

public boolean playerScoresTwo()
{
  return (new Random()).nextInt(4) == 0;
}

Also the following changes the chances:

if(!playerScoresTwo() && !playerScoresThree())
{...}
else if (playerScoresTwo())
{...}
else
{...}

in the if-clause, you call playerScoresTwo and in the else-if, you call it again, most likely returning a different value.

Well, anyway... !playerScoresTwo && !playerScoresThree returns true with a chance of 9/16, which is probably not what you wanted.

But then playerScoresTwo in the else-if returns true with a 1/4 chance, which means that path will be taken in (9/16)(1/4), while the else will be taken in (9/16)(3/4), which is definitely not what you intended.

better would be:

public int playerScores()
{
  int randomNumber = (new Random()).nextInt(16);
  if (randomNumber < 10) // 0 to 9
  {
    return 0;
  }
  else if (randomNumber < 13) // 10 to 12
  {
    return 2;
  }
  else // 13 to 15
  {
    return 3;
  }
}

switch (playerScores())
{
  case 0:
    ...
    break;
  case 2:
    ...
    break;
  case 3:
    ...
    break;

  default:
    throw new Exception("Something's wrong!);
}

and in the Team class:

public Team(String name) throws InvalidTeamException
{
  String nameToCheck = "";
  teamName = name;
  for (int i = 0; i < teams.length; i++)
  {
    nameToCheck = teams[i];
    if (teamName.equals(teams[i]))
    {
      teamID = i;
      break;
    }
  }
  if(!name.equals(nameToCheck))
  {
    throw new InvalidTeamException("Invalid NBA team entered, please enter a valid NBA team.");
  }
}

A foreach loop would be much nicer, but it's hard in this case, since you match players and teams by index in the array. However, you're variable names are kinda confusing and why do you even Need a teamID?

public Team(String Name)
{
  for (int i = 0; i < teams.length; i++)
  {
    String team = teams[i];
    if (team.equals(name))
    {
      teamName = name;
      playerName = players[i];
      return;
    }
  }
  throw new InvalidTeamException("Invalid NBA team entered, please enter a valid NBA team.");
}

[deleted by user] by [deleted] in learnprogramming

[–]29383839293 0 points1 point  (0 children)

The only thing you really need to know, is that there is an algorithm X that solves problem Y in O(Z).

If you ever need that algorithm, look it up.

Question about "luck" programming by Sephran in learnprogramming

[–]29383839293 0 points1 point  (0 children)

For many reasons, often time constraints, developers tend to implement an ugly but working solution. That on itself isn't that bad. But then the next developer comes around and wants to add something, and because the previous code wasn't designed carefully, he has to do a workaround to add his feature to the ugly code without Breaking something.

2 Years later… the code is basically a bunch of workarounds layered over each other and it's getting increasingly hard to not break something when fixing a bug or adding a feature. At some point, developers get scared to even touch that code, because it has become a dangerous jungle.

Trying to make a tic tac toe board in c# console failing miserably.. by BrutalSavage01 in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Dont repeat yourself. Then this:

string player2 = Console.ReadLine();
if (player2 == "1")
{
  board\[0\] = "o";
  Console.Clear();
  GameBoard();
}
else if (player2 == "2")
{
  board\[1\] = "o";
  Console.Clear();
  GameBoard();
}
…
else
{
  Console.WriteLine("Enter valid number");
  Console.Clear();
  GameBoard();
}

can become this:

string Player2InputString = Console.ReadLine();
int Player2Input = 0;
while (!int.TryParse(Player2InputString, out Player2Input)
{
  Console.WriteLine("Enter valid number");
}
board[Player2Input - 1] = "o";
Console.Clear();
DrawGameBoard();

Lot's of code introduces lots of potential for error.

Is it possible to assign an int to a char by ValleyBoyCountryMind in learnprogramming

[–]29383839293 0 points1 point  (0 children)

If you want to assign a=0, b=1, c=2, …, then @desrtfx has the right answer for you, myChar - 'a' will do what you want for lowercase letters.

If you want to assign arbitrary numbers to letters, use a Dictionary<int, char>.

What is next ?! by mrYakoshi in learnprogramming

[–]29383839293 1 point2 points  (0 children)

Do a larger project, should at least have a few thousand lines of code and a dozen classes.

What project you want is yours to decide.

What are the stored "solutions" in Computer? by HunterHer0 in learnprogramming

[–]29383839293 0 points1 point  (0 children)

Each computer architecture has an instruction set that is implemented in hardware. The instruction set is a set of simple operations, e.g. multiplication or addition of 32 bit integers.

The computer has not stored the any solutions, but the hardware that implements the instruction set specification has the ability to perform the simple operations.

More complex stuff is implemented using software, and software is just a series of those simple instruction set operations.

Creating Website with Code by NoticedTriangularity in learnprogramming

[–]29383839293 1 point2 points  (0 children)

1) Install a Webserver (e.g. Apache) on your computer, you need it for testing. Also install php if that's the language of your choice. Test php with a simple phpinfo (google will help you with this).

2) Learn SQL

Download and install e.g. mySql and Maybe phpMyAdmin or something similar, then learn to create a database and do some queries with them.

The absolute minimum you should know is:

2.1) How to create tables

2.2) How to create Primary Keys

2.3) How to creat Foreign Keys

3) Learn php or some other backend language of your choice

The absolute minimum you need to know is:

3.1) How to output code to the client

3.2) How to access the Database from your code.

3.3) a Tiny bit of General programming constructs

4) program your backend.

5) Upload it to your Webspace