Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searchable Names Part1 in Good code section incorrect variable is used #125

Open
IsaacMorris1980 opened this issue Jul 7, 2023 · 2 comments

Comments

@IsaacMorris1980
Copy link

IsaacMorris1980 commented Jul 7, 2023

In Searchable Names Part 1 seaction 2

Bad:

// What the heck is data for?
var data = new { Name = "John", Age = 42 };

var stream1 = new MemoryStream();
var ser1 = new DataContractJsonSerializer(typeof(object));
ser1.WriteObject(stream1, data);

stream1.Position = 0;
var sr1 = new StreamReader(stream1);
Console.Write("JSON form of Data object: ");
Console.WriteLine(sr1.ReadToEnd());

Good:

var person = new Person
{
    Name = "John",
    Age = 42
};

var stream2 = new MemoryStream();
var ser2 = new DataContractJsonSerializer(typeof(Person));
ser2.WriteObject(stream2, data);

stream2.Position = 0;
var sr2 = new StreamReader(stream2);
Console.Write("JSON form of Data object: ");
Console.WriteLine(sr2.ReadToEnd());

As you can see in ser2.WriteObject(stream2, data); should be person not data as data has not been declared and to match what bad code block is doing

@IsaacMorris1980 IsaacMorris1980 changed the title Searchable Names Part1 in Good code section data is used instead of person Searchable Names Part1 in Good code section incorrect variable is used Jul 7, 2023
@olmeyer
Copy link

olmeyer commented Feb 9, 2024

You are also not following the very first rule "Avoid using bad names". What do "ser2 and "sr2" stand for?
"stream2" is not a good name either.

@IsaacMorris1980
Copy link
Author

@olmeyer I agree i was not asking about all the incorrect items on the good side

ar person = new Person
{
    Name = "John",
    Age = 42
};

var stream2 = new MemoryStream();
var ser2 = new DataContractJsonSerializer(typeof(Person));
ser2.WriteObject(stream2, data);  // i was asking about here should it not say person instead of data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants