Hot answers tagged

8

The programming paradigm that organizes the software into functions is called procedural programming. For example, C is a procedural programming language. Most dynamic languages like JS or Python can be used in a procedural style. While you are also organizing your code into separate files, there is no commonly used name that describes this. For example, ...


7

Does this extend to using exceptions to handle argument validation? Even more so. Consider C#, which discourages exceptions for control flow, but encourages enforcing function preconditions with exceptions. Should it therefore fail more loudly? Does Python Zen "Errors should never pass silently." apply here? Yes. As it stands the caller has no ...


4

I'm still not sure whether to use exceptions or guard clauses for dealing with invalid arguments. Guard clauses and exceptions aren't mutually-exclusive. Failing the condition in a guard clause is often grounds for throwing an exception. In a lot of ways, something like guard_against_none(value) is a shorthand for "throw something if value is None." It ...


3

In C++, constructors are special operators. There is special syntax for calling a base class constructor: class A { ... }; class B: public A { public: B() : A() { ... } // ^^^^^ }; If the base constructor is not called explicitly, the default constructor for the base class will be called automatically. This is important for C++'s memory model and ...


3

Q: For the functionalities that are similar for the mobile and web admin, how can avoid duplicating the code? Essentially, encapsulating the domain and the business logic in their own components and modules so the web layer acts as a driver or facade. This is what web interfaces are meant to be, an extension which allows our business expand up to the WWW. ...


2

The support for random.sample(a.keys(), 1)[0] is an implementation accident and can break down in future Python3 versions. There is good reason to recommend that neither choice() nor sample() supports a dictionary or a 'dict_keys' object as an argument. As Tim Peters say: The dict type doesn't support efficient random choice (neither do sets, by the way), ...


2

Well, you wrote to take care of converting arguments to public methods to some unit system internal to the package so that private (or hidden, since we're talking about Python) methods don't have to "think" about units. so for yourself, you already see the major drawback of the "arbitrary unit" approach: it is way less tedious any less error prone to ...


1

Can both programs run from the same node? Are the programs happy with sharing their node? What level of service are you trying to establish? Is there enough compute resources on that single droplet to maintain both programs, especially during peak load? Is there an overriding concern such as security, reliability, or a business/country policy/regulation ...


1

Related code should be vertically close. You have the situation where, depending on how you look at it, different classes are related in different ways. I think the driving force here is the model and so I would put views related to a specific model in their own files views/foo.py views/bar.py.


1

I really like the answer about structured versus not, so I'm just providing a concrete example below. SimpleNamespace will accept keys that begin with _. If you're looking for a quick and easy way to turn, say, JSON you don't control into objects with field names, this is very handy: d = {"_id": 2342122, "text": "hi there!"} # Elasticsearch gives this id! ...


1

This is about ownership and the responsibility to close the file. You can pass on a stream or file handle or whatever thingy that should be closed/disposed at some point to another method, as long as you make sure it is clear who owns it and certain it will be closed by the owner when you are done. This typically involves a try-finally construct or the ...


1

Jay's answer is correct that you can do this thing. The normal way to describe this is that you'll be developing the python algorithm (function) into a "service" (standalone application) that your PHP app will "consume" (make calls to). This is a fine idea if the algorithm is complex enough that you want to think of it as its own program; sequestering your ...


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