Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Che Delilas
Nov 23, 2009
FREE TIBET WEED
I realize this is :can:, but does anyone have any strong opinions on commenting style for ASP.NET MVC Controller Action header comments specifically?

For example, I have an action:

C# code:
[HttpGet]
public ActionResult Products()
{
   return View(dbContext.Products.ToList());
}
What information should I be adding in the header comments? I know the standard advice is to talk about why things are being done this way, but "displays products because people want to see products" is akin to "assigns the number 5 to the integer variable called myNumber."

Please don't fight with each other over this. :shobon: I'm less interested in the "right way" as I am just learning what other people do in these situations.

Che Delilas fucked around with this message at 00:38 on Nov 29, 2014

Adbot
ADBOT LOVES YOU

Dromio
Oct 16, 2002
Sleeper

Che Delilas posted:

What information should I be adding in the header comments?

I wouldn't put in a comment at all in that case. What needs explaining?

Horn
Jun 18, 2004

Penetration is the key to success
College Slice

Dromio posted:

I wouldn't put in a comment at all in that case. What needs explaining?

Agreed. Don't add comments that are useless like "pull data from db using id". The code itself should be enough in those cases. I add comments where the why of what I'm doing might not be clear

Dietrich
Sep 11, 2001

Any time you feel like you have to write a comment to explain your code, it means you need to re-write your code.

(Or you're doing something that makes absolutely no logical sense because there's a framework bug you have to deal with or something)

ljw1004
Jan 18, 2005

rum
I write comments for the invariants in my code - loop invariants, termination proofs, object state invariants, concurrency/async-reentrancy invariants - because they're the only really important thing about my code, and they're never straightforwardly implied or justified by the code itself.

In the case of ASP.NET WebAPIs, I also write in comment an example http request (usually just the Uri) and a sample response. This example is present in unit tests but I like to have it closer to the code. I think it's crucial to always ground ourselves in the raw messages that get sent over the wire, not just in higher level abstractions of them.

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer

Funking Giblet posted:

I think you're missing something major! You can validate the configuration at runtime which will find unmapped fields, or write a unit test to do as much. Not to mention injectable value and type resolvers which can be reused across types.
Yeah I've seen the validation as unit tests thing, but the places I've seen it usually involve writing a bunch of exceptions for the missing/extra/renamed fields. That's when I get to: why bother, you're effectively writing the maps anyway at this point?
I guess I haven't seen it work out simpler in the long run (though I've only really seen it used extensively twice), but if it works for you great.

bpower
Feb 19, 2011
Is it just me or is a lot of the .Net documentation a bit poo poo?

I want to learn about the different ways to render links with razer. So I end up here.

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions_methods(v=vs.118).aspx

As a learning tool thats bloody awful. Where do you guys go to learn about the small things? I seem to just bounce around the web looking for examples.

brap
Aug 23, 2004

Grimey Drawer

Dietrich posted:

Any time you feel like you have to write a comment to explain your code, it means you need to re-write your code.

(Or you're doing something that makes absolutely no logical sense because there's a framework bug you have to deal with or something)

Overcommenting is terrible, and a code style that "documents" itself well is important, but it's too much to say that good code never warrants comments.

Funking Giblet
Jun 28, 2004

Jiglightful!

Destroyenator posted:

Yeah I've seen the validation as unit tests thing, but the places I've seen it usually involve writing a bunch of exceptions for the missing/extra/renamed fields. That's when I get to: why bother, you're effectively writing the maps anyway at this point?
I guess I haven't seen it work out simpler in the long run (though I've only really seen it used extensively twice), but if it works for you great.

Mapper.AssertConfigurationIsValid is all you need.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

bpower posted:

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions_methods(v=vs.118).aspx

As a learning tool thats bloody awful. Where do you guys go to learn about the small things? I seem to just bounce around the web looking for examples.

Stackoverflow is usually pretty good, though they close "best practices" or opinion questions; they're not interested in discussion. Codeproject can have some decent tutorials. Don't expect them to be up-to-date on everything, but they can be a good overview and starting point. Usually googling your specific issue or question will get you something from one or both of those sites first thing.

Dietrich
Sep 11, 2001

fleshweasel posted:

Overcommenting is terrible, and a code style that "documents" itself well is important, but it's too much to say that good code never warrants comments.

I didn't say that, though.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
Thanks guys, got some good viewpoints.

RICHUNCLEPENNYBAGS
Dec 21, 2010

Dietrich posted:

Any time you feel like you have to write a comment to explain your code, it means you need to re-write your code.

(Or you're doing something that makes absolutely no logical sense because there's a framework bug you have to deal with or something)

I mean.... that's an OK rule of thumb, but that's not really true, entirely.

Che Delilas posted:

Stackoverflow is usually pretty good, though they close "best practices" or opinion questions; they're not interested in discussion. Codeproject can have some decent tutorials. Don't expect them to be up-to-date on everything, but they can be a good overview and starting point. Usually googling your specific issue or question will get you something from one or both of those sites first thing.

Programmers.SE is a bit more in that direction.

UnhappyEggWhisk
Oct 25, 2003

Che Delilas posted:

I realize this is :can:, but does anyone have any strong opinions on commenting style for ASP.NET MVC Controller Action header comments specifically?

For example, I have an action:

C# code:
[HttpGet]
public ActionResult Products()
{
   return View(dbContext.Products.ToList());
}
What information should I be adding in the header comments? I know the standard advice is to talk about why things are being done this way, but "displays products because people want to see products" is akin to "assigns the number 5 to the integer variable called myNumber."

Please don't fight with each other over this. :shobon: I'm less interested in the "right way" as I am just learning what other people do in these situations.

If you must:

C# code:
/// <summary>
/// Displays a <see cref="SomeController.Products"/> view.
/// </summary>
/// <returns>A <see cref="SomeController.Products"/> view.</returns>
[HttpGet]
public ActionResult Products()
{
   return View(dbContext.Products.ToList());
}

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

UnhappyEggWhisk posted:

If you must:

C# code:
/// <summary>
/// Displays a <see cref="SomeController.Products"/> view.
/// </summary>
/// <returns>A <see cref="SomeController.Products"/> view.</returns>
[HttpGet]
public ActionResult Products()
{
   return View(dbContext.Products.ToList());
}

And, really, you mustn't.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

fleshweasel posted:

Overcommenting is terrible, and a code style that "documents" itself well is important, but it's too much to say that good code never warrants comments.

I want to emphasize here that while in .NET land code is often readable all on its own and this principle applies, this should not be taken as general-purpose programming advice. You might one day work in a horrifying language where you forget what your own code does the day after you write it because even the most basic expressions are horrifying. Please, please comment to your heart's content if you ever reach such a day.

Dietrich
Sep 11, 2001

Why the hell is Random.Next() not thread safe?

wwb
Aug 17, 2004

Newf posted:

In favor of the css white-space: nowrap? What's the advantage?

That is one way though there are a number of CSS techniques one could use (ie -- min width) to get there.

The advantage is that you are no longer jamming presentation elements (HTML nonbreaking spaces) into your code and it is overall cleaner and easy to manage long term and it is doing the right thing.

aBagorn
Aug 26, 2004
Hey, just looking for a couple different opinions here. At my annual review, one of the goals in the next year (in line with me taking more of a leadership role here) is to start actually implementing a peer code review process, and I'm wondering how any of you that have more formalized processes go through with it?

e: v Exactly the type of answer I was looking for! Although 1 and 2 will be hard to implement currently as I am also in charge of ramping up the unit testing enforcement as well.

We are (again, slowly) migrating over to TFS, but we still have some old stuff using SVN

aBagorn fucked around with this message at 19:46 on Dec 1, 2014

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

aBagorn posted:

Hey, just looking for a couple different opinions here. At my annual review, one of the goals in the next year (in line with me taking more of a leadership role here) is to start actually implementing a peer code review process, and I'm wondering how any of you that have more formalized processes go through with it?

1) do unit tests pass?
2) look at new unit tests
3) look at code for any obvious architectural / readability problems
4) compiles with no warnings or code analysis violations

Code review is not to validate functionality

If you're using tfs 2012 or later, the code review feature makes this easier to manage and audit and can be enforced as a requirement before checkin. If you're using git, use a pull request for the same basic idea.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Dietrich posted:

Why the hell is Random.Next() not thread safe?

I'm assuming because Random is from old .NET and the C# devs chose performance over thread safety, and because locking before calling .Next is easy if you need it.

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer
Try to get the responsibility for reviews to be shared by the whole team. You don't want to end up as a "gatekeeper" or seen as a blocker to code getting to production. The team should be responsible for code quality.

In reviews just make sure you aren't too hard (especially early on). You're looking for acceptable not perfect. Even if no code is changed as a result of reviews you'll be getting benefits from better visibility of the code base and what your colleagues are doing day-to-day.

Gul Banana
Nov 28, 2003

Dietrich posted:

Why the hell is Random.Next() not thread safe?

because it mutates state..? exact same reason that
globalPreviousRandomNumber = currentRandomNumber;
wouldn't be.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Bognar posted:

And, really, you mustn't.

Yeah I mean, that's the obvious "function header comment just to have one" type of comment. I couldn't think of anything novel to put there, especially since it's for a Controller class and not a library I'm going to distribute or something. Good to know I don't really need to bother in cases like that.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Gul Banana posted:

because it mutates state..? exact same reason that
globalPreviousRandomNumber = currentRandomNumber;
wouldn't be.

Java's Random is thread-safe by default. Just because you mutate internal state doesn't mean you can't lock while doing so.

Sedro
Dec 31, 2008
Java also provides "thread safe" versions of their collections

bpower
Feb 19, 2011
Entity Framework question about nullable Guids in the db.


I have the following table with 3 Guids. ReportsToId is nullable


CREATE TABLE [dbo].Person](
[PersonId] [uniqueidentifier] NOT NULL,
[ReportsToId] [uniqueidentifier] NULL,
[BusinessId] [uniqueidentifier] NOT NULL)


When I get a new Person from the front end the ReportsToId might be an actual Guid or the Empty "0000000000-0000000-0000" .

var p = PersonFromFrontEnd();
db.People.Add(p);
db.SaveChanges;


This runs
insert into person([PersonId] , [ReportsToId] , [BusinessId] )
values ('454656-56465476-7789','000000-0000000000-0000000','566775765-756698698-57765')

When I actually want
insert into person([PersonId] , [ReportsToId] , [BusinessId] )
values ('454656-56465476-7789',null,'566775765-756698698-57765')

Or
insert into person([PersonId] , [BusinessId] )
values ('454656-56465476-7789','566775765-756698698-57765')


How can I do this?

Potassium Problems
Sep 28, 2001
It should work if you define the property as a nullable Guid, but you'd have to manually set it to null if you have an empty one. Don't know if there's some kind of mapping you could do in EF.

code:
public class Person 
{
  public Guid PersonId { get; set; }

  public Guid? ReportsToId { get; set; }

  public Guid BusinessId { get; set; }
}

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

bpower posted:

When I get a new Person from the front end the ReportsToId might be an actual Guid or the Empty "0000000000-0000000-0000" .

Either fix your front-end or explicitly check for Guid.Empty and replace with null.

bpower
Feb 19, 2011
Thanks guys. I dont know why I didn't do that already. Brain freeze I guess.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



wwb posted:

That is one way though there are a number of CSS techniques one could use (ie -- min width) to get there.

The advantage is that you are no longer jamming presentation elements (HTML nonbreaking spaces) into your code and it is overall cleaner and easy to manage long term and it is doing the right thing.

NBSPs still has perfectly legitimate uses, though. Take the surname 'von Neumann' for example. That could be considered one token and to express that to a browser which differentiates between breaking and non-breaking spaces you'd send 'von&nbsp;Neumann'. That's not mashing presentation into 'code', it's correctly representing information.

I'm not arguing for using NBSPs to pad content or whatever, but I don't think using them to tell the browser that "this string of characters is one thing; treat it as such" is wrong or bad or whatever.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Dumb question but you guys are my go-to for dumb questions.

I'm building a List (of String), and I want to check if something is already in the list before adding it. However what I'm not sure of, does the .Contains method match any part of any string in the list, or only entire items? So for example if the list is currently:
code:
ruby
emerald
black-diamond
And I want to add 'diamond', will List.Contains("diamond") return true because it's part of the "black-diamond" entry? Or will it return false?

Jewel
May 2, 2009

Scaramouche posted:

Dumb question but you guys are my go-to for dumb questions.

I'm building a List (of String), and I want to check if something is already in the list before adding it. However what I'm not sure of, does the .Contains method match any part of any string in the list, or only entire items? So for example if the list is currently:
code:
ruby
emerald
black-diamond
And I want to add 'diamond', will List.Contains("diamond") return true because it's part of the "black-diamond" entry? Or will it return false?

Just checked the docs;

quote:

This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable<T>.Equals method for T (the type of values in the list).

Which is what I thought. So it just calls .Equals (==) for each of them, so unless "diamond" == "black-diamond" returns true then it's not gonna match. Docs are handy! :)

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Scaramouche posted:

Dumb question but you guys are my go-to for dumb questions.

I'm building a List (of String), and I want to check if something is already in the list before adding it. However what I'm not sure of, does the .Contains method match any part of any string in the list, or only entire items? So for example if the list is currently:
code:
ruby
emerald
black-diamond
And I want to add 'diamond', will List.Contains("diamond") return true because it's part of the "black-diamond" entry? Or will it return false?

It's an exact match. Think of it like this: List<T> accepts any object. If I had a List<Customer>, what would a partial match of a customer be? It just checks for reference equality.

If you need a partial match, you could do list.Any(l => l.Contains("diamond");

Of course, keep in mind that's case sensitive.

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.

Scaramouche posted:

Dumb question but you guys are my go-to for dumb questions.

I'm building a List (of String), and I want to check if something is already in the list before adding it. However what I'm not sure of, does the .Contains method match any part of any string in the list, or only entire items? So for example if the list is currently:
code:
ruby
emerald
black-diamond
And I want to add 'diamond', will List.Contains("diamond") return true because it's part of the "black-diamond" entry? Or will it return false?

Dotnetfiddle.net is a handy place to bang out little test snippets when creating a new project doesn't seem worth the hassle.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Hah, thanks guys. I'm working off of mobile hotspot while switching broadband providers and my first Googling didn't give me what I wanted so I figured it'd be faster to ask here rather than wait 10 seconds for each page load.

Newf: That looks ideal, will have to see how it handles low bandwidth usage.

EDIT-In retrospect it feels like I'm spending more time checking if the list contains things than actually inserting into it. Might switch to hashset

Scaramouche fucked around with this message at 05:52 on Dec 5, 2014

Mr Shiny Pants
Nov 12, 2012

Scaramouche posted:

Dumb question but you guys are my go-to for dumb questions.

I'm building a List (of String), and I want to check if something is already in the list before adding it. However what I'm not sure of, does the .Contains method match any part of any string in the list, or only entire items? So for example if the list is currently:
code:
ruby
emerald
black-diamond
And I want to add 'diamond', will List.Contains("diamond") return true because it's part of the "black-diamond" entry? Or will it return false?

If you don't want duplicates a Set might be better.

Inverness
Feb 4, 2009

Fully configurable personal assistant.
New MSDN blog post: Introducing .NET Core. That's pretty interesting.

I was just reading the Reddit thread on it and saw a comment about AppDomains from a dev:

quote:

There are no AppDomains in .NET Core (nor any other .NET platform that we've released since .NET Framework itself). AppDomains add a significant overhead/complexity to the runtime, and are not what we internally call "pay for play". That is, regardless if you use them or not, AppDomains result in a ~5% overhead for all apps. For example, in mscorlib (which is domain neutral, and is loaded once for all AppDomains), every static field access requires an extra indirection. AppDomains made sense back in v1 of .NET, where sandboxing and isolation was a goal. In recent years however, partial trust is no longer guaranteed.
My only concern when it comes to AppDomains not being available is how I would unload an assembly in runtime without them. I've never really understood the whole partial trust security stuff or the deluge of security-related attributes all across the framework.

aBagorn
Aug 26, 2004
Design question!

I currently have a project (in the baby stages) that has this basic setup


Ember JS
Web API 2
Service/Business Logic Layer
Entity Framework

I've obviously been trying to keep EF model objects away from my Web API layer, but when trying to flatten everything into nice DTOs for Ember JS, it's becoming increasingly annoying to do so. Should I be flattening in the service layer? I am mainly using that for data access and some calculations that need to be done before objects are saved to the DB.

I had been serializing to JSON at that layer and passing up to the Web API in that format and had hoped to just pass that right along to ember without touching it, but based on the requirements of the views, that is no longer an option (the relationships between the classes are all many to many due to needing historical data, and the view only needs to grab the current relationship).

Forgive me if I'm explaining this incorrectly.

Adbot
ADBOT LOVES YOU

zergstain
Dec 15, 2005

After a while of Googling, I haven't been able to get a clear answer on whether QPC() measures CPU time or actual time. I need an operation to occur every 100ms, so I need actual time. If not QPC(), what should I use?

I'm stuck in MFC or WINAPI land. Maybe the wrong thread, but it was the closest I could find.

I don't want to type the actual function name on my phone.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply