Tag Info

Hot answers tagged

143

Exceptions do not contain useful details because the concept of exceptions has not matured yet enough within the software engineering discipline, so many programmers do not understand them fully, and therefore they do not treat them properly. Yes, IndexOutOfRangeException should contain the precise index that was out of range, as well as the range that was ...


33

Why is it that many common exceptions from system components do not contain useful details? In my experience, there are a number of reasons that exceptions do not contain useful information. I expect that these sorts of reasons would also apply to system components - but I don't know for sure. Security focused people see exceptions as a source of ...


15

The big difference is that CMake is a cross-platform meta-build system. A single CMake project can produce the usual Unix/Linux makefile, a Visual Studio project for Windows, an XCode project for Mac, and almost any other non-meta build system you might want to use or support. I wouldn't say using make directly or even manually editing makefiles is ...


11

Given that your question is tagged c++, do not use macros! They should only be used for include guards and a few other esoteric uses. The proper solution here is a standalone inline function. Assuming that WIDTH and DEPTH are constants that are visible to the function, here is its definition: inline int f(int x, int y, int z) { return x + WIDTH * (y + ...


11

Technically, the correct answer to your question is that it's a contradiction in terms. As soon as you raise a variable into the class definition, it is no longer a local variable, it is a member variable. But I assume you're asking whether or not there's ever a good reason to raise a local variable up into member variable status. This question comes down ...


8

I think your colleague was right: you are designing your exception cases based on how simple it is to implement within the hierarchy, not based on the exception-handling needs of the client code. With one exception type and an enumeration for the error condition (your solution), if the client code needs to handle single error cases (for example, ...


7

I don't have an excess of C# experience, or C++ specifically, but I can tell you this - developer-written exceptions 9 out of 10 times are more useful than any generic exception you will ever find, period. Ideally yes, a generic exception will point you to exactly why the error occurred and you'll be able to fix it with ease - but realistically, in large ...


7

Mandatory disclaimers (1) Because people who have seen the code can't say anything about it, and people who can freely comment on it have never seen the actual code, all we can do here is to speculate, speculate, and to speculate. Therefore, here is not an answer, just a speculation. (2) This is not the typical way I write C++ because most of the projects ...


6

Is make really outdated? I don't think so. In the end, make is still powerful enough to provide all the functionality desired, like conditional compilation of changed source and alike. Saying make was outdated, would be the same as saying writing custom linker scripts was outdated. But what raw make doesn't provide, are extended functionalities and stock ...


6

C++ can do it the same way C does. All C++ gives you is easier-to-use containers that wrap much of the low-level detail. For example, a string class can (and does) hold a block of memory on the stack for short strings, only allocating a heap buffer for larger ones. This buffer is exactly like a C string buffer, if the string resizes, the string class will ...


6

From the comments... if everything else you have is C++ then the best answer is to write it in C++, building a mish-mash of different bits of programming languages is a right PitA to maintain and support. So, if you have C++ and need to resolve XML to JSON, it seems obvious to use the xml2json library that you linked to. It comes with sources so you can ...


5

To be honest, this seems a bit like going backward and programming with one hand behind your back, but if by "structured", you mean, like how people created programs before Object Orientation in procedural languages, then it's about how you start thinking about the problem. In your fourth paragraph, you are essentially still thinking in an Object Oriented ...


5

A question about the word "meaning" should not be discussed in meaningless terms like CFoo. Lets make a better example: #ifdef _WIN32 class MyUsbDriver { /* ... a public API to access a special USB devcice.. */ /* ... internal implementation: Windows sepecific.. */ }; #elif _LINUX class MyUsbDriver { /* ... exactly the ...


4

Actually, Randall Cook gave a very good answer here, but I would like to add something. Assumed you are going to implement "WagTail" for "man", the correct way of implementing it depends on the expectations of the code calling that method on mammals. If the caller expects some kind of error behaviour or exception to be thrown, then you could actually ...


4

The fundamental problem is that a pure virtual function was added very high in a class hierarchy which not all conceivable subclasses can plausibly support. This is why one should be very careful defining deep class hierarchies. I see a couple approaches. One approach is to simply provide an empty implementation of WagTail for the Man class. Hopefully this ...


4

What you are after is not very specific for OOP, and has absolutely nothing to do with inheritance. You are after proper modularization. Each of your features should be a component, that means either a single class, or a group of classes, with a well defined interface, and not directly dependent on the Application object. In the current situation each ...


4

The question is specifically asking why do so many exceptions thrown by "system components" (aka standard library classes) not contain useful details. Unfortunately, most developers do not write the core components in standard libraries, nor are detailed design documents or other design rationale necessarily made public. In other words, we may never know ...


4

A lot of procedural code is very OOP-like. Basically, instead of object.function(params), you do function(object, params). You can group your files accordingly. However, what a lot of long-time OOP programmers don't realize is how limiting it is that a function must belong to one and only one class, and that all such functions must be grouped into one ...


3

shared memory is not the complete picture for IPC, its a data-passing mechanism but you still need some way to inform the other process that some data has been updated and is available to be read. How you do this is up to you, typically you'd use an OS mutex or event object, each process waits on this to be set, the application writing sets it once its ...


3

But I cannot get my head around to how the boost library does this. The boost interprocess mechanism has three necessary components to work: memory-mapped file: a memory-mapped file needs to be created and passed to a boost.interprocess allocator. This allocator will take chunks of the file and use them as if they were returned by a std::allocator, ...


3

Once upon a time high level languages were just an idea. People tried to implement compilers. Back then there were severe hardware limitations - there were no graphical tools so "plain text" ended up being used for the input file format; computers typically had an extremely tiny amount of RAM so the source code had to be broken into pieces and the compiler ...


3

Default c++ constructors should be avoided if that means leaving object in a partially constructed place. This is obvious a good advice but things get complicated when you also disallow exceptions (as it does) Not necessarily. Consider these rules: a constructor should receive already validated arguments, and perform no operations outside of ...


3

Is there anything wrong with this approach? Is it at all typical to have "top level" classes that are more or less made up of several other base classes? Yes, and yes. Historically, it has been common practice among game engine developers to build their game entities in exactly the way you describe. I have, in fact, several well-regarded books that ...


3

Let me provide a different, yet typical, answer: it depends. A lot of people (me included) have troubles with inheritance hierarchies such as the ones @greyfade mentioned in the other answer. The clean code mantra "prefer composition over inheritance" stems from exactly that. However, by understanding the problems that are at the root of inheritance ...


3

As long as the underlying hp value is not public, it doesn't matter too much either way. Having Entity::hurt()/heal() directly access a private hp member is fine, and making them wrappers for a Health::hurt()/heal() call is also fine. Assuming it's not public, I would base the decision on what you expect to happen in the future. Will health probably be a ...


3

IT depends on the frequency of the task in your hand. If it is going to be one time job, or let's say once in a year job, do not worry about the speed, reduce the complexity of your code (independent from the language you are planning to use). Otherwise, if such XML files are created every day / hour and require JSON conversion, I will even prefer C since ...


3

The way we used to do sort-of OOP back in the days of C was by declaring a struct and then declaring functions that accept such a struct as their first parameter. Then sometimes the need for polymorphism would kick in, so we would have our struct contain not only data fields, but also pointers to functions. It is quite pointless, really, doing by hand what ...


2

How to visualise this data model where every node is pointing to a list? Is their a standard approach to visualise any data model, with the given data members and methods of a class? It is a doubly-linked list. The fact that each node "knows" the list is more of an implementation detail than it is a matter of the data model architecture. Possible ...


2

I typically use one of the two ways. First way is to typedef at the place-of-first-declaration. Second way is to typedef at each place-of-use, and make it only visible to that place-of-use (by putting it inside the class or method that uses it). (1) Put the typedef close to the type that is being wrapped. /* MyAttrType.h */ #include ...


2

This is a case of a Liskov Substitution Principle violation. Your mammal class appears to be misnamed, not all mammals have tails as you may have noticed. You may be able to solve it with multiple-inheritence. class Waggable + WagTail class Terrestrial + SunBathe class Cat : Terrestrial, Waggable class Man : Terrestrial Though I'm not sure ...



Only top voted, non community-wiki answers of a minimum length are eligible