2,834 reputation
1022
bio website
location
age
visits member for 4 years, 7 months
seen Apr 24 at 7:06

Jan
15
comment Is there a pattern for a more “natural” way of adding items to collections?
item.AddTo(items) suppose you have a language without extension methods: natural or not, to support addTo every type would need this method and provide it for every type of collection supporting appending. That is like the best example of introducing dependencies between everything I've ever heard :P - I think the false premise here is trying to model some programming abstraction to 'real' life. That often goes wrong.
Dec
31
comment Do ALL your variables need to be declared private?
@supercat good point. Haven't looked at this question for a while, but now I just realize that what you describe, and what the class in my answer does, i.e. just provide some kind of group of public data fields, is basically already builtin in lots of languages: tuple (or even the specialized case for 2 values, pair), or in case of Python even namedtuple. Especially the latter is semantically and functionally pretty much exactly the same as the class in the answer. This just again shows there's definitely a need for such types.
Dec
24
comment Safety-critical software and optimising compilers
Regarding third point: in the last couple of years alone I already filed a couple of optimization bugs to compiler vendors, and that's just me. Searching for 'gcc optimization bug' etc also reveals the problem is most definitely real.
Dec
23
comment Non-Object Oriented Programming in Object Oriented Language
Just a note, when you're leaning towards a bunch of if/else or switch statements, it's always an option to consider some sort of lookupt table. So another way to rewrite that goes along the lines of var dict = new Dictionary<string,Action<UserInfo>{ { "email", callEmailFunction }, { "sms", callSmsFunction } }; dict[ action ]( userInfo ); (dict can be static, error handling omitted)
Dec
21
comment readability vs shorter code in returning from function
@user949300 I know what you mean, but that is maybe not the best way to put it. Counterexample: if this gets called 10 million times in a loop but I'm only going to look at the output after a year, for some reason, it's still plenty fast and I shouldn't waste time on any worries.. Usually the principle is: don't do anything if there's no performance problem which manifests. If there is, measure. Then decide what needs fixing.
Dec
21
comment readability vs shorter code in returning from function
@Jules gooed point. Especially because I consider the second much, much clearer :] If the first would have been formatted in multiple lines it would be more readable though.
Dec
17
comment Are all magic numbers created the same?
At this point (24, 1024 etc) it sort of becomes a matter of preference I guess.. I'd still prefer a constant (not a #define btw) like numberOfHoursPerDay instead of just 24.
Dec
12
answered VCS - Better way to change location than pushing to branch?
Dec
10
comment Is there ever a reason to use an array when lists are available?
+1, often I have some operation for a couple of objects of the same type which are not in a collection. It's easy, short and clear to write foreach( var x in new []{ a, b, c ) ) DoStuff( x ) or new []{ a, b, c ).Select( ... ) etc
Dec
9
awarded  Enlightened
Dec
9
comment Allow iteration of an internal vector without leaking the implementation
+1, this is a pretty nice and safe implementation
Dec
9
comment Allow iteration of an internal vector without leaking the implementation
@BЈовић by not exposing the complete vector - hiding does not necessarily mean the implementation has to be literally hidden from a header and put in the source file: if it's private client cannot access it anyway
Dec
9
awarded  Nice Answer
Dec
8
comment Class design - should methods call other methods?
It's not really 'abstracting' the DB, that's one step further, I rather meant things even as simple as using a string constant instead of blatantly copy/pasting the complete query string so that when you have to change the string you have to do it in one place only (though in this case I'd just make a single function which executes the query and allows iteration over all resulting elements or so) - for the exeption handling: I don't really have sources for that, I also don't know php at all :]
Dec
8
comment Class design - should methods call other methods?
I mean both: your query strings, executing the query, catching the exception, formatting the exception log message
Dec
8
comment Class design - should methods call other methods?
Not really related to your question, but one thing this code could really benefit from is minimizing the amount of repetition aka not using copy/past coding aka adhering to the DRY pinciple: there are about 5 lines in each function which are exactly the same. That just screams for refactoring into one single method. On which then you can apply the question again :]
Dec
6
comment Allow iteration of an internal vector without leaking the implementation
note: though this certainly works and is accepted it's worth taking note of rwong's comments to the question: adding an extra wrapper/proxy around vector's iterators here would make clients independent of the actual underlying iterator
Dec
6
comment Allow iteration of an internal vector without leaking the implementation
@rwong good points indeed - I reckon my solution is maybe too simplistic. Yet, for 1) I'm probaby used to using auto unless it's not possible, a mechanism which makes one way less dependent of actual iterator type and 2) that is a design decision made and hence has to be documented anyway - sure using vector is dangerous because it's iterators can be invalidated but it's not like that makes vector unusable. Though depending on the exact usage which the OP doesn't mention set/list/... might indeed be a better choice here.
Dec
6
answered Allow iteration of an internal vector without leaking the implementation
Dec
6
comment Allow iteration of an internal vector without leaking the implementation
What @DocBrown says is likely the appropriate solution - in practice this means you give your AddressBook class a begin() and end() method (plus const overloads and eventually also cbegin/cend) which simply return the vector's begin() and end(). By doing so your class will also be usable by all most std algorythms.